问题
I'm currently writing a program in AutoIt but I want to outsource some functions to C++ because its more secure. I managed it to create an IE and navigate to a site.
But thats not what I need. I'm creating an instance of the IE in AutoIt and the part written in C++ has to connect to this IE and navigate to a specific page.
Right now its an C++ Application but later on, when everything runs fine, I will create a .Dll out of it and call it with AutoIt. So I can pass values like hWnd and so on.
I want to do this with good cause so please do not ask me why I am doing this.
Does anybody know how I can manage it? Thanks in advance.
CoInitialize(NULL);
IWebBrowser2* pBrowser = NULL;
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer, NULL,
CLSCTX_SERVER, IID_IWebBrowser2, (LPVOID*)&pBrowser);
if (SUCCEEDED(hr) && (pBrowser != NULL))
{
VARIANT vEmpty;
VariantInit(&vEmpty);
VARIANT vFlags;
V_VT(&vFlags) = VT_I4;
V_I4(&vFlags) = navOpenInNewWindow;
BSTR bstrURL = SysAllocString(L"http://www.ard.de");
pBrowser->Navigate(bstrURL, &vFlags, &vEmpty, &vEmpty, &vEmpty);
pBrowser->Quit();
SysFreeString(bstrURL);
}
if (pBrowser)
pBrowser->Release();
CoUninitialize();
return 0;
回答1:
The code that you posted creates a new IE (tab) instance, which is not what you want. You want to connect to an existing instance and for that you have to use a different approach.
First you create a IShellWindows instance. This object is an interface to a registry of shell windows (e.g. Windows Explorer or IE).
You can do two things with this: you can subscribe to its events and be notified as the user opens and closes windows; You can also enumerate the current windows.
You enumerate the current windows using get_Count() and Item() methods of IShellWindows. This enumeration will give you IDispatch interfaces that you then can QueryInterface() to IWebBrowser2 interface (skip if it fails, not IE). Note that you might still get windows that are not IE, so more filtering might be required.
From here you can try to find out if it's the window you are looking for.
回答2:
The current window is at the top of z order in all shell windows, so you can enumerate shell windows and pick the top one. Note the shellwindows lists Windows Explorer window as well, so need to filter out those windows by either the class name or the executable name.
Reference:
Automate the Active Windows Explorer or Internet Explorer Window
来源:https://stackoverflow.com/questions/14464002/connect-to-running-ie-instance-c