How to join a web browser in SDK (some original part translation)

zhaozj2021-02-16  91

Transfer from lsaturn, learn to learn, don't see

Because of the work relationship, it is necessary to embed a web browser under SDK, but the program is developed by SDK, there are many articles on the Internet, but it is designed to design MFC, and later get two implementation methods under the help of netizens.

1. Yes based on ATL:

#include

CCOMMODULE _MODULE;

#include

#pragma comment (Lib, "ATL")

Int WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NSHOWCMD)

{

MSG msg;

// Initialize COM and ATL Coinitialize (NULL); atlaxwininit ();

HWND hwnd = CreateDialog (hInstance, MAKEINTRESOURCE (IDD_UNLIMITED), 0, MainDlgProc); if (! Hwnd) {MessageBox (NULL, TEXT ( "! Fail to create the dialog"), "Test", MB_ICONERROR); return 0;} ShowWindow; WHILE (GetMessage (& MSG, NULL, 0, 0)) {if (hwnd == 0 ||! Isdialogmessage (hwnd, & msg)) {TranslateMessage (& MSG); DispatchMessage (& MSG);}} Couninitialize (); return msg.wparam;}

Bool Callback MAINDLGPROC (HWND HDLG, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {

Rect rc; iWebBrowser2 * Webbrowser; variant varmyurl; static caxwindow winContainer; // This is the COM container provided by ATL

Switch (Message) {case wm_initdialog: rc.top = 8; rc.left = 8; rc.bottom = 250; rc.right = 180; WinContainer.create (HDLG, RC, LPCTSTR ("Microsoft.IExplorer.4") , WS_CHILD | WS_VISIBLE | WS_VSCROLL); // create a browser control WinContainer.QueryControl (__uuidof (IWebBrowser2), (void **) & WebBrowser); VariantInit (& varMyURL); varMyURL.vt = VT_BSTR; #ifndef UNICODE {wchar_t * buffer; DWORD size;

Size = multibytetowidechar (cp_acp, 0, "www.xxx.com", -1, 0, 0); if (! (buffer = (wchar_t *) Globalalloc (GMEM_FIXED, SIZEOF (wchar_t) * size)) Return False; MultiByteToWideChar (CP_ACP, 0, "www.xxx.com", -1, buffer, size); varMyURL.bstrVal = SysAllocString (buffer); GlobalFree (buffer);} #else varMyURL.bstrVal = SysAllocString ( "www.xxx. Com "); # endif webbrowser-> navigate2 (& varmyurl, 0, 0, 0, 0); VariantClear (& varmyurl); WebBrowser-> Release ();

Break;} // end switch (message) returnaf;

2. The following is an English translation of the article, the source is how http://www.codeguru.com/Cpp/IN/ieprogram/article.php/c4379/ using pure Win32 C in below to display a web page author: Jeff Glatt Time: December 12, 2002 Environment: Win32, VC6 (recommended but not required), IE 4.0 (or other support OLE online editing

Other browsers).

Introduction:

There are many articles on the Internet to introduce how to embed an IE browser in your own program. But examples of these articles are generally based on MFC, .NET, C # or at least WTL, because these frameworks themselves make a lot of work makes it easy to embed a browser. But if you only use pure C and Win32 (or even C ), then these examples don't use it. This article is to teach you how to embed a browser under pure C and Win32 environments, more generally, teach you how to interact with Ole / COM object below pure C and Win32 environments and create your own OLE / COM Object.

I even gave a DLL that I have compiled, as long as you are willing to deal with any Ole / COM, you just need to call its extraction function, you can display a web page unless you want to modify the source code.

Using Static, Edit, ListBox, ComboBox, etc. Windows standard control, you can get the handle of these controls, such as HWnd, can control them via SendMessage, and you can control them, and you want to pass some information or some data It will also give you a message (they will put the message in the message queue of the window, you can get these messages via getMessage).

But for the OLE / COM object is not the case. You and there are no news exchanges between them. OLE / COM object gives you some function pointers, you can control these objects through these function pointers. For example, IWebBrowser2 object gives a function pointer to make you read and display a page in your program. In turn, if the OLE / COM object wants to give you some data or notifications, you need to write some functions and provide these function pointers to the OLE / COM object, so that OLE / COM object can complete data transfer by calling these functions or Event notification. In other words, you customize an OLE / COM object in your own program. Therefore, the most troublesome in pure C is how to make the embedded OLE / COM object fully interact with your program.

In general, you call the functions in the OLE / COM object, the OLE / COM object calls you the function you provide in your program. It is like calling the DLL export function, but this DLL can also call a function of the rule in your C program (a bit similar to the callback function). However, the DLL is different, you don't need to use loadLibrary () and getProcAddress () to get a function of a COM object, but through the system function to get a pointer of an object, then through this object to get its function pointer . OLE / COM object and its vTable

Simply, a COM object is indeed a C structure that contains some function pointers that can be called by external calls. These pointers must be the first member variable of the structure, after which

It is some other data, but the function pointer must be the first member variable. This must pay attention (because we will have to create your own OLE / COM object in our C program, so

You have to learn how to declare and create a COM "structure"). In fact, this first member variable is a pointer to another structure, and the function pointer is actually stored in this structure. In essence, this secondary structure is actually

The above is an array stored in these different function pointers. We call this array as VTable. In addition, the first three function pointers in vtable must be these three specific function pointers:

QueryInterface (), addRef (), and release () (when you create your own object, you can play any name, as long as this function is in line with the rules mentioned later).

Here is the definition of these three functions:

HRESULT STDMETHODCALLTYPE QueryInterface (IUnknown FAR * This, REFIID riid, LPVOID FAR * ppvObj); HRESULT STDMETHODCALLTYPE AddRef (IUnknown FAR * This); HRESULT STDMETHODCALLTYPE Release (IUnknown FAR * This);

Let's take a look at what these parameters and HRESULT do mean. The first member variable of the OLE / COM object must be a pointer to the vTable, at least queryinterface (), addRef (), and release () three function pointers in the vTable.

(They are all related to the iUnknown interface), and the three must be as defined like the prototypes given above. Here I have the easiest example of the OLE / COM object I called "MyObject", I first defined a VTable structure called "myObject_vtbl" and then defined

"MyObject" object.

/ * This is the VTable for MYOBJECT It must start out with at * least the following three pointers * / struct MYOBJECT_VTBL {(HRESULT STDMETHODCALLTYPE * QueryInterface) (IUnknown FAR * This, REFIID riid, LPVOID FAR * ppvObj);.. (HRESULT STDMETHODCALLTYPE *) AddRef (IUnknown FAR * This); (HRESULT STDMETHODCALLTYPE *) Release (IUnknown FAR * This);. / * There would be other pointers here if this object had more * functions * /} / * This is the MYOBJECT structure * / struct myobject {/ * The first thing in the object must be a pointer * to its vTable! * / struct myObject_vtbl * lpvtbl;

/ * The Object May Have Other Embedded Objects Here, or Some * Private Data. * But All this Must Be After the Above VTable Pointer. * /}

As can be seen from the example, a COM object is always pointing to its vTable pointer, and the first three pointers of VTABLE are generally named QueryInterface, addRef, and

Release, one of the objects, which depends on the function of this object, such as a function pointer for a browser object, absolute and some function pointers that play music objects, but

It is the first member variable of all OLE / COM objects to always point to its vTable pointer, and the first three pointers of VTABLE are always queryinterface, addref, and release, this

It is rule that you can only follow.

转载请注明原文地址:https://www.9cbs.com/read-13401.html

New Post(0)