Lm th? no ? chuy?n ?i t? System::String * Char * trong Visual C ++

D?ch tiu ? D?ch tiu ?
ID c?a bi: 311259 - Xem s?n ph?m m bi ny p d?ng vo.
Bi vi?t ny ? c?p ?n nh?ng i?u sau y Microsoft Khun kh? .NET l?p th vi?n khng gian tn:
  • System::runtime::InteropServices
  • Msclr::interop
Bung t?t c? | Thu g?n t?t c?

? Trang ny

Tm t?t

Bi vi?t ny th?o lu?n v? m?t s? cch ? chuy?n ?i t? System::String * char * b?ng cch s? d?ng nh?ng i?u sau y:
  • Qu?n l? Ti?n ch m? r?ng trong C++ trong Microsoft Visual C++ .NET 2002 v trong Microsoft Visual C++ net 2003
  • C + +/ CLI trong Microsoft Visual C++ 2005 v trong Microsoft Visual C++ 2008

Phng php 1

PtrToStringChars cung c?p cho b?n m?t con tr? n?i th?t ? ?i t?ng String th?c t?. N?u b?n v?t qua con tr? ny nghe m?t cu?c g?i khng ?c qu?n l? ch?c nng, b?n ph?i ?u tin pin con tr? ? ?m b?o r?ng cc ?i t?ng khng di chuy?n trong m?t thng rc khng ?ng b? qu tr?nh b? su t?p:
//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);	
				

Phng php 2

StringToHGlobalAnsi sao chp n?i dung c?a m?t ?i t?ng String ?c qu?n l? vo ?ng ngu?n g?c, v sau chuy?n ?i n vo M? qu?c gia tiu chu?n vi?n (ANSI) ?nh d?ng trn bay. Phng php ny phn b? b? nh? yu c?u b?n x? ?ng:
//using namespace System::Runtime::InteropServices;
System::String * str = S"Hello world\n";
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
Marshal::FreeHGlobal(str2);
				
Lu ? Trong Visual C++ 2005 v trong Visual C++ 2008, b?n ph?i thm ph? bi?n h? tr? th?i gian ch?y ngn ng? bin d?ch ty ch?n (/ clr:oldSyntax) ? thnh cng bin d?ch m?u m? tr?c . ? thm ph? bi?n h? tr? th?i gian ch?y ngn ng? bin d?ch ty ch?n, h?y lm theo cc b?c sau:
  1. B?m chu?t D? n, v sau nh?p vo ProjectName Thu?c tnh.

    Lu ?ProjectName l m?t gi? ch? cho tn c?a d? n.
  2. M? r?ng C?u h?nh thu?c tnh, v sau nh?p vo T?ng qut.
  3. Trong ngn ph?i, b?m vo ? ch?n H? tr? ngn ng? chung th?i gian ch?y, c? c php (/ clr:oldSyntax) trong cc Ph? bi?n th?i gian ch?y ngn ng? h? tr? thi?t l?p d? n.
  4. B?m chu?t p d?ng, v sau nh?p vo Ok.
? bi?t thm chi ti?t v? ngn ng? chung th?i gian ch?y h? tr? bin d?ch ty ch?n, gh thm Web site sau c?a Microsoft Developer Network (MSDN):
http://msdn2.Microsoft.com/en-US/Library/k8d11d4s.aspx
Cc b?c ny p d?ng cho ton b? bi vi?t.

Phng php 3

Cc l?p h?c VC7 CString c m?t nh xy d?ng m ph?i m?t m?t con tr? chu?i ?c qu?n l? v t?i CString v?i n?i dung c?a n:
//#include <atlstr.h>
System::String * str = S"Hello world\n";
CString str3(str); 
printf(str3);
				

Phng php 4

Visual C++ 2008 gi?i thi?u cc marshal_as<T> </T> nguyn soi gip ? l?p v l?p helper nguyn soi marshal_context() .
//#include <msclr/marshal.h>
//using namespace msclr::interop;
marshal_context ^ context = gcnew marshal_context();
const char* str4 = context->marshal_as<const char*>(str);
puts(str4);
delete context;
Lu ? M? ny khng bin d?ch b?ng cch s? d?ng ti?n ch m? r?ng ?c qu?n l? cho C++ trong Visual C++ .NET 2002 ho?c trong C ++ Visual Studio.net 2003. N s? d?ng m?i c/C + +/ CLI c php ? ?c gi?i thi?u trong Visual C++ 2005 v msclr khng gian tn m? m?i ?c gi?i thi?u trong Visaul C++ 2008. ? thnh cng bin d?ch m? ny, b?n ph?i s? d?ng /clr C++ bin d?ch chuy?n trong Microsoft Visual C++ 2008.

Qu?n l? Ti?n ch m? r?ng trong C++ m?u m? (Visual C++ 2002 ho?c Visual C++ 2003)

//compiler option: cl /clr  
#include <vcclr.h>
#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

int _tmain(void)
{
   	System::String * str = S"Hello world\n";

	//method 1
	const __wchar_t __pin * str1 = PtrToStringChars(str);
	wprintf(str1);	

	//method 2
	char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
	printf(str2);
	Marshal::FreeHGlobal(str2);

	//method 3
	CString str3(str); 
	wprintf(str3);

    return 0;
}
				

C + +/ CLI m?u m? (Visual C++ 2005 v Visual C++ 2008)

//compiler option: cl /clr 

#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

#if _MSC_VER > 1499 // Visual C++ 2008 only
#include <msclr/marshal.h>
using namespace msclr::interop;
#endif 

int _tmain(void)
{
		     System::String ^ str = "Hello world\n";

	     /method 1
	     pin_ptr<const wchar_t> str1 = PtrToStringChars(str);
     	wprintf(str1);	

	     //method 2
	     char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
     	printf(str2);
	     Marshal::FreeHGlobal((IntPtr)str2);

	     //method 3
	     CString str3(str); 
	     wprintf(str3);

     	//method 4
#if _MSC_VER > 1499 // Visual C++ 2008 only
	     marshal_context ^ context = gcnew marshal_context();
	     const char* str4 = context->marshal_as<const char*>(str);
     	puts(str4);
	     delete context;
#endif

	return 0;
}

Tham kh?o

Cho khc hng ?u-hit Visual C++ net Microsoft Knowledge Base Bi vi?t, h?y gh thm Web site sau c?a Microsoft:
http://support.Microsoft.com/default.aspx?xmlid=FH%3BEN-US%3Bvcnet

Thu?c tnh

ID c?a bi: 311259 - L?n xem xt sau cng: 30 Thang Nm 2013 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Visual C++ 2008 Express Edition
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
T? kha:
kbhowtomaster kbmanaged kbnewsgrouplink kbmt KB311259 KbMtvi
My d?ch
QUAN TRONG: Bai vit nay c d?ch b?ng phn mm dich thu?t cua Microsoft va co th c Cng ng Microsoft chinh sa l?i thng qua cng ngh CTF thay vi mt bin dich vin chuyn nghip. Microsoft cung c?p cc bi vi?t ?c c? bin d?ch vin v ph?n m?m d?ch thu?t th?c hi?n v c?ng ?ng ch?nh s?a l?i ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng nhi?u ngn ng? Tuy nhin, bi vi?t do my d?ch hoc thm chi cng ng chinh sa sau khng ph?i lc no c?ng hon h?o. Cc bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 311259

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com