Declare LSA_UNICODE_STRING as a class , not a struct . By doing so, you make it a reference type. This corresponds to the LSA_OBJECT_ATTRIBUTES declaration because ObjectName is of type PLSA_UNICODE_STRING , which is a pointer to a structure. You need to specify LayoutKind.Sequential when you do this, as this is not the default value for the class. After you make this change, you can set the variable to null .
[StructLayout(LayoutKind.Sequential)] class PLSA_UNICODE_STRING { public UInt16 Length; public UInt16 MaximumLength; public IntPtr Buffer; }
You can accept the same policy for LSA_OBJECT_ATTRIBUTES so that it is passed as null .
[StructLayout(LayoutKind.Sequential)] class PLSA_OBJECT_ATTRIBUTES { public uint Length; public IntPtr RootDirectory; public PLSA_UNICODE_STRING ObjectName; public uint Attributes; public IntPtr SecurityDescriptor; public IntPtr SecurityQualityOfService; } [DllImport("advapi32.dll")] static extern uint LsaOpenPolicy( PLSA_UNICODE_STRING SystemName, PLSA_OBJECT_ATTRIBUTES ObjectAttributes, uint DesiredAccess, out IntPtr PolicyHandle );
Note that the pinvoke.net ad mistakenly uses SetLastError=true in LsaOpenPolicy . This is incorrect because the error code is returned in the return value. I also removed the PreserveSig setting to true since this is the default value. Their LSA_OBJECT_ATTRIBUTES also seems incorrect, because the ObjectName parameter is of type LSA_UNICODE_STRING , which is a string, not a pointer to it.
I would advise you to be extremely skeptical about what you find on pinvoke.net. Most of the ads on this site are simply incorrect.
You are asking about the possibility of using types with a null value. According to @JaredPar, answer here , this is not an option.
source share