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?

+1


source to share


8 answers


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



+2


source


What is the return value from the PostThreadMessage call? This may provide a clue.



+1


source


This works great for me:

TerminateProcess(procinfo.hProcess, 0);

      

+1


source


Try sending WM_CLOSE to the main window (top evel). This is equivalent to the normal Alt-F4 output.

+1


source


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.

0


source


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 .

0


source


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.

0


source


No, never use SendMessage () (basic win32 rule) Don't use EnmWindows () (terrible)

0


source







All Articles