• 0

Process created with CreateProcess crashes. ShellExecute works.


Question

So, I've run upon a little problem here. I could easily find the answer myself, of course, but my left-click finger hurts today, so I decided to ask Neowin :p

So there's mah little code that is supposed to launch things. It also works.

s := EntryConstData[Header.Current].Executable;
p := EntryConstData[Header.Current].Parameters;
d := EntryConstData[Header.Current].Directory;
ShellExecute(0, 'open', PChar(s), PChar(p), PChar(d), SW_SHOWNORMAL);

I've decided that mah code needs to wait until the launched thing terminates, so that's why I changed the last line to this.

if Length(p) > 0 then
  s := s + ' ' + p;
ZeroMemory(@si, SizeOf(TStartupInfo));
ZeroMemory(@pi, SizeOf(TProcessInformation));
si.cb := SizeOf(TStartupInfo);
h := CreateProcess(nil, PChar(s), nil, nil, false, 0, nil, PChar(d), si, pi);
WaitForSingleObject(pi.hProcess, INFINITE);

Now, however, some of the things launch and then crash with Exception code 0xc0000005. What gives?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

While you all are, no doubt, very busy solving my problems, I'd like to further add that this:

ZeroMemory(@sei, SizeOf(TShellExecuteInfo));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.fMask := SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb := 'open';
sei.lpFile := PChar(s);
sei.lpParameters := PChar(p);
sei.lpDirectory := PChar(d);
sei.nShow := SW_SHOWNORMAL;
h := ShellExecuteEx(@sei);
WaitForSingleObject(sei.hProcess, INFINITE);

...doesn't solve it either.

Link to comment
Share on other sites

  • 0

I also don't know which language you are using, but maybe this will help. I used this tutorial long ago to accomplish the same thing you are trying, except in C++. I have since modified the function, but it still mostly resembles the original. I attached a header containing my current version of the function to this post (because Neowin's post editor screws up my formatting when I try to copy/paste it into the code block).

Edit: I just saw your tag. I guess that should have clued me to the language. My post is still just as (ir)relevant, however.

Edit 2: I'm not a Delphi programmer, and probably never will be, but I found some information that may help you. This post seems to answer a similar question to yours, and this one explains how to use the CreateProcess function in Delphi.

Link to comment
Share on other sites

  • 0

I might have worded my problem a little obscurely. My code seems to work. In *some* cases it just makes the child process to crash soon after it starts.

Perhaps you should check the result of CreateProcess / ShellExecuteEx and if there is an error check what it is using GetLastError

I do error checking both before (CreateFile with STANDARD_RIGHTS_ALL) and after - return values are all nonzero/true, target program always executes.

You seem to use "@" to provide a reference/pointer in the second case, but not the first

Those are little inconsistencies in the WinAPI translations by Borland et al. Mostly harmless. Or I think so.

Edit 2: I'm not a Delphi programmer, and probably never will be, but I found some information that may help you. This post seems to answer a similar question to yours, and this one explains how to use the CreateProcess function in Delphi.

Thanks, that's useful. The last code snippet showed me how to use MsgWaitForMultipleObjects, so that I can still process message loop while waiting around.

Now I have found that if I remove the waiting for termination part, no crashing happens, regardless of method used. :s

Link to comment
Share on other sites

This topic is now closed to further replies.