C# 调用DLL动态库返回值的问题,请教~~~急!!!

LVOLCANO 2013-08-26 09:55:46
c++里的函数
int GetInfo(long handle,unsigned char *tmp/*这个值要返回出来*/)
{
....................
retnum = classA->ReadData(tmp,1024);
}

int ClassA::ReadData(unsigned char* buffer/*void * buffer会报Invalid access to memory location错误*/, int limit )
{
...................
}

c#调用
[DllImport("CLib.dll", CharSet = CharSet.Ansi,EntryPoint = "GetInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetInfo(long handle,StringBuilder sb/*这里的值用ref,byte[] 都用过了,还不行*/)

请教~~~急!!!


...全文
238 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcmakebest 2013-08-27
  • 打赏
  • 举报
回复
这是DLL的标准方式,只要修改 DllImport 里的就好了,如下: CallingConvention.StdCall
LVOLCANO 2013-08-27
  • 打赏
  • 举报
回复
多谢各位兄弟,我的编译方式改为 __stdcall 就好了,不知道为啥子
xfyxq 2013-08-26
  • 打赏
  • 举报
回复
public static extern int GetInfo(int handle,StringBuilder sb)
FTD_2009 2013-08-26
  • 打赏
  • 举报
回复
public static extern int GetInfo(int handle,StringBuilder sb)你这样改成试试支持二楼一下
zjzhong 2013-08-26
  • 打赏
  • 举报
回复
[System.Runtime.InteropServices.DllImportAttribute("CLib.dll", EntryPoint="GetInfo")] public static extern int GetInfo(int handle, System.IntPtr tmp) ; string str = new string(' ', 1024); IntPtr tmp = Marshal.StringToHGlobalAnsi(str); int i = GetInfo(10, tmp); string returnStr = Marshal.PtrToStringAnsi(tmp);

110,538

社区成员

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

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

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