Re: CreateProcess and CloseHandle



nowhere@xxxxxxxxxxxxxx wrote:
I just tried using one of the examples for launching and closing
applications as suggested in the FAQ.

The FAQ doesn't have anything about closing applications.

I'm using Delphi 2005 with this code:

if CreateProcess(nil,
PChar(CommandLine),
nil,
nil,
False,
0,
nil,
Dir,
StartUpInfo,
ProcInfo) then
begin
WaitForSingleObject(ProcInfo.hProcess, 30000);

It's really pointless to call that function if you ignore the return value. May as well just call Sleep(30000). And then the timeout value is just arbitrary.

// Some other processing done in here
// --

CloseHandle(ProcInfo.hThread);
CloseHandle(ProcInfo.hProcess);
end;

My CommandLine contains the application name (Internet Explorer in
this instance) and arguments (a URL), and CreateProcess launches the
IE with the specified URL.

WaitForSingleObject does what it's supposed to do.

The problem is that CloseHandle doesn't close the application.

Good! It's not supposed to. It merely tells Windows that your application is no longer interested in that process. It does not tell the OS to close the program. After all, the user might still be using that program.

And if the program is Internet Explorer, it's quite likely the user *is* still using that program.

As I'm working on a batch processing system with typically 200 URLs to
process, if I left it running I would end up with 200 instances of IE
running (if it doesn't crash my PC first).

What are you using Internet Explorer to process files for?

Does anyone have any suggestions as to why it's not working, or an
alternative way of controlling an external application?

The best way is to not involve an external program at all.

If it's processing that only Internet Explorer can do, then create an instance of the Web-browser control in your own process and control it from there. You'll probably use interfaces like IHTMLDocument, and perhaps a component like TWebBrowser.

--
Rob
.


Quantcast