我有下面的代码片段,它从在 dll 中定义的 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;
当 dll 和以上代码都使用 x86 选项构建时,相同的代码片段有效。但是当使用 x64 选项运行时,我在 Marshal.PtrToStructure(current, manArray(j)) 处得到 AccessViolation Exception
注:使用VS2010,framework 4.0,Windows 7 64位操作系统