How to get around DnsRecordListFree error in .NET

2019-08-19 06:24发布

I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree in the .NET Framework 4.0. I am using Windows 7. How do I get around it? Here is the error:

Faulting application name: MxRecordService.exe, version: 1.0.0.0, time stamp: 0x4ce25848
Faulting module name: ntdll.dll, version: 6.1.7600.16559, time stamp: 0x4ba9b802
Exception code: 0xc0000374
Fault offset: 0x00000000000c6df2
Faulting process id: 0x110c
Faulting application start time: 0x01cb85f46c1140da
Faulting application path: C:\vss\Web\MXRecordWS\MxRecordService\bin\Debug\MxRecordService.exe
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: e8c9ecb6-f1e8-11df-8afe-0026b9777c1c

My code is:

[DllImport("dnsapi.dll", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern UInt32 DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string hostname, UInt16 iType, UInt32 iOptions, UInt32 aipServers, ref  IntPtr ppQueryResults, UInt32 pReserved);

    [DllImport("dnsapi.dll", EntryPoint = "DnsRecordListFree", CharSet = CharSet.Ansi, SetLastError = true)]
    public static extern void DnsRecordListFree(ref IntPtr pRecordList, UInt32 iFreeType);

I have tried the post How to get around DnsRecordListFree error in .NET Framework 4.0?

[Update]: Finally, fixed it. The solution is try to make sure the win32 struct and c# struct is bit(bit size) mapping. This can be achieved by using exact c# type for win32 type.

标签: c# dllimport
1条回答
叛逆
2楼-- · 2019-08-19 07:14

Try to change the function DnsRecordListFree definition to:

  [DllImport("dnsapi.dll", EntryPoint = "DnsRecordListFree", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern void DnsRecordListFree(ref IntPtr pRecordList, UInt32 iFreeType);

Edit: I guess there is only one version of this function

查看更多
登录 后发表回答