473,414 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Conversion from System::String::^ to char*

Hello!

i know this may be a newbie question, but i need to convert a string from
System::String^ to char*, in the msdn page tells how, but i need to set to
/clr:oldSyntax and i dont want it because in further editions of .net this
will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?

thank you very much!

alejandro aleman
Mar 13 '06 #1
2 5002
"Alejandro Aleman" <al**************@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello!

i know this may be a newbie question, but i need to convert a string
from System::String^ to char*, in the msdn page tells how, but i need to
set to /clr:oldSyntax and i dont want it because in further editions of
.net this will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?

thank you very much!

alejandro aleman


To convert from . . to . . use .
[const] char* System::String^ String(char*)
constructor
[const] wchar_t* System::String^ String(wchar_t*)
constructor
BSTR System::String^ String(wchar_t*)
constructor
System::String^ [const] char*
Marshal::StringToCoTaskMemAnsi or Marshal::StringToHGlobalAnsi
System::String^ wchar_t*
Marshal::StringToCoTaskMemUni or Marshal::StringToHGlobalUni
System::String^ const wchar_t*
PtrToStringChars(String^)
System::String^ BSTR
Marshal::StringToBSTR

Marcus Heege
Mar 13 '06 #2
Hi Alejandro!
i know this may be a newbie question, but i need to convert a string from
System::String^ to char*, in the msdn page tells how, but i need to set to
/clr:oldSyntax and i dont want it because in further editions of .net this
will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?


For convertion char*/wchar_t* into System::String you need just the
"constructor of "System::String".

For the conversion to char*/wchar_t* you need to explicit allocate
memory and therefor I suggest to use the following method.
It seems to be overloaded, but it can handle all situations (like
exceptions and so on...):

<code>
struct StringConvA
{
char *szAnsi;
StringConvA(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::StringToHGlobalAnsi(s).ToPointe r()))
{}
~StringConvA()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};

struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String* s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::I nteropServices::Marshal::StringToHGlobalUni(s).ToP ointer()))
{}
~StringConvW()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};

#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
int _tmain()
{
String *s = S"abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}
</code>
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
4
by: LadyEve | last post by:
Hi! I've got a problem when converting a System.String into a System.Char*! Problem: Invalid Cast Operation Here is the source: Type type = Type.GetType("System.Char*"); String val = "c";...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
7
by: Jonathan Porter | last post by:
Hello All - Hoping someone might be able to point me in the right direction. I have a System::String that is part of a managed class. Within a member function, I need to convert this String to a...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
0
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error:...
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.