Click here to Skip to main content
12,630,847 members (30,980 online)
Click here to Skip to main content
Add your own
alternative version

Stats

268.5K views
589 downloads
61 bookmarked
Posted

AutoSig: A browser helper object that automatically adds a different signature when you post a message to a CP forum

, 1 Jul 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A browser helper object that automatically adds a different signature when you post a message to a CP forum
<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (

,

etc) -->

Introduction

I was inspired to write this article after seeing this article by Kant. Kant wrote a utility that allows creation of quotes collection and saving in XML format. User can chose to copy a random quote to the clipboard which can be pasted in the signature field when posting a message to a CP message board. The same feature is also available in Desktop Bob. I imagined that it would be kind of nice if a random quote automatically got into the signature field. This utility does that. When you install the utility and navigate to any url in CP from where you can post a message, the signature field is automatically filled with one of the quotes from an XML file. All you need to do is to write some quotes in an XML file either manually or using the CodeProjectQuote utility. A default XML file (copied from Kant's article) is provided with the setup and the source.

Installation and Use

Here are the steps you need to take to install and use the utility.

  1. Download and extract the setup zip file
  2. Double click on the AutoSigSetup.msi from Windows Explorer. You need to have windows installer 2.0 on your machine. It is very likely that your system already has it. You also need to be an administrator on the machine to install the utility.
  3. Navigate to a URL in code project from where you can post a new message. The signature field is automatically filled in. Every time you renavigate (not refresh) to this page a different signature comes up. Note that your default signature will be replaced. If you want to keep your default sig just rfresh the page.
  4. Obviuosly you will need to add your own quotes to the signature XML file. The default installation of the utility places an XML file named sigs.xml in the same folder as the app itself (Although this is not a very good practice in general). You can edit the file there itself.
  5. If you chose you can point to a different location for the signature XML file by modifying the registry value SigFilePath under the key HKEY_CURRENT_USER\Software\CodeProject\AutoSig. The string data you place in this registry value should be the full path of the new signature XML file.

Building the Code

If instead of using the installation app you decide to build the utility yourself, you need to take the following steps.

  1. Download and extract the source zip file
  2. Open AutoSig2000.sln if you are using VS.NET 2002 or AutoSig.sln if you are using VS.NET 2003.
  3. Build the solution.
  4. Open a command prompt window and go to the output directory of the AutoSig2002 or AutoSig project
  5. Type Regasm /codebase AutoSig.dll . This will register the utility.
  6. Copy sigs.xml file to the output directory or see step 5 in the Installation and use section to specify a different path
  7. Edit sigs.xml file to add your own quotes

How it Works

The utility is a Browser Helper Object. A Browser Helper Object is a COM object that is instantiated for every Internet Explorer browser window. Internet Explorer passes the IWebBrowser2 interface pointer to the Browser Helper Object. It is upto the Browser Helper Object to make use of it in whatever way it wants to. The BHO needs to implement the COM interface IObjectWithSite. In C# the interface looks like this.

[
    ComVisible(true),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
    Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IObjectWithSite
{
    [PreserveSig]
    int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
    [PreserveSig]
    int GetSite(ref Guid guid, out IntPtr ppvSite);
}
Internet explorer calls the SetSite method after it instantiates the BHO. The site parameter is actually an object which implements IWebBrowser2 interface. In case of AutoSig the implementation of this method reads configuration values from the registry and adds an event handler to the DocumentComplete event and stores the object in a member variable. When Internet Explorer window closes, it calls the SetSite method again passing a null value. The AutoSig BHO removes the event handler when such condition occurs.
if (site != null)
{
  webBrowser = (WebBrowser)site;
  webBrowser.DocumentComplete +=
     new DWebBrowserEvents2_DocumentCompleteEventHandler(
         this.OnDocumentComplete);
}
else
{
    webBrowser.DocumentComplete -=
      new DWebBrowserEvents2_DocumentCompleteEventHandler(
         this.OnDocumentComplete);
    webBrowser = null;
}

Now everytime the browser completes navigation to a web site the OnDocumentComplete method is called. The code in this method does the following:-

  1. Checks whether the naviagtion URL belongs to one of the CodeProject domains.
  2. If the above condition was true it sees whether the URL is one used in CP to post a message to a forum (either a new message or a reply).
  3. If both the conditions are true. The code obtains a reference to the DOM object corrensponding the the textarea element where the user types a signature.
    object sigObj = document.all.item(destElemId, 0);
    IHTMLTextAreaElement sig = sigObj as IHTMLTextAreaElement;
  4. Now all that needs to be done is to get a signature from the XML file and assign it to the value property of the textarea. This is done asynhcronously for the simple reason that reading from the XML file may take some time during that time the user can still continue typing his message. In practice however I find that the time taken is negligible. The XML file could have been read at once and stored in memory but I prefer conserving memory as much as possible so the file is read each time. The asynchronous call is done using an asynchronous delegate.
    //private delegate void SetRandomSigDelegate(
    //        IHTMLTextAreaElement textArea);
    
    SetRandomSigDelegate delg = new SetRandomSigDelegate(
        this.SetRandomSig);
    delg.BeginInvoke((IHTMLTextAreaElement)sig, 
        new AsyncCallback(this.OnAsyncCallComplete), delg);

    The SetRandomSig method which gets called from a thread in the threadpool reads the xml file and selects a value from it and places it in the textarea.

Final Comments

I hope the utility proves useful to fellow CPians. Currently there is only one known bug, that is when the page is refreshed the signature is not set. This happens because the DocumentComplete event does not get fired. Any suggestions on how to solve this problem is welcome. Thanks to Kant and Daniel Truni for providing inspiration for the article.

Update July 2, 2003

A common problem people faced was that the installation app did not work. This was because microsoft.mshtml.dll - an assembly required by the project was not included with the setup. This was done because this file increased the setup size to almost 2 MB. In this version now the dependency on the assembly is removed. Instead I have created a new assembly called mshtmlsubset.dll which includes only those interfaces used by AutoSig. This reduces the size of installation drastically.

History

  • July 2, 2003 - Fixed to remove dependency on mshtml.tlb
  • June 7, 2003 - Initial Release <!------------------------------- That's it! --------------------------->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rama Krishna Vavilala
Architect
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralIt worked! Pin
Nacnudrium15-Jun-09 13:44
memberNacnudrium15-Jun-09 13:44 
Generaltest 10 - it will work this time Pin
Nacnudrium15-Jun-09 13:43
memberNacnudrium15-Jun-09 13:43 
Generaltest n Pin
Nacnudrium15-Jun-09 13:33
memberNacnudrium15-Jun-09 13:33 
Generaltest 8 Pin
Nacnudrium15-Jun-09 13:22
memberNacnudrium15-Jun-09 13:22 
GeneralTest 7 Pin
Nacnudrium15-Jun-09 13:20
memberNacnudrium15-Jun-09 13:20 
Generaltest 6 - sigh Pin
Nacnudrium15-Jun-09 13:13
memberNacnudrium15-Jun-09 13:13 
Generaltest 5 - hope it works this time Pin
Nacnudrium15-Jun-09 13:09
memberNacnudrium15-Jun-09 13:09 
Generaltest 4 - sorry Pin
Nacnudrium15-Jun-09 13:05
memberNacnudrium15-Jun-09 13:05 
Generaltest 3 Pin
Nacnudrium15-Jun-09 13:02
memberNacnudrium15-Jun-09 13:02 
Generaltest 2... Pin
Nacnudrium15-Jun-09 11:56
memberNacnudrium15-Jun-09 11:56 
Generaltest Pin
Nacnudrium15-Jun-09 11:55
memberNacnudrium15-Jun-09 11:55 
GeneralTest Pin
RBRiddick6-May-09 5:48
memberRBRiddick6-May-09 5:48 
GeneralRe: Test Pin
RBRiddick7-May-09 0:50
memberRBRiddick7-May-09 0:50 
Generaltest test Pin
VuNic30-Mar-09 8:57
memberVuNic30-Mar-09 8:57 
Generaltest Pin
chen_kinnrot26-Jan-09 10:22
memberchen_kinnrot26-Jan-09 10:22 
Generaltest 1/11/2009 Pin
jmort11-Jan-09 21:05
memberjmort11-Jan-09 21:05 
General2nd Test 1/11/2009 Pin
jmort11-Jan-09 18:08
memberjmort11-Jan-09 18:08 
GeneralTest 1/11/2009 Pin
jmort11-Jan-09 17:56
memberjmort11-Jan-09 17:56 
Generalit Does not work Pin
kuszob31-Dec-08 19:41
memberkuszob31-Dec-08 19:41 
Generaltest Pin
kuszob31-Dec-08 19:31
memberkuszob31-Dec-08 19:31 
Generaltest Pin
cre006730-Dec-08 5:52
membercre006730-Dec-08 5:52 
Generaltest Pin
savage_panda29-Oct-08 15:35
membersavage_panda29-Oct-08 15:35 
Generaltest Pin
djbobo10-Sep-08 6:35
memberdjbobo10-Sep-08 6:35 
GeneralKnowledge Pin
shamshadgem2-Sep-08 19:17
membershamshadgem2-Sep-08 19:17 
GeneralGames Pin
shamshadgem2-Sep-08 18:51
membershamshadgem2-Sep-08 18:51 
GeneralHis this is test Pin
SmartCodar16-Jul-08 19:33
memberSmartCodar16-Jul-08 19:33 
Questionwhy it does not work? Pin
jiqihuman12-Jun-08 7:23
memberjiqihuman12-Jun-08 7:23 
AnswerRe: why it does not work? Pin
SmartCodar16-Jul-08 4:17
memberSmartCodar16-Jul-08 4:17 
Generalhi Pin
jiqihuman11-Jun-08 4:40
memberjiqihuman11-Jun-08 4:40 
Generaltt Pin
jiqihuman11-Jun-08 4:35
memberjiqihuman11-Jun-08 4:35 
Generaltest again Pin
jiqihuman11-Jun-08 4:34
memberjiqihuman11-Jun-08 4:34 
Generalhi Pin
jiqihuman11-Jun-08 4:33
memberjiqihuman11-Jun-08 4:33 
Generalhello ,again Pin
jiqihuman11-Jun-08 4:29
memberjiqihuman11-Jun-08 4:29 
Generalhello Pin
jiqihuman11-Jun-08 4:22
memberjiqihuman11-Jun-08 4:22 
GeneralTest Pin
Member 384357530-May-08 11:44
memberMember 384357530-May-08 11:44 
Generaltest Pin
redbuddy28-Feb-08 9:20
memberredbuddy28-Feb-08 9:20 
GeneralRe: test Pin
kaiserlew20-Apr-08 16:28
memberkaiserlew20-Apr-08 16:28 
Generaltest Pin
Member 462175225-Jan-08 14:38
memberMember 462175225-Jan-08 14:38 
GeneralNot Working Pin
edvz8-Dec-07 22:32
memberedvz8-Dec-07 22:32 
Generaltttyyy Pin
royroy71-Dec-07 7:51
memberroyroy71-Dec-07 7:51 
Generalt Pin
Dhammika22-Nov-07 8:50
memberDhammika22-Nov-07 8:50 
Generaltest Pin
Dhammika22-Nov-07 8:48
memberDhammika22-Nov-07 8:48 
GeneralTeaaaaaaest Pin
CoreyTaylor30-Oct-07 8:49
memberCoreyTaylor30-Oct-07 8:49 
Generaltest [modified] Pin
sam.liu22-Oct-07 20:06
membersam.liu22-Oct-07 20:06 
GeneralControlling Sidebar from BHO Pin
Alex Shneyder9-Oct-07 18:09
memberAlex Shneyder9-Oct-07 18:09 
Generaltest Pin
jeremy@trevatt.com25-Sep-07 16:32
memberjeremy@trevatt.com25-Sep-07 16:32 
GeneralTest Pin
zeltera5-Sep-07 21:52
memberzeltera5-Sep-07 21:52 
GeneralNice Pin
zeltera5-Sep-07 21:47
memberzeltera5-Sep-07 21:47 
GeneralRe: Nice Pin
born2code24-Mar-08 16:03
memberborn2code24-Mar-08 16:03 
Questionwhy it is not working for me? Pin
Mongris8-Aug-07 21:03
memberMongris8-Aug-07 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 2 Jul 2003
Article Copyright 2003 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid