Solved

how to convert from managed string to char* in c++\clr ?

Posted on 2009-04-04
3
2,105 Views
Last Modified: 2013-12-17
how to  convert from managed string to char* in c++\clr ?
here is my try below:
but it returns an error:
error C2682: cannot use 'safe_cast' to convert from ' System::String ^ '  to ' char * '

System::String^ s = "test";
	cascade_name = safe_cast<char*,System::String^>(s);
\\ Important note: cascade_name variable is a global variable which has type of char*
\\ from header file: char* cascade_name;

Open in new window

0
Comment
Question by:Ameerh24
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 350 total points
ID: 24067755
Take a look at http://support.microsoft.com/kb/311259 ("How to convert from System::String* to Char* in Visual C++ ") whichalso covers your problem (scroll down for the full code):
		     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

Open in new window

0
 

Assisted Solution

by:mayank13
mayank13 earned 150 total points
ID: 24105961
Let say we have :

string s1 = "abcd";

char *c1;

Now to convert string to char just use strcpy() having ->

strcpy( c1, s1.c_str());

Note: s1.c_str(), tell compiler to read string as char* string.

0
 

Author Closing Comment

by:Ameerh24
ID: 31566548
thanks, the solution woks fine
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not seen Link button 5 39
VB.NET - Error - Object not set in Module 4 33
Database Connections Not being returned to Connection Pool 7 25
No Data for DropDown List 2 25
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question