C # The IE control is mainly used for BHO objects, but we know that BHO is a COM object, while. the development of com-based applications under. NET is not very simple, but controlled here. Code I have searched for the call with COM, and there is no such information in China. The following is a translation. The translation is not good. Please note.
Introduction:
When browsing Internet information, we often need to enhance the user's browsing information. IE browser is actually a scalable model that provides a large number of plug-ins to accomplish this goal. in fact, you are already using it many times, such as Google tool bar and babelfish translator. You are always using these plug-ins. These plug-ins are all band objects in BHO extensions or browsers, use these plug-ins to integrate some complex functions, specifically implementing a COM interface.
Many implementations of BHO objects use C + to compile an ATL component. If C # is used to write, that is, controlled code is used to implement COM and ATL, it seems impossible, but fortunately, net Framework provides of the interoperability with COM.ArticleI will introduce how to use. Net interoperability to implement a BHO object.
Get to know com:
I have been engaged in the development of C ++, MFC, VB, and ATL for many years. before net, I wanted to become a good com developer. Because of this, the interoperability of COM has been in my mind. Although it has been a long time, I have fully developed it.. Net Development Project, COM has become very far away, but sometimes,
For example, if I want to rewrite an ATL BHO component for C # to use it, the idea goes beyond the water.
Before learning more about BHO, I need to elaborate on some points. First, the BHO object depends on the browser's main window. In fact, this means that once a browser window is generated, a new BHO object instance will be generated. The lifecycle of any BHO object is the same as that of the browser instance. Second, BHO only exists in Internet Explorer 4.0 and later versions.
If you are using Microsoft Windows? 98, Windows 2000, Windows 95, or Windows NT version 4.0, the Active Desktop shell 4.71 is also run, and BHO is also supported by Windows Resource Manager. BHO is a COM in-process service registered in the Registry with a single click. At startup, Internet Explorer queries the key and preloads all objects under the key.
BHO objects are loaded with the display of the browser's main window, and are loaded with the destruction of the browser's main window. If you open multiple browser windows, multiple BHO instances are also generated.
No matter what command line the browser starts, BHO objects are loaded. For example, BHO objects are loaded even if you just want to see a specific HTML page or a given folder. Generally, BHO is taken into account when assumer.exe or ipolice.exe is running. If you set the "open each folder in its own window" (open in an independent window for each folder) folder option, each time you open a folder, The BHO object will be loaded.
As mentioned above, a BHO object will be loaded by Internet Explorer or Windows Resource Manager (premise: Shell version 4.71 or later. So I specifically designed a BHO to process HTML webpages, so this BHO has nothing to do with the resource manager. If a DLL does not want to be loaded together by the caller, you only need to implement in dllmain () to find out who will return false after calling the object. See the following code:
If (dwreason = dll_process_attach) {tchar pszloader [max_path]; // return the name of the caller module. The first parameter should be null. For details, see msdn. Getmodulefilename (null, pszloader, max_path); _ tcslwr (pszloader); If (_ tcsstr (pszloader, _ T ("assumer.exe") return false ;} once you know that the current process is Windows resource manager, you can exit immediately.
Note: It is dangerous to add more conditional statements! In fact, some other processes will be abandoned when they try to mount the DLL. If you do another experiment, for example, the execution file icycler.exefor Internet assumer, then the first attacker will use regsvr32.exe (ProgramUsed to automatically register objects ).
If (! _ Tcsstr (pszloader, _ T ("iexplore.exe") You cannot register the DLL again. In fact, when regsvr32.exe tries to load the DLL to activate the dllregisterserver () function, the call will be abandoned.
8. Contact the web browser
The setsite () method is exactly where the BHO object is initialized. In addition, you can execute all the tasks that are only allowed to happen once in this method. When you open a URL using Internet Explorer, you should wait for a series of events to ensure that the required documents have been fully downloaded and initialized. Only now can you access the document content through the interface exposed by the object model (if any. This means you need to obtain a series of pointers. The first is the pointer to iwebbrowser2 (which is used to generate the webbrowser object. The second pointer is related to the event. This module must be implemented as a browser event listener to receive downloads and document-related events. The following uses the ATL sensitive pointer for encapsulation:
Ccomqiptr <iwebbrowser2, & iid_iwebbrowser2> m_spwebbrowser2; ccomqiptr <iconnectionpointcontainer, & iid_iconnectionpointcontainer> m_spcpc;Source codeSee the following section:
Hresult cviewsource: setsite (iunknown * punksite) {// retrieves and stores iwebbrowser2 pointer m_spwebbrowser2 = punksite; If (m_spwebbrowser2 = NULL) return e_invalidarg; // retrieve and store the iconnectionpointercontainer pointer m_spcpc = m_spwebbrowser2; If (m_spcpc = NULL) return e_pointer; // retrieve and store the browser handle hwnd. install a keyboard hook and use retrievebrowserwindow (); // connect to the container return connect () for Receiving Event Notifications;} To Get The iwebbrowser2 interface pointer, You can query it. You can also query iconnectionpointcontainer when an event occurs. Here, setsite () retrieves the browser handle hwnd and installs a keyboard hook in the current thread. Hwnd is used to move or resize the following Internet Explorer window. The hook is used to implement the hotkey function. you can press the hotkey to display/hide the code window.
IX. Get events from Internet Explorer
when you direct a new URL, the browser needs to complete two events: download the document and prepare the host environment for it. That is to say, it must Initialize an object and make it available externally. For different document types, or to load a registered Microsoft ActiveX? The server processes the document (such as the word .doc file) or initializes some internal components to analyze the document content and generate and display the document. This is the case for HTML web pages, whose content becomes available due to the role of DHTML objects. When all documents are downloaded, The downloadcomplete event is activated. This does not mean that the object model can be used to securely manage the content of a document. In fact, the documentcomplete event only indicates that everything has ended and the document is ready (note that the documentcomplete event only arrives when you first access the URL. If you perform a refresh action, you only receive one documentcomplete event ).
to intercept events from the browser, BHO needs to connect to the browser through the iconnectionpoint interface and implement the idispatch pointer passing interface to handle various events. Now we use the iconnectionpointcontainer pointer we have obtained to call the findconnectionpoint method-it returns a pointer pointing to the connection point object (it is through this connection point object to obtain the required external interface, which is diid_dwebbrowserevent2 ). The following code shows the connection point:
Hresult cviewsource: connect (void) {hresult hr; ccomptr <iconnectionpoint> SPCP; // The receive connection point hR = m_spcpc-> findconnectionpoint (diid_dwebbrowserevent2, & SPCP); If (failed (HR) return hr; // transmits the event processor to the container. Each time an event occurs, the container activates the corresponding functions on the implemented idispatch interface. HR = SPCP-> advise (reinterpret_cast <idispatch *> (this), & m_dwcookie); Return hr;} call the advise () method of the iconnectionpoint interface, BHO tells the browser that it is very interested in the events it generates. Due to the com event processing mechanism, all these means that BHO provides the idispatch interface pointer to the browser. The browser calls back the invoke () method of the idispatch interface and uses the event id value as the first parameter:
Hresult cviewsource: invoke (dispid dispidmember, refiid riid, lcid, word wflags, dispparams * pdispparams, variant * pvarresult, interval info * p1_info, uint * puargerr) {If (dispidmember = dispid_documentcomplete) {ondocumentcomplete (); m_bdocumentcompleted = true ;}:} remember to separate the event from the browser when it is no longer needed. If you forget to do this, the BHO object will be locked, even after you close the browser window. Obviously, the best time to implement separation is when the event onquit is received.