积极答复者
求助!!!关于C#调用C++DLL文件中二维指针的问题

问题
-
在C#里,我想调用一个C++DLL文件里一个函数,Dll导出函数原型如下:
extern "C" void __declspec(dllexport) generate_container(int quantity, int destination, int weight, double **container)其中的最后一个形参是要用来输出的,我想问的是,在c#声明这个函数时,最后一个参数应该如何声明?具体编程时应该怎么实现?
我还要调用另一个函数,里面的参数有四维整型指针变量,更不知道应该如何弄了.
请各位前辈帮帮忙,这东西比较急,我学C#时间不长,这东西弄了好长时间了,谢谢啦!
- 已编辑 yangsh_2008 2011年4月27日 12:00
答案
-
如果你想研究一下这个问题的话,可以把你的dll发给我。st.liaobin@gmail.com
- 已标记为答案 yangsh_2008 2011年6月28日 6:48
全部回复
-
或者这样说吧,我重新定义了一个返回一个二维指针的简单函数ReturnValue(int **p)
c++里面的代码:
extern "C" _declspec(dllexport)void ReturnValue(int **p);
void ReturnValue(int **p)
{
int a[]={5,4,3,2,1};
int b[]={5,4,3,2,1};
int c[]={5,4,3,2,1};
int d[]={5,4,3,2,1};
int e[]={1,2,3,4,5};
int *name[5]={a,b,c,d,e};
p=name;
}那么在C#里调用时,应该怎么弄才能利用返回的那个二维指针将二维数组的值输出?
-
首先你给出的这个C代码是有问题的。
- 如果你想在C代码中分配空间,那参数应该是int ***p,然后*p = name
- 如果你是在C代码外部分配空间,那函数参数是对的,但内部操作就只能类似p[i][j]这样。
以1为例解释
static extern void ReturnValue(out IntPtr p); // p为二维数组的地址 IntPtr p; ReturnValue(out p); // q[i]为一维数组的地址 int[] q = new int[5]; // 从非托管内存复制数据到托管内存中 Marshal.Copy(p,q,5); IntPtr x = new IntPtr(q[0]); // y为第1个一维数组的值 int[] y = new int[5]; Marshal.Copy(x,y,5); x = new IntPtr(q[1]); // z为第2个一维数组的值 int[] z = new int[5]; Marshal.Copy(x,z,5); 以此类推
-
非常感谢你的回答! 关于我给出来的C++代码,确实存在你所说的问题,我之前没弄清楚。至于ReturnValue函数的代码你也看到了,形参P应该是在函数代码外分配的,所以参数类型应该是int**了,我也按照你提供的那个方法试了试,但输出来的值却不是函数ReturnValue里那个数组的值,而是一堆不确定的值。 实际上,当代码执行完 ReturnValue(out p); p的值为324, 当执行完 Marshal.Copy(p,q,5); 后,数组q只有q[1]=324,其它四个元素都等于0。后来我尝试着把q声明为5维Intptr型一维数组,还是不行。问题可能出在哪儿呢? 大哥,看来你对这方面比较熟悉,我以前没接触过这方面,关于这方面,请问有什么好的资料吗?
-
如果你想研究一下这个问题的话,可以把你的dll发给我。st.liaobin@gmail.com
- 已标记为答案 yangsh_2008 2011年6月28日 6:48
-
Hello,
请问您解决了您的问题了吗? 能分享下解决方案或者经验吗?
不胜感激,谢谢先。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
问题已经解决了,你把你邮箱给我,我给你发过去.
Thanks.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
非常感谢。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
你好啊,请问您也能给我发一份C++ 2维数组 被 C# 调用的解决方案么?另外请问您是否也知道如何 传递 结构数组到C#?
我的邮箱, turbalman@gmail.com. 先谢过了哈,这份资料找了好久了。。。
期待您的帮助!
-
已经发过去了,请查收。
可惜发送不成功,你能换个邮箱地址吗?或者msn,QQ 什么的。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
你好啊,请问您也能给我发一份C++ 2维数组 被 C# 调用的解决方案么?另外请问您是否也知道如何 传递 结构数组到C#?
我的邮箱, turbalman@gmail.com. 先谢过了哈,这份资料找了好久了。。。
期待您的帮助!
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
已经发过去了。请查收。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.