????? ????? ?? system:: string * ??? Char * ?? Visual c + +

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

?? ??? ??????

??????

?????? ??? ??????? ??? ??? ?????? ?? system:: string * ??? char * ???????? ?? ???:
  • ?????? ????? ? c + + ?? Microsoft Visual c + +.NET 2002 ??? Microsoft Visual c + +.NET 2003
  • C + +/CLI ?? Microsoft Visual c + + 2005 ??? Microsoft Visual c + + 2008

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

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

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

??????????????????? ???? ??????? ???? ????? ???? ???? ??????? ??????? ???????? ??? ?? ????? ??? ????????? ??????? ?????? ?????? (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);
				
?????? ?? Visual c + + 2005 ? 2008 Visual c + +? ??? ????? ??????? ??? ??? ??????? ??? ?????? ??????? ??????? ?????? (/clr: oldsyntax) ????? ????? ????????? ???????? ?????? ?????. ?????? ??????? ??? ??? ??????? ??? ?????? ??????? ??????? ??????? ???? ??????? ???????:
  1. ???? ??? ???????? ?? ???? ??? ??? ??????? ?????.

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

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

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

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

???? visual c + + 2008 marshal_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 + + ?? Visual c + +.NET 2002 ?? ?? Visual c + +.NET 2003. ?????? ?? ???? C + +/CLI ???? ?????? ???? ?? ??????? ?? Visual c + + 2005 ?????????? ???????? ????? ????? ????? ??????? ???? ?? ??????? ?? 2008 c + + ??????. ?????? ??? ???????? ???????? ?????? ??? ??????? ??? ??????? ?????? ??????? ??????? /clr c + + ?? Microsoft Visual c + + 2008.

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

?????

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

???????

???? ???????: 311259 - ????? ??? ??????: 19/???/1434 - ??????: 6.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 KbMtar
????? ????
???: ??? ????? ??? ??????? ???????? ????? ?????????? ??????? ?????? ??? ??????? ?? ??? ??????? ?? ??????? ??? ??? ?? ???? ????????? ???? ??????? ???????(CTF) ?? ?? ???? ????? ????. ???? ?? ???? ?????????? ??? ???????? ???????? ???? ??? ?? ???????? ??????? ?? ??? ??????? ???????? ?? ??? ??????? ??????? ??? ?????? ?? ?????? ??? ???? ???????? ???????? ?? ????? ??????? ????? ????? ??????. ??? ?? ???????? ???????? ?? ????? ??? ????? ?? ???????? ?? ???? ????? ?? ?????. ?????? ??? ???? ?????????? ???? ?????? ?? ??? ????? ?? ??? ??? ?? ??????? ?? ??? ????? ?? ???? ????? ????? ?? ????? ??????? ???? ?? ???????? ?? ??? ???????.
???? ??? ????? ??????? ?????? ??????????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