这样做只能取到第一条数据。请指点。
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); 

解决方案 »

  1.   

    不是吧,如果是一条数据,你访问pp[i]都会出错的
      

  2.   

    好像是testfunc(p, iItemCount); 这里的问题,可能传的是P的地址,也就是数组的第一个,所以只能打出第一个来
      

  3.   

    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;   
    }
     这段是不是有问题还是传入的时候有问题。
      

  4.   

    最好不要用自己定义的结构来作为dll传递的参数,改成char*试试吧。
      

  5.   

    testfunc(ref p, iItemCount);
      

  6.   

    你把C#的dllimport贴出来看看。
    建议使用IntPtr作为参数引用
    codetestfunc(IntPtr p, int iItemCount);
      

  7.   

    给你个例子
    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);        }