如何获取IP/调用dnsapi.dll方法:DnsValidateServerStatus

发布于 2024-12-18 17:27:14 字数 416 浏览 0 评论 0原文

我不太熟悉非托管代码,但一直在我的 C# 应用程序中使用 dnsapi.dll 中的一些方法。有很多关于如何使用 DnsQuery 或 DnsFlushResolverCache 的示例,但 DnsValidateServerStatus 方法似乎是新的(需要 Win 7 或 Server 2008 R2)。我想在我的 C# 应用程序中使用此方法,但我似乎无法使编组和结构正常工作。该方法的文档可以在以下位置找到: http://msdn.microsoft。 com/en-us/library/windows/desktop/ee837436(v=VS.85).aspx

请帮忙!

I am not all that familiar with unmanaged code but have been using some of the methods in dnsapi.dll in my C# application. There are lots of examples on how to use DnsQuery, or DnsFlushResolverCache for example, but the method DnsValidateServerStatus seems to be new (requiring Win 7 or Server 2008 R2). I would like to use this method from my C# application but I can't seem to get the Marshaling and structures to work correctly. The documentation for this method can be found at:
http://msdn.microsoft.com/en-us/library/windows/desktop/ee837436(v=VS.85).aspx

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-12-25 17:27:14

此 Win32 API 的困难部分是 SOCKADDR 结构。在 PINVOKE.NET 上有一个 SOCKADDR 结构。以下示例基于此实现:

[DllImport("Dnsapi.dll")]
private static extern int  DnsValidateServerStatus(IntPtr sockaddr, string queryName, ref uint serverStatus);

WinsockSockAddr addr = new WinsockSockAddr(IPAddress.Parse("127.0.0.1"), 0);
uint serverStatus = 0;
int status = DnsValidateServerStatus(addr.PinnedSockAddr, "fqdn server name", ref serverStatus);

Console.Out.WriteLine(status);
Console.Out.WriteLine(serverStatus);

希望这会有所帮助。

The difficult part of this Win32 API is the SOCKADDR structure. On PINVOKE.NET there is an implementation of the SOCKADDR structure. The following example is based on this implementation:

[DllImport("Dnsapi.dll")]
private static extern int  DnsValidateServerStatus(IntPtr sockaddr, string queryName, ref uint serverStatus);

WinsockSockAddr addr = new WinsockSockAddr(IPAddress.Parse("127.0.0.1"), 0);
uint serverStatus = 0;
int status = DnsValidateServerStatus(addr.PinnedSockAddr, "fqdn server name", ref serverStatus);

Console.Out.WriteLine(status);
Console.Out.WriteLine(serverStatus);

Hope, this helps.

黯淡〆 2024-12-25 17:27:14

不确定它是否有效:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct sockaddr {

    /// u_short->unsigned short
    public ushort sa_family;

    /// char[14]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=14)]
    public byte[] sa_data;
}



    /// Return Type: DWORD->unsigned int
    ///server: PSOCKADDR->sockaddr*
    ///queryName: PCWSTR->WCHAR*
    ///serverStatus: PDWORD->DWORD*
    [System.Runtime.InteropServices.DllImportAttribute("dnsapi.dll", EntryPoint="DnsValidateServerStatus")]
public static extern  uint DnsValidateServerStatus([System.Runtime.InteropServices.InAttribute()] ref sockaddr server, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string queryName, [System.Runtime.InteropServices.OutAttribute()] out uint serverStatus) ;
}

Not sure does it work:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct sockaddr {

    /// u_short->unsigned short
    public ushort sa_family;

    /// char[14]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=14)]
    public byte[] sa_data;
}



    /// Return Type: DWORD->unsigned int
    ///server: PSOCKADDR->sockaddr*
    ///queryName: PCWSTR->WCHAR*
    ///serverStatus: PDWORD->DWORD*
    [System.Runtime.InteropServices.DllImportAttribute("dnsapi.dll", EntryPoint="DnsValidateServerStatus")]
public static extern  uint DnsValidateServerStatus([System.Runtime.InteropServices.InAttribute()] ref sockaddr server, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string queryName, [System.Runtime.InteropServices.OutAttribute()] out uint serverStatus) ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文