Reputation: 1
I need to invoke I_NetLogonControl2 from c#. Here I how I am doing it
[DllImport("Advapi32.dll", SetLastError = true, PreserveSig = true)]
public static extern uint LsaQueryInformationPolicy(SafeLsaPolicyHandle policyHandle, uint informationClass, out IntPtr buffer);
[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint I_NetLogonControl2(
[In, Optional] string ServerName,
[In] DWORD FunctionCode,
[In] DWORD QueryLevel,
[In] byte[] InputData,
out IntPtr QueryInformation
);
[StructLayout(LayoutKind.Sequential)]
internal struct LSA_UNICODE_STRING : IDisposable
{
public UInt16 Length;
public UInt16 MaximumLength;
public IntPtr Buffer;
}
[StructLayout(LayoutKind.Sequential)]
internal struct POLICY_PRIMARY_DOMAIN_INFO
{
public LSA_UNICODE_STRING Name;
public IntPtr Sid;
}
status = LsaQueryInformationPolicy(LsaPolicyHandle,
(uint) POLICY_INFORMATION_CLASS.PolicyPrimaryDomainInformation,
out ppPDI);
var val = (POLICY_PRIMARY_DOMAIN_INFO)Marshal.PtrToStructure(ppPDI,
typeof(POLICY_PRIMARY_DOMAIN_INFO));
Console.WriteLine("vALUE = {0}", Marshal.PtrToStringUni(val.Name.Buffer));
status = I_NetLogonControl2("", (uint)5 /* NETLOGON_CONTROL_REDISCOVER */, 2, val.Name.Buffer, out handle);
if (status != 0)
{
Console.WriteLine("I_NetLogonControl2 returned error value = {0}", status);
return false;
}
When I invoke I_NetLogonControl2 I get an error saying that the stack I unbalanced since the parameters do not match the parameters in native code. I am not sure what I am really missing here. Any help will be appreciated.
Thanks!
[UPDATE] After the changes and with the new code that is updated in the above code snippet, I am getting an AccessViolationException when I invoke the I_NetLogonControl2. Not sure what is going on. Any pointers will be helpful.
Upvotes: 0
Views: 300
Reputation: 31394
You are not passing the right number of parameters to I_NetLogonControl2. Your definition is missing the QueryLevel parameter.
[DllImport("NetApi32.dll", CharSet = CharSet.Unicode)]
public static extern uint I_NetLogonControl2(
[In] string ServerName,
[In] DWORD FunctionCode,
[In] DWORD QueryLevel,
[In] byte[] InputData,
out IntPtr QueryInformation
);
Also I_NetLogonControl2 does not set last error so that should not be included in the P/Invoke definition.
Upvotes: 2