CreateProcessWithLogonW from service causing 0xc0000142
Posted on 2007-10-15
I have a service running under a local admin account (not system). I'm in the process of adding functionality to execute programs (typically non-interactive tools) under different user accounts. This can happily be limited to W2K and up, so I figured CreateProcessWithLogonW was the way to go.
Now to the problem: the first time the service launches an app via CreateProcessWithLogonW, it works perfectly. Any subsequent launches result in a system dlg for error 0xc0000142 (application failed to initialize properly). This on XP + SP2.
When this happens, the service itself does not detect any error and keeps running quite happily. The dlg title always relates to the tool being launched, and a quick check of taskmgr shows the tool in the process list until I dismiss the dlg that is.
One more thing - I've written my service so that it can also run as a normal console app for easier testing. When I run it like this, the launches work perfectly every time. The exact same code is executing, under the same admin account, but this time it isn't running from a service environment.
Here's how I'm calling CreateProcessWithLogonW. szUser, szPwd and szDir are valid unicode strings. cmd is actually a CByteArray (MFC) containing a unicode version of the command line. My typical test command is cmd.exe /C "c:\test.bat" (where test.bat just does something like dir > c:\out.txt). si and pi are zeroed out, then I set si.cb prior to the call.
bResult = CreateProcessWithLogonW( szUser,