急!c#调用c++写的dll,如何传递一维数组作为参数

speeter 2011-02-25 10:01:17
C++dll函数如下
power(int handle,usigned char meas,unsigned long freq,unsigned short band,unsigned short sample,unsigned long num,unsigned char trigger, unsigned char type,short threshold,short start_time,short *power,unsigned long *act_freq)
这个函数里面power和actfreq是输出,power指向了一个数组,num是其大小,不知道怎么能把这个*power数组的值传到C#里。
我转换成了
[DllImportAttribute("tc_rfid.DLL")]
public static extern int power_time(int handle, Byte meas, UInt32 freq, UInt16 band, UInt16 sample, UInt32 num, Byte trigger, Byte type, System.Int16 threshold, System.Int16 start_time,ref short power,ref uint act_frequency);

调用时声明short[] power=new short[num];
power(....,ref power[0],ref act_frequency)
调用时会提示我尝试读写保护内存,求教达人!
...全文
438 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
speeter 2011-04-13
  • 打赏
  • 举报
回复
忘了结贴了,问题最后解决了,是dll版本的问题。。。我表示很无奈,大家的方法应该都好使的,我尽量散分~
speeter 2011-02-28
  • 打赏
  • 举报
回复
感觉应该是调用的问题,就是不知道该声明成什么样子,手里的dll只有函数名,也不知道具体怎么声明的
[Quote=引用 10 楼 whrspsoft3723 的回复:]

调用约定没有搞定?
[/Quote]
speeter 2011-02-28
  • 打赏
  • 举报
回复
试过直接声明成数组的,也不好用。。。找朋友写了个相同接口的函数封装成dll后,直接用数组就能调用。。。郁闷了,不知道是不是dll问题,继续求大牛啊
[Quote=引用 9 楼 chrisak 的回复:]

C# code
power(int handle,byte means,uint freq,ushort band,ushort sample,uint num,byte trigger,byte type,short threshold,short start_time,short[] power,ref uint act_freq)

既然传的就是数组干嘛要写成ref?直接声明成数组不好……
[/Quote]
xingyuebuyu 2011-02-28
  • 打赏
  • 举报
回复
[DllImportAttribute("tc_rfid.DLL",,CallingConvention = CallingConvention.Cdecl)]

可能不是StdCall的调用方式
http://www.cnblogs.com/KissKnife/archive/2006/11/04/549990.html
whrspsoft3723 2011-02-26
  • 打赏
  • 举报
回复
调用约定没有搞定?
ChrisAK 2011-02-26
  • 打赏
  • 举报
回复
power(int handle,byte means,uint freq,ushort band,ushort sample,uint num,byte trigger,byte type,short threshold,short start_time,short[] power,ref uint act_freq)

既然传的就是数组干嘛要写成ref?直接声明成数组不好嘛?
speeter 2011-02-26
  • 打赏
  • 举报
回复
照着ls的改了还是报尝试读写保护内存,继续求教啊
[Quote=引用 7 楼 xingyuebuyu 的回复:]
short[] power2=new short[num];
IntPtr power=Marshal.UnsafeAddrOfPinnedArrayElement(power2,0);

power(....,IntPtr power,ref act_frequency)
[/Quote]
happyrain2010 2011-02-25
  • 打赏
  • 举报
回复
stringbuilder
foxd 2011-02-25
  • 打赏
  • 举报
回复
同意2楼的。
zzmsyt 2011-02-25
  • 打赏
  • 举报
回复
和正常的一样power(....,ref power,ref act_frequency)
flyerwing 2011-02-25
  • 打赏
  • 举报
回复
power(....,ref power,ref act_frequency)
xingyuebuyu 2011-02-25
  • 打赏
  • 举报
回复


short[] power2=new short[num];
IntPtr power=Marshal.UnsafeAddrOfPinnedArrayElement(power2,0);

power(....,IntPtr power,ref act_frequency)
speeter 2011-02-25
  • 打赏
  • 举报
回复
能具体说下吗?
[Quote=引用 4 楼 happyrain2010 的回复:]
stringbuilder
[/Quote]
speeter 2011-02-25
  • 打赏
  • 举报
回复
2楼方法不行啊,照着改了一样会提示尝试读写保护内存。。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧