求解!VC.NET中如何将string转换为字符数组?

honglian 2010-11-04 12:50:46
现有一个textBoxIP1,我要将读进来的值传给zm30x_open_device函数的参数ip。如下:
String^ s = textBoxIP1->Text;
zm30x_open_device(char *ip);

C#.NET中可以这样做:
string s = textBoxIP1.Text;
char[] c = s.ToCharArray();
ZM30XDLL.zm30x_open_device(c);
但在VC.NET怎么做呢?请高手帮忙!在线等待,不甚感激!
...全文
141 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
honglian 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 summertao 的回复:]
不过,有个更方便的方法:
using namespace System::Runtime::InteropServices;
String^ str = textBox1->Text->Trim();
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
zm30x_open_device(str2);
你试试这个应该……
[/Quote]
多谢大侠了,搞定了,呵呵,以后还要多看看MSDN啊!
summertao 2010-11-04
  • 打赏
  • 举报
回复
不过,有个更方便的方法:
using namespace System::Runtime::InteropServices;
String^ str = textBox1->Text->Trim();
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
zm30x_open_device(str2);
你试试这个应该可以的
summertao 2010-11-04
  • 打赏
  • 举报
回复
不好意思是我错了.MSDN上是这样的:
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >
using namespace System;
int main() {
String ^str = "Hello";
pin_ptr<const wchar_t> wch = PtrToStringChars(str);
printf_s("%S\n", wch);
size_t convertedChars = 0;
size_t sizeInBytes = ((str->Length 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err= wcstombs_s(&convertedChars, ch, sizeInBytes, wch,sizeInBytes);
if (err != 0) printf_s("wcstombs_s failed!\n");
printf_s("%s\n", ch);
}
honglian 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 summertao 的回复:]
调用的地方这样写
zm30x_open_device((char*)*c);
[/Quote]
多谢这位大侠,这样调用编译可以通过,但是传进去的值是错的!
summertao 2010-11-04
  • 打赏
  • 举报
回复
调用的地方这样写
zm30x_open_device((char*)*c);
honglian 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 summertao 的回复:]
interior_ptr<const Char> c = PtrToStringChars(s);
[/Quote]
编译有错,cannot convert parameter 1 from 'cli::interior_ptr<Type>' to 'char *'
honglian 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q342210738 的回复:]
LZ看下这个函数:c_str()

是string的
[/Quote]
我试过了,textBoxIP1->Text返回的是System::String类型的,转化为std::string编译有错。
summertao 2010-11-04
  • 打赏
  • 举报
回复
刚才忘记说了,加上这个:#include <vcclr.h>
summertao 2010-11-04
  • 打赏
  • 举报
回复
interior_ptr<const Char> c = PtrToStringChars(s);
lyingbo 2010-11-04
  • 打赏
  • 举报
回复
LZ看下这个函数:c_str()

是string的

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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