c# 中掉用DLL,参数中的uchar**该如何解决?

ghost5216 2009-03-11 09:34:22
原型为
int ABC(HANDLE hHandle,UCHAR **pHeader,DWORD *Length);

我尝试
[DllImport("XXX.dll")]
public static extern int ABC(IntPtr hHandle, byte[]pHeader, ref UInt32 Length);
这样得到的byte[]pHeader,数据不对,
使用
[DllImport("XXX.dll")]
public static extern int ABC(IntPtr hHandle, ref byte[]pHeader, ref UInt32 Length);

可这样pHeader只输出一个正确的值,实际应该返回十个

请问UCHAR ** 在C#中该如何替代?
...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2009-03-11
  • 打赏
  • 举报
回复
假设C++用LocalAlloc分配的内存,那么:


static void Main()
{
IntPtr pointer;
int i = ABC(..., out pointer, ...);

string header = Marshal.PtrToStringAnsi(pointer);
Marshal.FreeHGlobal(pointer); // free memory allocated by LocalAlloc()

Console.WriteLine(header);
Console.ReadLine();
}
[DllImport("my.dll")]
extern static int ABC(..., out IntPtr pointer, ...);
ghost5216 2009-03-11
  • 打赏
  • 举报
回复
To:zgke
用您提供的方法与
[DllImport("XXX.dll")]
public static extern int ABC(IntPtr hHandle, byte[]pHeader, ref UInt32 Length);
一样
这样得到的byte[]pHeader,数据不对,
ghost5216 2009-03-11
  • 打赏
  • 举报
回复
To :
gomoKu

用来传出一个字符串(被调用方负责分配内存),
linaren 2009-03-11
  • 打赏
  • 举报
回复
用ref string[] 试下
gomoku 2009-03-11
  • 打赏
  • 举报
回复
要先确认UCHAR **pHeader是用来做什么的。
它可以用来传出一个字符串(被调用方负责分配内存),
也可以用来传入一个字符串数组(由调用方负责初始化)。

不搞清楚,不会有正确的调用。
zgke 2009-03-11
  • 打赏
  • 举报
回复
这样看行不行



IntPtr _ByteIntPrt = System.Runtime.InteropServices.Marshal.AllocHGlobal(10);


ABC(??,ref _ByteIntPrt,ref 1);

byte[] _Temp = new byte[10];
System.Runtime.InteropServices.Marshal.Copy(_ByteIntPrt, _Temp, 0, 10);
ghost5216 2009-03-11
  • 打赏
  • 举报
回复
急啊

111,126

社区成员

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

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

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