AccessViolationException我收到一個AccessViolationExcpetion通過調用Marshal.PtrToStructure(intPtr,typeof(Servent))。 任何想法我做錯了什麼?我在x64上試過這個。當調用Marshal.PtrToStructure
IntPtr intPtr = NativeMethods.GetServByName(name, "tcp");
if (intPtr != IntPtr.Zero)
{
Servent servent = (Servent)Marshal.PtrToStructure(intPtr, typeof(Servent));
result = System.Convert.ToInt32(IPAddress.NetworkToHostOrder(servent.s_port));
}
else
{
throw CreateWSAException();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct Servent
{
public string s_name;
public IntPtr s_aliases;
public short s_port;
public string s_proto;
}
這將是有益的,如果你能提供的'Servent'結構的本機定義;也就是說,如果你有權訪問它。 – kbrimington 2010-09-01 13:12:22