提问人:Manuel 提问时间:11/10/2023 最后编辑:GSergManuel 更新时间:11/10/2023 访问量:87
指针的封送结构 - Byte 是 OK,Byte() 是 Not OK
Marshal structure to pointer - Byte is OK, Byte() is Not OK
问:
我正在尝试从结构中获取指针,将其传递给非托管DLL。
奇怪的是,如果我使用这个结构,如下:
Structure message
Public x As Byte
End Structure
并像这样构建指针:
Dim mess As message = New message With {.X = &H33}
Dim sizeOfHeader As Integer = 1
Dim pHeader As IntPtr = Marshal.AllocHGlobal(sizeOfHeader)
Marshal.StructureToPtr(mess, pHeader, True)
一切都很好 - 我可以将指针传递到我的非托管应用程序,并获取正确的信息。
但是,如果我使用此结构(使用字节数组 - 因为我需要多个字节 - 将来具有可变长度):
Structure message
Public x As Byte()
End Structure
并像这样构建指针:
Dim mess As message = New message With {.X = New Byte() {&H33}}
Dim sizeOfHeader As Integer = 1
Dim pHeader As IntPtr = Marshal.AllocHGlobal(sizeOfHeader)
Marshal.StructureToPtr(mess, pHeader, True)
失败 - 非托管应用程序接收的数据是随机的(不是0x33)
我在这里做错了什么?
编辑:
有时我还会得到一个
System.AccessViolationException:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
从函数。Marshal.StructureToPtr
此外,有时调试器仅以代码0xc0000374结束(似乎是),但没有其他信息。STATUS_HEAP_CORRUPTION
答:
-1赞
Nick Abbot
11/10/2023
#1
我认为当你使用时,你真正想要的是:Byte()
Marshal.StructureToPtr(mess.x(0), pHeader, True)
您需要结构中第一个字节的地址。
评论
MarshalAsAttribute
sizeOfHeader
1
Marshal.SizeOf
1
ByValArray
Byte
Byte()
Byte()