???? ID: 311259 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

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

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ???? ????? Microsoft .NET Framework ????? ????????? ???????? ?? ???????? ???? ??:
  • 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?? ???????? ???????? ???????? ?? ??? ??? interior ???? ????? ??? ?? ???? ????????? ?????? ??? ???? ?? ??? ?? ???? ??? ??, ?? ???? ?? ????????? ???? ?? ???????? ???? ?? ??? ?????????? ?????? ?????? ????????? ?? ????? ???? ?? ??? ???? ??? ?????:
//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);	
				

???? 2

StringToHGlobalAnsi??? ??? ??? ??? ???????? ???????? ???????? ?? ??????? ?? ????????? ????? ??, ?? ?? ??? ????? ?? ??????? ????????? ???? ?????? (ANSI) ?????? ??? ?????? ???? ??? ?? ???? ?? allocates ?????? ??? ??? ??????:
//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. ????? ????,???? ?????? ????-????? ????, ?? ???? ???OK.
??????? ???? ?????? ?????? ??????? ???????? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????:
HTTP://msdn2.Microsoft.com/en-us/library/k8d11d4s.aspx (http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx)
?? ????? ?? ???? ???? ?? ???? ?????

???? 3

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

???? 4

????? C++ 2008 introducesmarshal_as<t></t>??? ???? ?????? ??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;
???:?? ??? ???? C++ ????? C++ .NET 2002 ?? ????? C++ .NET 2003 ?? ??? ???????? ????????? ?? ????? ???? ??????? ????? ?? ????? ?? C + +/ ????? C++ 2005 ?? ?? ??? ???????? ?? ?? ?? CLI ????????msclr???????? ??? ?? Visaul C++ 2008 ??? ???????? ???? ??? ??? ?? ??? ??????????? ?????? ????, ?? ??? ???? ?? ????? ????/clrMicrosoft Visual C++ 2008 ??? 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;
}

??????

???? ?? ??? ???-hit ????? C++ .NET Microsoft ???????? ?????? ?? ???, ????? Microsoft ??? ???? ?? ?? ????:
HTTP://support.Microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet (http://support.microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet)
????? C++ .NET ?? ???? ??? ???? ??????? ??????? ?? ??? ????? Microsoft Usenet ?????? ???? ?? ????:
Microsoft.public.dotnet.languages.VC (http://msdn.microsoft.com/newsgroups/default.aspx?query=Microsoft.public.dotnet.languages.vc+&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us)

???? ???? ???? ??:
  • 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  (http://support.microsoft.com/kb/311259/en-us/ )