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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

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 <vcclr.h>
System::String * str = S"Hello world\n";
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"Hello world\n";
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 ProjectName Properties.

    Note ProjectName is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. In the right pane, click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project settings.
  4. 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: 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 <atlstr.h>
System::String * str = S"Hello world\n";
CString str3(str); 
printf(str3);
				

Complete Sample Code

//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); ntf(str3);

    return 0;
}
				

REFERENCES

For other top-hit Visual C++ .NET Microsoft Knowledge Base articles, visit the following Microsoft Web site: For more general information about Visual C++ .NET, visit the following Microsoft Usenet newsgroup:

Modification Type:MajorLast Reviewed:1/9/2006
Keywords:kbHOWTOmaster kbManaged kbNewsgroupLink KB311259 kbAudDeveloper