• 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?

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.

  • 0

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

Also what language is this in?

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

An example in C I found: http://win32c.rampantlogic.com/2008/05/how-do-i-wait-for-process-to-terminate.html

  • 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.

  • 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

This topic is now closed to further replies.
  • Posts

    • Anyway to download these versions without being on the Experimental builds?
    • Nothing is stopping you from continuing with your testing cadence. If updates are released every 2 weeks instead of 4, and you test once every 4 weeks, the exact same amount of patches will still be available for you in those 4 weeks. For example: Before 4th week - patch 1, 2, 3, 4 After 2nd week - patch 1 and 2 4th week - patch 3 and 4 Still the same amount after 4.
    • Everyone else has said it. I'm gonna say it - you don't know what you're talking about. I do. I have two laptops. One work, one personal. I have access to two more laptops - both personal. At home I manually update my personal laptop when I see on Neowin that there is an update - I carry on and only apply the updates when I am ready. My work one only updates when my workplace decides to send it - I carry on and only apply the updates (when they actually arrive, which is usually days after the release) when I switch off the laptop at the end of the day as usual. The two other personal laptops only get updated when I get to it which is rarely - the people who own them carry on using them until I get to it and update them. All of the browsers on all laptops are configured to restore the tabs when launched. Google and Microsoft have changed from 6 weeks to 4, and it looks like it's going to move to 2. None of these changes affect how any of these browsers on the laptops are used. Not one jot. My advice to you is stop panicking whenever you see an update. Just carry on with what you're doing. This even benefits you in a way - from your comment you sound like you don't like the changes or the frivolous new features - great - then carry on as before!
    • AMAZON needs to take total accountability for this.
    • Server Summit had a heap of announcements, ADCS changes are baller.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!