views:

892

answers:

8

Hi Im trying to start iexplore.exe let it run for 5 seconds and then close it again.

iexplore opens just fine however it doest close when i call the PostThreadMessage can any one see what im doing wrong here is the 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 im doing wrong? Or is there at better what to do the trick?

Thank you

+1  A: 

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

I hoped to avoid the enumration of windows, since i got a handle to the process and the thread.
CruelIO
A: 

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

1800 INFORMATION
The return value is 1, so everything should be OK
CruelIO
+1  A: 

For me, this works perfect:

TerminateProcess(procinfo.hProcess, 0);
schnaader
It works for me to, however i dont know how nice this method is, does it clean up?
CruelIO
No cleanup, bad idea. This will quite likely leak temporary files. That's worse than a memory leak; a persistent loss of HD capacity.
MSalters
This kind of behavior is exactly why many Windows programs suck.
John Dibling
Good point John Dibling, fortunately nobody in the Unix environment ever feels the need to do a "kill -9" huh. Oh right, they do all the time.
1800 INFORMATION
A: 

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.

Heng-Cheong Leong
Here is what i would use it for. When the user push a button in my program ie.exe opens on a specific webpage. When the user logs off my system it should close ie,exe too. Also the second time a user pushes the button, the old ie.ee should close and a new one opens.
CruelIO
A: 

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

MSalters
should i then enumerate the windows, or is there an easier way?
CruelIO
A: 

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.

CruelIO
The reason why it works is because it uses SendMessage() rather than XXXThreadMessage().
John Dibling
A: 

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.

John Dibling
A: 

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