编组结构数组(2D 结构数组)
Marshalling an array of array of structs (2D struct array)
我有一个包含以下功能的第 3 方 DLL:
SDK_API FunctionInQuestion(char* name, myStruct table[row][column]);
我很确定这个函数会修改 myStruct table[行][列]。
我需要从 .net 调用它,这是我尝试的方式(使用的语言是 VB.NET,但如果你知道如何在 C# 中做到这一点,那不是问题,我很确定原理是一样的)
<System.Runtime.InteropServices.DllImportAttribute("dllinquestion.dll", EntryPoint:="FunctionInQuestion", CallingConvention:=Runtime.InteropServices.CallingConvention.Cdecl)> _
Public Shared Function FunctionInQuestion(ByVal name As System.Text.StringBuilder, ByRef table()() As myStruct) As Integer
End Function
myStruct C:
typedef struct
{
unsigned short int x;
unsigned short int y;
}myStruct;
我的结构.net:
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure myStruct
Public x As UShort
Public y As UShort
End Structure
我已经 Googling/searching Whosebug 几个小时了,到目前为止 'solution' 每次都尝试过,但我无法让它工作。如果你 redirect/vote 关闭问题,请你先看看另一个问题。如果它真的是二维结构数组的编组并且您确定问题包含答案,那么请务必关闭它,这样更好。
您应该将其编组为一个简单的线性数组。我更熟悉 C#,但在 VB 我认为它会 运行 像这样:
ByVal table() As myStruct
然后您需要在 .net 代码中手动执行 2D 到 1D 索引转换。
我有一个包含以下功能的第 3 方 DLL:
SDK_API FunctionInQuestion(char* name, myStruct table[row][column]);
我很确定这个函数会修改 myStruct table[行][列]。
我需要从 .net 调用它,这是我尝试的方式(使用的语言是 VB.NET,但如果你知道如何在 C# 中做到这一点,那不是问题,我很确定原理是一样的)
<System.Runtime.InteropServices.DllImportAttribute("dllinquestion.dll", EntryPoint:="FunctionInQuestion", CallingConvention:=Runtime.InteropServices.CallingConvention.Cdecl)> _
Public Shared Function FunctionInQuestion(ByVal name As System.Text.StringBuilder, ByRef table()() As myStruct) As Integer
End Function
myStruct C:
typedef struct
{
unsigned short int x;
unsigned short int y;
}myStruct;
我的结构.net:
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure myStruct
Public x As UShort
Public y As UShort
End Structure
我已经 Googling/searching Whosebug 几个小时了,到目前为止 'solution' 每次都尝试过,但我无法让它工作。如果你 redirect/vote 关闭问题,请你先看看另一个问题。如果它真的是二维结构数组的编组并且您确定问题包含答案,那么请务必关闭它,这样更好。
您应该将其编组为一个简单的线性数组。我更熟悉 C#,但在 VB 我认为它会 运行 像这样:
ByVal table() As myStruct
然后您需要在 .net 代码中手动执行 2D 到 1D 索引转换。