在C++里面定义
typedef struct testStruct
{
LONG Height;
LONG Width;
BYTE log;
};
__declspec(dllimport) testStruct ReturnStruct(LONG a);__declspec(dllimport) testStruct ReturnStruct(LONG a)
{
testStruct test;
test.Height = a;
test.Width = 30L;
test.log = FALSE;
return test;
}打包成DLL以后,在C#里面用DLLImport调用这个ReturnStruct方法的话,要怎么写code呢?
typedef struct testStruct
{
LONG Height;
LONG Width;
BYTE log;
};
__declspec(dllimport) testStruct ReturnStruct(LONG a);__declspec(dllimport) testStruct ReturnStruct(LONG a)
{
testStruct test;
test.Height = a;
test.Width = 30L;
test.log = FALSE;
return test;
}打包成DLL以后,在C#里面用DLLImport调用这个ReturnStruct方法的话,要怎么写code呢?
[DllImport("your.dll", CharSet = CharSet.Auto)]
public static extern testStruct ReturnStruct(uint a);
[System.Runtime.InteropServices.StructLayout
(LayoutKind.Explicit)]
public struct testStruct { [System.Runtime.InteropServices.FieldOffset(0)]
public Int32 Height;
[System.Runtime.InteropServices.FieldOffset(4)]
public Int32 Width;
[System.Runtime.InteropServices.FieldOffset(8)]
public Byte log;
}
public struct testStruct
{
long Height;
long Width;
byte log;
}
无法封送处理“parameter #1”: 无效的托管/非托管类型组合(该值类型必须与 Struct 成对出现)。
[StructLayout(LayoutKind.Sequential)]
public class testStruct
{
public Int32 Height;
public Int32 Width;
public byte log;
}这样定义就对了 然后类型匹配的问题就慢慢去研究了