tags:

views:

244

answers:

1

Hi:

Most sample code on the web for calling CreateProcessAsUser() has a similar PInvoke signature to the following:

    public static extern bool CreateProcessAsUser(IntPtr hToken,
        string lpApplicationName,
        string lpCommandLine,
        ref SECURITY_ATTRIBUTES lpProcessAttributes,
        ref SECURITY_ATTRIBUTES lpThreadAttributes,
        bool bInheritHandles,
        int creationFlags,
        IntPtr environment,
        string currentDirectory,
        ref STARTUPINFO startupInfo,
        out PROCESS_INFORMATION processInfo);

The problem is, I want to pass STARTUPINFOEX instead, which is allowed in Vista/W7. If I was writing C/C++ then I could just cast it.

How should I deal with this in C#? STARTUPINFOEX looks like this:

    [StructLayout(LayoutKind.Sequential)]
    public struct STARTUPINFOEX
    {
        public STARTUPINFO StartupInfo;
        public IntPtr lpAttributeList;
    };

Should I be passing an IntPtr to the structure instead? If I change the Pinvoke signature and do something like this...

   IntPtr pStartupInfoEx = Marshal.AllocHGlobal(Marshal.SizeOf(startupInfoEx));
   Marshal.StructureToPtr(startupInfoEx, pStartupInfoEx, true);

.. I crash my shell when I execute the CreateProcessAsUser() :(

Any ideas gratefully received. Thank you.

A: 

I think you should just pass it like the STARTUPINFO, I mean with the ref keyword. The marshaling of the structure is done the same way it is done for the STARTUPINFO.

cedrou
So do you mean declare CreateProcessAsUser with ref STARTUPINFOEX instead of ref STARTUPINFO?
mrbouffant
That's what I mean
cedrou
OK, that doesn't crash BUT the call doesn't work - returning error 183. Even if I forget about setting the lpAttributeList and using it essentially as STARTUPINFO (i.e. lpAttributeList) the call returns 183. Argh.
mrbouffant
FIXED it at last - when I declare STARTUPINFOEX I was failing to set the cb member of the embedded STARTUPINO to the value returned by Marshal.SizeOf(<a startupinfoex object>). All works now and I can reparent my processes under W7 from managed code :) Thanks for the hint about ref STARTUPINFOEX - that is exactly right.
mrbouffant