Functions such as CreateProcess have signatures taking pointers to structs. In C I would just pass NULL as a pointer for the optional parameters, instead of creating a dummy struct object on the stack and passing a pointer to the dummy.
In C#, I have declared it as (p/invoke)
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
CreateProcessFlags dwProcessCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation);
But if I try to pass null for the lpProcessAttributes argument or lpThreadAttributes argument, I get a compiler error:
Error 2 Argument 3: cannot convert from ‘<null>’ to ‘ref
Debugging.Wrappers.SECURITY_ATTRIBUTES’
How can I modify the above function signature so that I can just pass null for the SECURITY_ATTRIBUTES arguments, without this compiler error? (And also be able to pass a real struct if I want to?)
nullis only valid for Reference types in .Net. your SECURITY_ATTRIBUTES is astruct, which is aValueType. Rather than passing null, you need to pass an empty SECURITY_ATTRIBUTES structure. (just saynew SECURITY_ATTRIBUTES()) in your call.A cleaner method is to add a static Empty property to your struct, and just pass
SECURITY_ATTRIBUTES.EmptyOr better yet, rather than using P/Invoke to create a Process, check out the
System.Diagnostics.Processclass, which should probably do what you need it to.