System::String * ?? ???? ?? ??? ???? * ????? C++ ??? ?????? ???? ?? ??? ????

???? ?????? ???? ??????
???? ID: 311259 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? ???? ????? ???? ?? ??? ???????? ???? ?? Microsoft .NET ????????? ????? ????????? ????????:
  • System::Runtime::InteropServices
  • Msclr::interop
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? System::String * ?? ???? * ???? ?? ??? ????? ?? ????? ???? ?????? ???? ?? ??? ?? ????? ?? ????? ???? ??:
  • Microsoft Visual C++ .NET 2002 ?? Microsoft Visual C++ .NET 2003 ??? C++ ?? ??? ???????? ?????????
  • C + + / CLI Microsoft Visual C++ 2005 ?? Microsoft Visual C++ 2008 ???

???? 1

PtrToStringChars ?? ???? ?????? ???? ???????? ???????? ???????? ?? ??? ??? ??? ?? ?? ???? ??? ???? ????????? ??????? ??? ???? ?? ???, ??? ?? ???? ???? ??? ????? ???????? ??? ?????????? ?? ?????? ?? ????? ??????????? ???? ?? ?? ????????? ???? ?? ??? ?????? ?????????:
//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);	
				

???? 2

StringToHGlobalAnsi ??? ??? ??? ??? ???????? ???????? ???????? ?? ??????? ????????? ?????, ?? ?? ?????? ???? ?? ??? ??? ??????? ????????? ???? ??????????? (ANSI) ?????? ?????? ??? ?? ???? ?????? ??? ??? ?????? ?????? ????:
//using namespace System::Runtime::InteropServices;
System::String * str = S"Hello world\n";
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
Marshal::FreeHGlobal(str2);
				
??? ????? C++ 2005 ?? ????? C++ 2008 ???, ?? ??????? ???? ?????? ?????? ??????? ?????? ?? ?????? ???? (/ clr:oldSyntax) ??????????? ????? ??? ????? ?? ?????? ???? ?? ???? ??????? ???? ?????? ?????? ??????? ?????? ?????? ?? ???, ????? ????? ?? ???? ????:
  1. ????? ?????????, ?? ???? ??? ????? ???? ProjectName ???.

    ???ProjectName ????????? ?? ??? ?? ??? ??????????? ???
  2. ??????? ???? ???????????? ???, ?? ???? ??? ????? ???? ???????.
  3. ???? ??? ???, ????? ?? ??? ????? ???? ??????? ???? ?????? ??????, ?????? ???????? (/ clr:oldSyntax) ??? ??????? ???? ?????? ?????? ????????? ?????????
  4. ????? ???? ????, ?? ???? ??? ????? ???? ???.
??????? ???? ?????? ?????? ??????? ???????? ?? ???? ??? ???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????:
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
?? ????? ?? ???? ???? ???? ???? ????

???? 3

VC7 CString -???? ??? ???????? ???????? ?????? ???? ?? ?? ???????? ??, ?? CString ???? ??????? ??? ????:
//#include <atlstr.h>
System::String * str = S"Hello world\n";
CString str3(str); 
printf(str3);
				

???? 4

Visual C++ 2008 ?? ????? ???? marshal_as<T> </T> ?????? ?????? ???? ?? marshal_context() marshal ????? ???? ???
//#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;
??? ?? ??? ??? ????? C++ .NET 2002 ?? ????? C++ .NET 2003 ??? C++ ?? ??? ???????? ????????? ?? ????? ???? ??? ?????? ???? ??? ?? ??? C + ????? + / CLI ????????, ?? ????? C++ 2005 ?? ?? msclr ???????? ??? ?? Visaul C++ 2008 ??? ???????? ???? ??? ?? ??? ???? ???? ??? ??? ?? ??? ??????????? ?????? ???? ?? ???, ?? Microsoft Visual C++ 2008 ??? /clr C++ ??????? ????? ?? ????? ???? ??????

????????? ???????? C++ ????? ??? ?? ??? (????? C++ 2002 ?? ????? 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 ????? ??? (????? C++ 2005 ?? ????? 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;
}

??????

???? ????? ??? ????? C++ .NET Microsoft ????? ??? ?? ??? ????, Microsoft ?? ????? ??? ???? ?? ????:
http://support.microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet

???

???? ID: 311259 - ????? ???????: 30 ?? 2013 - ??????: 3.0
???? ???? ???? ??:
  • 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
??????: 
kbhowtomaster kbmanaged kbnewsgrouplink kbmt KB311259 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:311259

??????????? ???

 

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