0

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.

mrbouffant
  • 131
  • 10

1 Answers1

0

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
  • 2,780
  • 1
  • 18
  • 23