C# PInvoke I_NetlogonControl 2

C# PInvoke I_NetlogonControl 2,c#,pinvoke,C#,Pinvoke,我需要从c调用I_netlogoncontroll2。这是我如何做的 [DllImport("Advapi32.dll", SetLastError = true, PreserveSig = true)] public static extern uint LsaQueryInformationPolicy(SafeLsaPolicyHandle policyHandle, uint informationClass, out IntPtr buffer); [DllImport("N

我需要从c调用I_netlogoncontroll2。这是我如何做的

[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;
}
当我调用I_netlogoncontroll2时,我得到一个错误,说堆栈I不平衡,因为参数与本机代码中的参数不匹配。我不确定我到底错过了什么。任何帮助都将不胜感激

谢谢


[更新]更改后,使用上述代码段中更新的新代码,调用I_netlogoncontroll2时,我会收到AccessViolationException。不知道发生了什么事。任何指针都会有帮助。

您没有将正确数量的参数传递给。您的定义缺少QueryLevel参数

[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
 );

此外,I_netlogoncontroll2未设置最后一个错误,因此不应包含在p/Invoke定义中。

每当您询问错误时,请将整个错误消息发布到您的问题中。不要只挑几个字。谢谢汉斯·帕桑!我已经更新了参数,错误消失了。但是,当我现在调用I_netlogoncontroll2时,我得到的返回状态为5,它映射到:ERROR_ACCESS_DENIED。我需要什么权限?再次感谢![可选]并不表示它在SAL注释中的作用。我刚把它取下来。