c#调用c++写的dll并如何把结构体地址作为参数传递(急!)
请高手帮忙,
如下是c++中的结构体以及函数原型:
typedef struct _struct1
{
char Str[16];
int iScore;
}struct1;
typedef struct _struct2
{
int Score;
int Num;
struct1* pSt1;
}struct2;
int myFunction(struct2* result);
我在c#中的声明如下:
[DllImport("test.dll", EntryPoint = "myFunction", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int myFunction(IntPtr result);
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto,Size=8)]
public struct struct1
{
[FieldOffset(0)]
public byte[] Str;
[FieldOffset(4)]
public int iScore;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct struct2
{
public int Score;
public int lNum;
public IntPtr pSt1;
}
下面这段代码是c++中的实现,没有任何问题。
int retval=0;
int num=5;
struct2 testresult;
testresult.pSt1 = new struct1[num];
testresult.Score = 0;
testresult.Num = num;
retval = myFunction(&testresult);
问题是如何在c#中实现上面在c++中实现的代码?请高手帮忙。先谢了。