• 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

    • they should stop making bad games that no one asked for
    • Nice rant! Expletive after expletive after expletive. Poor petal, I've touched a nerve. Would you like a tissue to wipe those tears away. Btw, nice one calling Neowin trash. Why would you stick around when you disrespect this site and the people behind it? Just go away if you feel that way.
    • Internet Download Manager (IDM) 6.43 Build 1 by Razvan Serea Internet Download Manager (IDM) is a tool to increase download speeds by up to 8 times due to its smart dynamic file segmentation technology. Unlike other download managers and accelerators, Internet Download Manager segments downloaded files dynamically during download process, and it reuses available connections without additional connect and login stages to achieve the best possible acceleration performance. Comprehensive error recovery and resume capability will restart broken or interrupted downloads due to lost connections, network problems, computer shutdowns, or unexpected power outages. All popular browsers are supported IDM integrates seamlessly into Google Chrome, FireFox, Microsoft Edge, Opera, Safari, Internet Explorer, Maxthon and all other popular browsers to automatically handle your downloads. You can also drag and drop files, or use Internet Download Manager from command line. The program supports proxy servers, ftp and http protocols, firewalls, redirects, cookies, authorization, MP3 audio and video content processing. IDM includes web site spider and grabber IDM downloads all required files that are specified with filters from web sites, for example all pictures from a web site, or subsets of web sites, or complete web sites for offline browsing. It's possible to schedule multiple grabber projects to run them once at a specified time, stop them at a specified time, or run periodically to synchronize changes. Easy downloading with one click When you click on a download link in a browser, IDM will take over the download and accelerate it. You don't need to do anything special, just browse the Internet as you usually do. IDM will catch your downloads and accelerate them. IDM supports HTTP, FTP, HTTPS and MMS protocols. Changes in Internet Download Manager 6.43 Build 1: Added the ability to download MP4 files from web sites where previously only TS videos were available. IDM displays both TS and MP4 file formats in its video download button. If you only need MP4 files, disable TS in IDM Options -> General tab -> Customize IDM Download panels in browsers -> Edit button. Remove TS extension on "Customize IDM Download panel in browsres" dialog Fixed video downloading problems on several popular web sites Fixed bugs Download: Internet Download Manager 6.43 Build 1 | 11.9 MB (Shareware) Links: Internet Download Manager Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • This is of course "clickbait" WTF? It is literally your example but tech based. A "clickbait" title is a sensationalized headline designed to manipulate readers into clicking a link using things like "fear" rather than delivering objective facts. A "clickbait" headline also usually provides little value compared to the hype generated. How does this headline not qualify? It's a generic often reused headline that is overly sensationalized. Oh no! "millions" can't use this app anymore. It has no basic facts like what f*cking app. You read the article and it's the Samsung VPN which no one cares about and there is a million free VPNs. How are you defending this ######? Headlines like this (and among other things) make me read Neowin much less than I used to in the past. It's trash...
  • Recent Achievements

    • Veteran
      branfont went up a rank
      Veteran
    • Reacting Well
      Almohandis earned a badge
      Reacting Well
    • First Post
      Cosminus earned a badge
      First Post
    • One Year In
      ThatGuyOnline earned a badge
      One Year In
    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      475
    2. 2
      +Edouard
      182
    3. 3
      PsYcHoKiLLa
      122
    4. 4
      Steven P.
      85
    5. 5
      neufuse
      73
  • Tell a friend

    Love Neowin? Tell a friend!