• 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

    • NirLauncher 1.30.24 by Razvan Serea NirLauncher is a suite of more than 200 of NirSoft's excellent portable freeware Windows utilities, and provides an interface that makes it easy to find and launch the tools you need. Which works for us - because there's something here for everyone. Have you forgotten a password stored in your browser or email client, for instance? Recovery tools here may be able to find them for you. Maybe you'd like to check your hard drive health? A disk tool will display its S.M.A.R.T. data (if the drive supports this), so you can view read/ write errors, temperature and other useful details. Is your system unstable? The System Utilities section includes several tools that can help to explain why your PC might be crashing. And there are a host of other programs on offer in categories like "Network Monitoring", "Web Browser Tools", "Video/ Audio Related Utilities", "Outlook/ Office Utilities" and more. Please note, perhaps because a few of these tools can be used maliciously (the password revealers, say), some antivirus programs will flag them as threats. We've never had a problem with any NirSoft tool, though, and you can read more on this issue at the author's site. NirLauncher is an excellent set of free tools, and a must-have for everyone's portable troubleshooting toolkit. NirLauncher Features: NirLauncher can be used from USB flash drive without need of any installation. NirLauncher and all the utilities in the package are completely freeware, without any Spyware/Adware/Malware. This package doesn't contain any 3-party software, toolbars, Web browser plugins, or other unwanted surprises. It will not install any software on your system and it will not change your Web browser homepage or other settings on your system. NirLauncher package includes variety of tools that you may need for your daily computer use, including utilities to recover lost passwords, to monitor your network, to view and extract cookies, cache, and other information stored by your Web browser, to search files in your system, and more... For every utility in the package, you can easily run it, view the help file, or jump to the Web page of the utility. When using it from USB flash drive, the configuration of every utility is saved into .cfg file on the flash drive. On x64 systems, NirLauncher automatically run the x64 version of the utility, when there is a separated x64 version. NirLauncher also allows to add more software packages in additional to the main NirSoft package. NirLauncher allows you generate plugin files for BartPE (Launcher -> Generate BartPE Plugin Files), so you can easily use the utilities of NirSoft from a bootable live windows CD. Additional packages (Piriform, SysInternals...) and instructions are available on the Nirlauncher download page. Note: This zip file below is password-protected. The password for extracting the files is nirsoft9876$ Download: NirLauncher 1.30.24 | 39.8 MB (Freeware) Link: NirLauncher Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • What people who support this position of LibreOffice do not understand is that EuroOffice is not made to appease the open source enthusiasts (I am also one) and evangelists. EuroOffice was made because some European companies wanted independence from Microsoft Office Suite, which is something installable on your computer. This move to independence was pushed by public institutions and governments in Europe, as well. Using a proprietary FORMAT as default, does not make you dependent on MS. The actual program does. A format can be changed with a simple update in the future in a dystopian world where MS would manipulate the format to lock others out. However, using MS Office proprietary format, guarantees that all the current documents used by companies, organizations, institutions, etc, will be compatible with EuroOffice and the suite will have the best chances at adoption, especially by slow moving organizations like governments and the public sector. It is as simple as that. For the same reason, even the UI is incredibly similar to MS Office. For the same reason (adoption) the choice was made to be open source. Not because EU particularly loves open source ideologically, but because it gives the best starting point to create trust in the project and amass developers and contributions to the project quickly, to catch up with proprietary projects like MS Office. I don't understand how people don't realize it.
    • How old is this tip? Seems 15-20 years old? Processor states for the CPU under Windows power options has been a thing for a long, long time. It certainly isn't new or hidden... Also, with laptops it doesn't make any difference what OS you are running, all of them are configured for battery longevity over performance, for obvious reasons. Wanted to add as well that most systems in use currently do burst as setup in the uefi bios settings, and usually when a setting is "hidden" like this in Windows it's because it's either obsolete or it is redundant--doesn't override the bios and the CPU drivers. There is a lot of crap in the registry that needs to come out...;) It's hamless and might consume 1-2kb of space in total, though.
    • I can't believe Starmer is still there...his party lost so big. He's a stubborn coot, but this is largely unenforceable, so I would imagine he'll be resigning soon. A key here is for parents to buy their kids phones sans Internet access--and set up the Internet at home, where mom and day can, you know, act like parents instead expecting the government to raise their kids.
  • 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
      512
    2. 2
      +Edouard
      205
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      ATLien_0
      91
    5. 5
      Steven P.
      85
  • Tell a friend

    Love Neowin? Tell a friend!