c#调用c++的DLL,不能正确返回值,高手帮忙解决,急!!!!

tjkevin 2009-02-19 03:12:13
有一个C++生成的dll,名称为test.dll,入口点为_datadown@12其中包含一个方法 string datadown_sp(string inputstring,string type,ref string outstring).其中,type值为固定的,TYPE="U".在C#中调用后,不能正常返回值.
正常运行的结果应该返回两个值,一个是datadown_sp.一个是outstring.但调用后,datadown_sp返回乱码,outstring返回初始定义的值.
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //使用C#导入dll必须的

namespace csharptest
{
class Program
{
// dll中导出函数的声明
[DllImport("test.dll", EntryPoint="_datadown@12",CallingConvention=CallingConvention.Winapi)]
public extern static string datadown_sp(string inputstring,string type,ref string outstring);

static void Mytest()
{
try
{
string returnstring;
string outstring="";
string savestring;
returnstring=datadown_sp(";123455=#","U",ref outstring);
savestring=outstring;
}
}
}
}
正常运行结果应该是:retrunstring="0|16",savestring="123455 #".但上面代码中returnstring返回是乱码,savestring的值返回是"".
高手们帮忙解决下,着急呀!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


[DllImport("CFDll.dll", EntryPoint = "CFISO15693_Inventory")]
public static extern int CFISO15693_Inventory(ushort wDevID, ref byte[] pData, ref byte pLength);
调用:
int cfIso15693_inventory;
byte[] dsfidUid = new byte[20];
ushort x = 0;
cfIso15693_inventory = CFISO15693_Inventory(x, ref dsfidUid, ref length);
...全文
260 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tweeger 2009-02-23
  • 打赏
  • 举报
回复
试试这个:
[DllImport("test.dll", EntryPoint = "DataDown_sp", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
public extern static string DataDown_sp(string inputstring, string type, ref string outstring);
tweeger 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tjkevin 的回复:]
就是因为没有C++的源码才郁闷,搞不清那个DLL到底返回什么类型的值,怎么也不行,只能找高手了.
[/Quote]
没有源码也该有个“xxx.h”的头文件吧?看看那个里面是怎么声明该函数的?
tjkevin 2009-02-19
  • 打赏
  • 举报
回复
就是因为没有C++的源码才郁闷,搞不清那个DLL到底返回什么类型的值,怎么也不行,只能找高手了.
soaringbird 2009-02-19
  • 打赏
  • 举报
回复
把ref string outstring换成StringBuilder outstring, 记得要先分配足够空间
tweeger 2009-02-19
  • 打赏
  • 举报
回复
请lz先给出该方法在C++中的原始声明以供参考。
tjkevin 2009-02-19
  • 打赏
  • 举报
回复
郁闷了,方法也执行了,返回的值就是不正确,返回个"㔭〰簲듎ꮴ﮷"这样的值,那个高手帮帮忙呀!!!!!!!!
EveryCase 2009-02-19
  • 打赏
  • 举报
回复
顶~~~~~~~~~~
宝_爸 2009-02-19
  • 打赏
  • 举报
回复
有C++的源代码吗?可以集成调试一下。在C#项目属性的debug页将Enable unmanaged code debuging选中。

C++的dll使用debug版的,并且将pdb文件也copy到相同目录。


returnstring=datadown_sp(";123455=#","U",ref outstring);
设置断点。按F11跟踪进去,vs会提示指定源代码的位置。指定好即可进入C++source code 跟踪。
tjkevin 2009-02-19
  • 打赏
  • 举报
回复
extern "C" DllExport LPSTR DataDown_sp(LPSTR DownLoadStr,LPSTR DownloadLSH,LPSTR Rec_Data_Sp)
这是函数原型,在c#中应该如何定义参数和返回值呀.LPstr Rec_Data_Sp应该在C#调用中返回.在请教高手帮忙了!!!!!!!!

110,534

社区成员

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

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

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