C # Use Win32 API to operate IE browser-Get IE URL

Source: Internet
Author: User

Windows is a message-driven operating system, so you can use messages to operate any controls in windows. In Windows, a large number of APIs are open to users. Users can use these APIs to operate controls in Windows from the underlying layer and use messages, the following uses C # To operate IE browser through Win32 API --- to obtain ie url as an example: As for the prototype of API message function, you can find it in msdn, the following example shows how to use the C # element to represent the prototype of the API message function in C.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices; // namespace required to operate WIN32API

Namespace iemanipulation
{
Public class iemanipulation
{
[Dllimport ("user32.dll")] // user32.dll is one of the core dynamic libraries of the Windows operating system.
Static extern int findwindow (string lpclassname, string lpwindowname );
[Dllimport ("user32.dll")]
Static extern int find1_wex (INT hwndparent, int hwndchildafter, string lpszclass, string lpszwindow );
[Dllimport ("user32.dll")]
Static extern int getwindowtext (INT hwnd, stringbuilder Buf, int nmaxcount );
[Dllimport ("user32.dll")]
Static extern int sendmessage (INT hwnd, int MSG, int wparam, stringbuilder lparam );

Const int wm_gettext = 0x000d; // gets the hexadecimal representation of text messages.

/// <Summary>
/// Get the URL of the current opened IE
/// </Summary>
Public static string geturl ()
{
Int parent = findwindow ("ieframe", null );
Int child = find1_wex (parent, 0, "workerw", null );
Child = finddomainwex (child, 0, "rebarwindow32", null );
Child = find1_wex (child, 0, "comboboxex32", null );
Child = finddomainwex (child, 0, "ComboBox", null );
Child = finddomainwex (child, 0, "edit", null); // get the hierarchy of the address bar through spy ++, and then obtain
Stringbuilder buffer = new stringbuilder (1024 );

// Child indicates the handle Number of the form to be operated
// Wm_gettext indicates a message. How to drive the form?
// 1024 indicates the size of the text to be obtained
// Buffer indicates that the obtained text value is stored in the memory cache.
Int num = sendmessage (child, wm_gettext, 1024, buffer );
String url = buffer. tostring ();
Return URL;
}
}
}

Related Article

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.