I am able to open a website with IE using the ShellExecute function but after doing some stuff, I need to close it. How can I do that programmatically? I tried passing the verb "close" to ShellExecute but it does not work. Thank you.
I am able to open a website with IE using the ShellExecute function but after doing some stuff, I need to close it. How can I do that programmatically? I tried passing the verb "close" to ShellExecute but it does not work. Thank you.
My suggestion is to find its process and close it..
See process and threads at msdn
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.
First try sending the IE window a WM_CLOSE message. If that doesn't work, then call TerminateProcess().
This was frustrating the hell out of me for a good long while. This is what I just came up with. I used ShellExecuteEx to retrieve extended information about the application, enumerated windows with Find/GetWindow, and closed it using WM_SYSCOMMAND and SC_CLOSE as the param. This was my "homework" as much as yours, needed to be done! Sorry if it seems like a spoiler
There are probably much much easier ways.Code:SHELLEXECUTEINFO eI; DWORD pIdExec, pTId; HWND hWnd; ZeroMemory(&eI, sizeof(eI)); eI.cbSize = sizeof(eI); eI.lpVerb = "open"; eI.lpFile = "iexplore"; eI.nShow = SW_SHOW; eI.fMask = SEE_MASK_NOCLOSEPROCESS; if(ShellExecuteEx(&eI) != FALSE) { WaitForSingleObject(eI.hProcess, 1000); hWnd = FindWindow(NULL, 0); while(hWnd != NULL) { GetWindowThreadProcessId(hWnd, &pTId); pIdExec = GetProcessId(eI.hProcess); if(pTId == pIdExec) { PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0); } hWnd = GetWindow(hWnd, GW_HWNDNEXT); } }
Tonto, thanks a lot man!
Wouldn't this have worked?
[QUOTE=bithub]Code:HWND hwnd = FindWindow("IEFrame",0); PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
First try sending the IE window a WM_CLOSE message. If that doesn't work, then call TerminateProcess().
[QUOTE]
WM_CLOSE would not have worked.
I would think that would risk closing an unwanted internet explorer window that is out of your posession. You could find all the IE windows, GetWindowThreadProcessId each, make sure it's the same, and then skip going through every top-level window. You could also use EnumWindows and the callback instead of Find/GetWindow enumeration. Whatever. Glad it was of some help anyways
>> WM_CLOSE would not have worked.
Yes, that is what I experienced too, kinda wierd.
Cool-August, that was exactly what I used
I know about the Spy++ tool that comes with Visual C++
Speaking of that, I have another (big) problem. That a look at this picture, please:
http://www.geocities.com/caduardo21/ie.JPG
How would I go about getting a handle to the highlighted "window"? Thanks.
I tried this without success
Code:#include <stdio.h> #include <windows.h> int main(void) { ShellExecute(NULL, "open", "www.google.com", NULL, NULL, SW_MAXIMIZE); HWND IEHwnd = FindWindow("IEFrame", NULL); HWND hwnd = FindWindowEx(IEHwnd, NULL, "Shell DocObject View", NULL); if (!hwnd) printf("blablabla hwnd\n"); HWND hwnd1 = FindWindowEx(IEHwnd, NULL, "Internet Explorer_Server", NULL); if (!hwnd1) printf("blablabla hwnd1\n"); return 0; }