vb.net中Marshal.PtrToStructure中的访问冲突异常

时间:2013-04-09 13:19:39

标签: vb.net marshalling windows-7-x64 access-violation intptr

我有下面的代码片段,它从一个在API中定义的C API获取指针。使用编组我试图获取结构数组,这是我的要求。

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Class vb_menu_dotnet
Public level As Short
Public menu_id As String
End Class
Dim current As IntPtr
Dim outArray As IntPtr
Dim manArray(100) As vb_menu_dotnet
vb_dotnet_get_menu_hierarchy(p_menu_handle, p_apl_id, outArray) //C API call
current = outArray
Dim j As Integer
For j = 1 To 100
manArray(j) = New vb_menu_dotnet()
Marshal.PtrToStructure(current, manArray(j)) //Access Violation Exception

C API的原型如下:

vb_dotnet_get_menu_hierarchy(tcodss_handle_t p_menu_handle,char* p_apl_id,vb_menu_dotnet** p_menu_array)
Structure defination :
typedef struct {
short level;
char* menu_id;
} vb_menu_dotnet;
 

当使用x86选项构建dll和上面的代码时,相同的代码段也能正常工作。 但是当使用x64选项运行时,我得到了AccessViolation Exception, Marshal.PtrToStructure(current,manArray(j))

注意:使用VS2010,framework 4.0,Windows 7 64位操作系统

0 个答案:

没有答案