How to use PostThreadMessage to close Internet Explorer from C ++
I am trying to start iexplore.exe
to start it for 5 seconds and then close it again.
iexplore
opens just fine, but doesn't close when I call PostThreadMessage. Can anyone see what I am doing wrong? Here is my code:
CString IEPath = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";//GetIEPath();
//IEPath += ' ' + url;
std::string strCommand((LPCTSTR)IEPath);
PROCESS_INFORMATION procinfo;
STARTUPINFO startupinfo;
GetStartupInfo(&startupinfo);
CreateProcess(
NULL,
(char *)strCommand.c_str(),// name of executable module
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
false, // handle inheritance option
CREATE_SHARED_WOW_VDM, // creation flags
NULL, // new environment block
NULL, // current directory name
&startupinfo, // startup information
&procinfo // process information
);
Sleep(5000);
::PostThreadMessage(procinfo.dwThreadId, WM_QUIT, 0, 0); //<---Dosent Close internet explorer!
Does anyone have any idea what I am doing wrong? Or is there a better way to do the trick?
if you can enumerate windows on the desktop and send WM_CLOSE to the IE window it might work .. you can use spyware to get the window class of the IE window
What is the return value from the PostThreadMessage call? This may provide a clue.
This works great for me:
TerminateProcess(procinfo.hProcess, 0);
Try sending WM_CLOSE to the main window (top evel). This is equivalent to the normal Alt-F4 output.
I have no answer why PostThreadMessage is not working. But perhaps if you clarify why you want to do this, is there a better solution?
For example, if you just want to show a web page for 5 seconds, you can create and show your own window with the built-in Internet Explorer ActiveX control. You will also be able to add a listener to detect when a web page is loaded in an ActiveX control so that you only start your 5 second counter after the web page is loaded and rendered.
I ended up doing the enumeration of windows (as the serval of you mentioned what I should do) I was inspired by http://simplesamples.info/Windows/EnumWindows.php .
Do not use PostThreadMessage (), which sends a message to a specific thread in the target process instead of the "Message Message Pump" process. Use PostMessage () or SendMessage () instead, which puts the message into the target Windows Message Pump process - which is exactly what you want.
No, never use SendMessage () (basic win32 rule) Don't use EnmWindows () (terrible)