Microsoft KB Archive/311259

= How to convert from System::String* to Char* in Visual C++ 2005 or in Visual C++ .NET =

Article ID: 311259

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q311259



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Runtime.InteropServices



SUMMARY
This article discusses several ways to convert from System::String* to char* by using managed extensions in Microsoft Visual C++ 2005 or in Microsoft Visual C++ .NET.

Method 1
PtrToStringChars gives you an interior pointer to the actual String object. If you pass this pointer to an unmanaged function call, you must first pin the pointer to ensure that the object does not move during an asynchronous garbage collection process: //#include  System::String * str = S&quot;Hello world\n&quot;; const __wchar_t __pin * str1 = PtrToStringChars(str); wprintf(str1);

Method 2
StringToHGlobalAnsi copies the contents of a managed String object into native heap, and then converts it into American National Standards Institute (ANSI) format on the fly. This method allocates the required native heap memory: //using namespace System::Runtime::InteropServices; System::String * str = S&quot;Hello world\n&quot;; char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str); printf(str2); Marshal::FreeHGlobal(str2); Note In Visual C++ 2005, you must add the common language runtime support compiler option (/clr:oldSyntax) to successfully compile the previous code sample. To add the common language runtime support compiler option, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) In the right pane, click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project settings.
 * 3) Click Apply, and then click OK.

For more information about common language runtime support compiler options, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article.

Method 3
The VC7 CString class has a constructor that takes a managed String pointer and loads the CString with its contents: //#include  System::String * str = S&quot;Hello world\n&quot;; CString str3(str); printf(str3);

Complete Sample Code
//compiler option: cl /clr using namespace System; using namespace System::Runtime::InteropServices;
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) using 

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

//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; }

