c# 操作vc里的CString?请做过此类调用的赐教!很多高手都说只能改成char* 运用,所以我把VC里面的函数改为了char*VC 函数举例:
char* test()
{
CString x="1234567890";
char* tempchar =x.GetBuffer(x.GetLength());
return tempchar;
}C#调用
[DllImport("CANLINK.dll",
EntryPoint = "test", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern StringBuilder test(); //有人建议使用StringBuilder或者直接保持CHAR*
[DllImport("CANLINK.dll",
EntryPoint = "test", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern char* test(); //char*
C#运行 StringBuilder mystr = new StringBuilder();
mystr =CANLINK.test();
MessageBox.Show(mystr.ToString());
//编译通过,MessageBox.Show乱码。
不用STRINGBUILDER,保持char*
string mystr = new string(CANLINK.test());
MessageBox.Show(mystr.ToString());//编译通过,MessageBox.Show乱码。
以上两种方式都试了,都是乱码,如果CString x="1"; 则能正确MessageBox 显示 1。
请问VC里必须用char* 做函数返回值和参数,怎么在C#里调用这些函数
char* test()
{
CString x="1234567890";
char* tempchar =x.GetBuffer(x.GetLength());
return tempchar;
}C#调用
[DllImport("CANLINK.dll",
EntryPoint = "test", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern StringBuilder test(); //有人建议使用StringBuilder或者直接保持CHAR*
[DllImport("CANLINK.dll",
EntryPoint = "test", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern char* test(); //char*
C#运行 StringBuilder mystr = new StringBuilder();
mystr =CANLINK.test();
MessageBox.Show(mystr.ToString());
//编译通过,MessageBox.Show乱码。
不用STRINGBUILDER,保持char*
string mystr = new string(CANLINK.test());
MessageBox.Show(mystr.ToString());//编译通过,MessageBox.Show乱码。
以上两种方式都试了,都是乱码,如果CString x="1"; 则能正确MessageBox 显示 1。
请问VC里必须用char* 做函数返回值和参数,怎么在C#里调用这些函数
解决方案 »
- socket多次交互
- 怎么实现多文档窗体,在主窗体左边实现树形菜单,但子窗体不会盖住树形菜单,请问怎么实现
- 抽象类和接口能实例化吗?能定义抽象类数组和接口数组吗?
- GDI+的问题
- 如何使重构的方式封装字段时不检查其他对象?
- 非常不错的源代码:可加颜色,能够分组,还可以有CheckBox的下拉框ComboBox
- Datagrid 更新问题
- 关于自定义WEB控件
- 编程技术已经从dos发展到windows,现在又到了,net时代,那么初学者的门槛不是越来越高了吗?
- 未处理的“System.NullReferenceException”类型的异常出现在 Terminal.API.dll 中。
- 数据库连接异常“用户‘’登陆失败。该用户与可信SQL Server连接无关联”
- Gridview 2次读取的数据显示在一个Gridview上
换个问题
void test(char* mychar)
{
CString x="1234567890";
char* mychar=x.GetBuffer(x.GetLength());
return ;
}
void test(char* mychar)
{
CString x="1234567890";
mychar=x.GetBuffer(x.GetLength());
}C# 调用:
StringBuilder mystr = new StringBuilder();
CANLINK.test(mystr);
MessageBox.Show(mystr.ToString()); //运行发现mystr 为空,如何才能让VC里的参数mychar ="1234567890"
void test(char* mychar)
{
CString x="1234567890";
mychar=x.GetBuffer(x.GetLength());
这样直接复制不行,需要把x的内容复制到实参的空间里
}
我改了一下,依然不行呢?CString y;//添个实参
void test(char* mychar)
{
CString x="1234567890";
y=x;
mychar=y.GetBuffer(y.GetLength());
}
C# 调用:
StringBuilder mystr = new StringBuilder();
CANLINK.test(mystr);
MessageBox.Show(mystr.ToString());
依然为空
{
CString x="1234567890";
y=x;
mychar=y.GetBuffer(y.GetLength());
}
我的意思是用strcpy、strncpy复制内容。或者你用char**做参数,函数里*mychar=x.Get.......这样
只是在函数内部把这个变量指向了另一个char*而已,待函数退出时就没了。