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函数,请给出详细例子代码,
非常感谢
...全文
352 20 打赏 收藏 转发到动态 举报
写回复
用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
一.从w95_s7.dll中导入PLC通讯函数的方法[DllImport] 在使用DllImport之前,必须引入InteropServices, 代码如下: using System.Runtime.InteropServices; 具体使用方法可以参考我的博客中转载的一篇文章 《C#(.net)中的DllImport用法[转] 》写的很不错,千万要注意C++数据类型到C#的对应关系,选用合适的类型。比如 char* 可以用string来转换,指针类型可以ref 或者数组。 原文地址:http://www.cnblogs.com/xumingming/archive/2008/10/10/1308248.html 二.定义结构体类型 2.1 PLC连接参数结构体 1//定义结构体[连接PLC所需参数] 2public struct PLCConnParam 3{ 4 public byte Addres; // 定义CPU的MPI/DP地址 5 //public byte SegmentId; // 保留为0 6 public byte Rack; // 定义CPU的机架号 7 public byte Slot; // 定义CPU的槽号 8} 2.2 PLC存储区域类别编号 1//定义枚举类型[PLC的存储区域编号] 2public enum PLCBlockType 3{ 4 I = 1, //Input bytes 5 Q = 2, //Output bytes 6 M = 3, //Flag bytes 7 T = 4, //Timer words 8 Z = 5, //Counter words 9 D = 6, //Data from DB 10} 三.常用函数详细讲解 3.1 建立PLC连接函数 首先从W95_S7.DLL中导入连接函数,访问权限为私有,C#将会对此函数进行封装,供外部调用,稍后讲解. 1/**//// 与PLC建立连接,该函数必须在其他所有函数调用之前被调用 2/// 3/// 连接数,在DOS,WIN3.1最多可以有4个,在WIN95以上最多可以有16个 4/// 与PLC通讯的设备名称,一般为S7ONLINE 5/// 参数列表,4个分别为MPI/DP地址,保留=0,槽号,机架号 6/// 0正常返回,非0为错误号 7[DllImport("w95_s7.dll")] 8private extern static int load_tool(byte nr, string device, byte[,] adr_table); 说明: 在一个MPI/DP网络中若有多个PLC时,可指定多个连接列。最后一列的所有参数须置0,以标志参数列结束。例如一个MPI/DP网中有两个PLC,他们的MPI地址分别为2和3,槽号均为2,机架号均为0,则可按如下方式调用:byte[,] ba={{2,0,2,0},{3,0,2,0},{0,0,0,0}}; int err=load_tool(1, "s7online",ba); 返回为int型,如果返回0则表示执行成功,非零,则需要根据错误号查找到错误具体信息,具体参照本文第五部分:错误代码字典

110,549

社区成员

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

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

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