c#如何调用动态DLL参数(char*)并有返回值?

lctqzq 2011-12-12 09:04:40
有一个动态DLL调用函数如下:
参数说明:
//ch1=没有返回值
//ch2=有返加值
extern "C" __declspec(dllexport)void __stdcall Test(char *ch1,char *ch2)
{
//
}

求助******************
在C#如何调用上面的DLL函数,请给出详细例子代码,
非常感谢
...全文
382 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
wushuai1346 2011-12-12
  • 打赏
  • 举报
回复
盟拜 Sandy945 强人
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
StringBuilder
看来只能用这个了,
感谢大家帮助
阿非 2011-12-12
  • 打赏
  • 举报
回复
...

又绕回来了,取返回值用StringBuilder
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
如果使用ref string 传入的值,是乱码;
代码如下:

[DllImport("yourdll.dll")]
public static extern void Test(ref string ch1,ref string ch2);

private void TestDLL()
{
string sg1,sg2;

sg1="AAA";
sg2="BBB";

Test(ref sg1,ref sg2);

MessageBox.Show(sg2);//这里显示“BBB”不对,我的DLL返回是123456}
}




阿非 2011-12-12
  • 打赏
  • 举报
回复
string sg1,sg2;

sg1="AAA";
sg2="BBB";

Test(ref sg1,ref sg2);
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
怎么使用这个ref string


[DllImport("yourdll.dll")]
public static extern void Test(ref string ch1,ref string ch2);
//错误1与“Test(ref string ch1,ref string ch2)”最匹配的重载方法具有一些无效参数

sdl2005lyx 2011-12-12
  • 打赏
  • 举报
回复
因为string是常量字符串,如果C++:const char*

用ref string,应该也可以!
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
#12楼,的方法已经成功了,
StringBuilder必须这个才有返回值,不能用string ???
sdl2005lyx 2011-12-12
  • 打赏
  • 举报
回复
改成这样:

[DllImport("yourdll.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern void Test(string ch1,StringBuilder ch2);
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
楼8:

[DllImport("yourdll.dll")]
public static extern void Test(ref string ch1,ref string ch2);
//错误1与“Test(ref string ch1,ref string ch2)”最匹配的重载方法具有一些无效参数
阿非 2011-12-12
  • 打赏
  • 举报
回复
string sg1;
StringBuilder sg2 =new StringBuilder(512);

sg1="AAA";


Test(sg1,sg2);
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
StringBuilder这个没有用,如何用?
yfl168648 2011-12-12
  • 打赏
  • 举报
回复
[DllImport("yourdll.dll")]
public static extern void Test(ref string ch1,ref string ch2);

那就应该是这样了
阿非 2011-12-12
  • 打赏
  • 举报
回复
接受返回值用StringBuilder
lctqzq 2011-12-12
  • 打赏
  • 举报
回复

[DllImport("yourdll.dll")]
public static extern void Test(string ch1,string ch2);

private void TestDLL()
{
string sg1,sg2;

sg1="AAA";
sg2="BBB";

Test(sg1,sg2);

MessageBox.Show(sg2);//这里显示“BBB”不对,我的DLL返回是123456}

大家帮看一下,为什么我的返回值不对呢?
火星大能猫 2011-12-12
  • 打赏
  • 举报
回复
3楼完全正确
char *在.net里可以用string来代替.
yinrongg 2011-12-12
  • 打赏
  • 举报
回复
也可以试试 stringbuilder
yfl168648 2011-12-12
  • 打赏
  • 举报
回复
[DllImport("yourdll.dll")]
public static extern void Test(string ch1,string ch2);

简单写一下声明,我觉得这样就可以了
lctqzq 2011-12-12
  • 打赏
  • 举报
回复
怎么用啊?写个详细例子代码
bdmh 2011-12-12
  • 打赏
  • 举报
回复
用string

111,092

社区成员

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

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

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