这样做只能取到第一条数据。请指点。
extern "C" _declspec(dllexport) int __stdcall testfunc(MyPoint *pp,int nItemCount)
{
for(int i=0;i<nItemCount;i++)
{
CString sql;
cout<<pp[i].x<<", "<<pp[i].y<<endl; }
return 0;
}
c#调用。
int iItemCount = 50; MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
testfunc(p, iItemCount);
extern "C" _declspec(dllexport) int __stdcall testfunc(MyPoint *pp,int nItemCount)
{
for(int i=0;i<nItemCount;i++)
{
CString sql;
cout<<pp[i].x<<", "<<pp[i].y<<endl; }
return 0;
}
c#调用。
int iItemCount = 50; MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
testfunc(p, iItemCount);
{
for(int i=0;i<nItemCount;i++)
{
CString sql;
cout<<pp[i].x<<", "<<pp[i].y<<endl;}
return 0;
}
这段是不是有问题还是传入的时候有问题。
建议使用IntPtr作为参数引用
codetestfunc(IntPtr p, int iItemCount);
c++struct MyPoint
{
int x;
int y;
};
#include"stdio.h"void _declspec(dllexport) TestFunc(MyPoint *p,int nItemCount)
{
FILE* f = fopen("c:\\mypoint.txt","w");
for(int i=0;i<nItemCount;i++)
{
fwrite(&p[i].x,sizeof(int),1,f);
fwrite(":",sizeof(char),1,f);
fwrite(&p[i].y,sizeof(int),1,f);
}
fclose(f);
}
c# struct Mypoint
{
public int x;
public int y;
}
[DllImport(@"E:\个人文件\Source\VS\test\Test_c_Console\debug\Test_c_Dll.dll")]
private static extern void TestFunc([In, Out] Mypoint[] p, int i);
private void button1_Click(object sender, EventArgs e)
{
Mypoint[] p = new Mypoint[50];
for (int i = 0; i < 50; i++)
{
p[i].x = i;
p[i].y = i ;
}
TestFunc(p, 50); }