Question

I'm trying to start iexplore.exe let it run for 5 seconds and then close it again.

iexplore opens just fine however it doesn't close when I call the PostThreadMessage. Can anyone see what I'm 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!

Anyone have an idea of what I'm doing wrong? Or is there better way what to do the trick?

Was it helpful?

Solution

if you can enumerate the windows on the desktop and send a WM_CLOSE to the IE window , it might work .. you can use the spy programme to get the window class of the IE window

OTHER TIPS

What is the return value from the PostThreadMessage call? That might give a clue.

For me, this works perfect:

TerminateProcess(procinfo.hProcess, 0);

Try sending WM_CLOSE to the main (top-evel) window. That's equivalent to the normal Alt-F4 exit.

I don't have an answer specifically why PostThreadMessage didn't work. But, perhaps if you elaborate on why you want to do this, there is 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 an embedded Internet Explorer ActiveX control. You'll be also able to add a sink to detect when the web page is loaded in the ActiveX control, so that you start your 5-second counter only after the web page is loaded and displayed.

I ended up doing an enumeration of the windows (As serval of you mentioned i should do) I was inspired of http://simplesamples.info/Windows/EnumWindows.php.

Don't use PostThreadMessage(), which sends a message to a specific thread in the target process instead of the process' Windows Message Pump. Use PostMessage() or SendMessage() instead, which place the message in the target process's Windows Message Pump -- which is exactly what you want.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top