gpt4 book ai didi

c# - 调用 Marshal.PtrToStructure 时出现 AccessViolationException

转载 作者:可可西里 更新时间:2023-11-01 09:22:11 34 4
gpt4 key购买 nike

我通过调用 Marshal.PtrToStructure(intPtr, typeof(Servent)) 得到一个 AccessViolationExcpetion。任何想法我做错了什么?我在 x64 上试过这个。

    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 结构在 x64 上不同:

struct  servent {
char FAR * s_name; /* official service name */
char FAR * FAR * s_aliases; /* alias list */
#ifdef _WIN64
char FAR * s_proto; /* protocol to use */
short s_port; /* port # */
#else
short s_port; /* port # */
char FAR * s_proto; /* protocol to use */
#endif
};

关于c# - 调用 Marshal.PtrToStructure 时出现 AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3618154/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com