C # How to obtain ihtmldocument2 objects through IE handle

Source: Internet
Author: User

I checked a lot of related information on the Internet, but many of them are not usable. Let's summarize the previous experiences.

 

1. Obtain the iwebbrowser2 interface of IE and then the ihtmldocument2 interface.

 

Int iehwnd = find0000wa ("ieframe", null );
// Initialize all IE Windows
Ishellwindows Sw = new shellwindowsclass ();
// Round all IE Windows
For (INT I = Sw. Count-1; I> = 0; I --)
{
// Obtain the iwebbrowser2 object for each IE
Iwebbrowser2 iwb2 = Sw. Item (I) As iwebbrowser2;
// Check whether the obtained handle matches the window handle.
If (iwb2.hwnd = iehwnd)
{
// Assign a value when the search is successful
Document = (htmldocumentclass) iwb2.document;
// Perform operations on the webpage
Document. getelementbyid ("kW"). setattribute ("value", "it is a test", 1 );
Document. getelementbyid ("Su"). Click ();
}

}

 

2. obtain the corresponding ihtmldocument2 interface through the objectfromlresult method of accessibility. However, the handle passed in is the handle that IE shows the HTML window.

[Dllimport ("user32.dll", entrypoint = "registerwindowmessagea")]
Private Static extern int registerwindowmessage (string lpstring );

 

[Dllimport ("user32.dll", entrypoint = "sendmessagetimeouta")]
Private Static extern int sendmessagetimeout (intptr hwnd, int MSG, int wparam, int lparam, int fuflags, int utimeout, out int lpdwresult );

 

[Dllimport ("oleacc. dll")]
Private Static extern int objectfromlresult (INT lresult, ref guid riid, int wparam, ref ihtmldocument2 ppvobject );

 

Private const int smto_abortifhung = 0x2;

 

Private Static guid iid_ihtmldocument = new GUID ("626fc520-a41e-11cf-a731-00a0c9082637 ");

 

Private Static ihtmldocument2 getdocumentfromhandle (intptr success whandle)
{
Int lngmsg = 0;
Int lres;

If (! When whandle. Equals (intptr. Zero ))
{
Lngmsg = registerwindowmessage ("wm_html_getobject ");
If (lngmsg! = 0)
{
Sendmessagetimeout (when whandle, lngmsg, 0, 0, smto_abortifhung, 1000, out lres );
If (! (Bool) (lres = 0 ))
{
Int hR = objectfromlresult (lres, ref iid_ihtmldocument, 0, ref document );
If (bool) (document = NULL ))
{
Return document;
}
}
}
}

Return document;

}

Specific details can view http://www.xtremevbtalk.com/showthread.php? T = 295336

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.