473,396 Members | 2,029 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,396 software developers and data experts.

System::String to C-Style String

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 C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!

Jon
Nov 17 '05 #1
7 5699
Jonathan Porter wrote:
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 C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!

Jon

you can use Marshal::StringToHGlobalUni and the corresponding ansi
function.

Example:

wchar_t* chars =
reinterpret_cast<wchar_t*>((Marshal::StringToHGlob alUni(string)).ToPointer());
hth

--
Ben
http://bschwehn.de
Nov 17 '05 #2
> you can use Marshal::StringToHGlobalUni and the corresponding ansi
function.

Example:

wchar_t* chars =
reinterpret_cast<wchar_t*>((Marshal::StringToHGlob alUni(string)).ToPointer());

also, don't forget to delete the allocated memory by calling FreeHGlobal
afterwards.
Nov 17 '05 #3
Jonathan Porter wrote:
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 C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!

You may use either the Chars property, or one of the member functions
ToCharArray(), CopyTo().
Personally I would use ToCharArray() to copy to a wchar_t * or if you
are sure that you use only English characters, the Chars property to
copy in a char array, or std::string etc.

--
Ioannis Vranos
Nov 17 '05 #4
const char * to_cStr(String * str)
{
char * rStr = new char[256];
const char* c_str = (const char*)
(System::Runtime::InteropServices::Marshal::String ToHGlobalAnsi(str)).ToPointer();
strcpy(rStr,c_str);
System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr((void*)c_str));
return rStr;
}
String * from_cStr(const char *str)
{
String *rStr;
String *c_str = new System::String(str);
rStr = c_str;
return rStr;
}

Nov 17 '05 #5
Hi,

If you like to do things directly, you can also use the function defined in
vcclr.h, PtrToStringChars, as that takes a System::Strying and returns a
pointer (wide char, System::Char) to the underlying characters. You can
then perform your own memory management and conversion, if necessary, to
char*. However, if you just need wchar_t* then you can use directly the
returned pointer from PtrToStringChars and there is no overhead at all (you
just have to remember to pin and then release while you are using this).

-Eric

"Jonathan Porter" <Jo************@discussions.microsoft.com> wrote in
message news:B5**********************************@microsof t.com...
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 C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas? I've tried putting it to a STL string first, but no luck.
Thanks!

Jon

Nov 17 '05 #6
=?Utf-8?B?Sm9uYXRoYW4gUG9ydGVy?= wrote:
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 C-Style
null terminated character array so that it can be passed to a legacy C
function. Any ideas? I've tried putting it to a STL string first,
but no luck. Thanks!


There are at least tree or more different methods to do this.

See: How To Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #7
Thanks everyone for your input, I appreciate your guidance.

Thanks again!

Jon

"Jochen Kalmbach" wrote:
=?Utf-8?B?Sm9uYXRoYW4gUG9ydGVy?= wrote:
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 C-Style
null terminated character array so that it can be passed to a legacy C
function. Any ideas? I've tried putting it to a STL string first,
but no luck. Thanks!


There are at least tree or more different methods to do this.

See: How To Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #8

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
9
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
22
by: Michael Rasmussen | last post by:
Hi all, Not sure if this is OT? I have a function in a library written in C++ which returns a strdup(s.c_str()) to an application written i C. Running Valgrind on my C-application shows this...
3
by: dragonslayer008 | last post by:
I need to expose a String property, so I have: property String^ Filename; in my ref class. Now, in a member function, I need to call a native function that takes a std::wstring parameter. Of...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.