473,320 Members | 1,828 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,320 software developers and data experts.

Convert a managed string to an unmanaged string

HI
In keeping with my policy of always using _TCHAR*s when possible, I am
trying to convert a System::String* to a _TCHAR*.
When I use this (my eventual aim is to get it into a pre-allocated,
unmanaged, _TCHAR* called 'error'):
const _TCHAR* umstring = (const
_TCHAR*)Marshal::StringToHGlobalAuto(merror).ToPoi nter();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));

then 'error' only receives the first character of the System::String*.

But when I do

const char* umstring = (const
char*)Marshal::StringToHGlobalAnsi(merror).ToPoint er();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));

it gets it all OK.
Why can't I do it with _TCHAR*s?
Nov 17 '05 #1
1 2769
Bonj,
In keeping with my policy of always using _TCHAR*s when possible, I am
trying to convert a System::String* to a _TCHAR*.
When I use this (my eventual aim is to get it into a pre-allocated,
unmanaged, _TCHAR* called 'error'):
const _TCHAR* umstring = (const
_TCHAR*)Marshal::StringToHGlobalAuto(merror).ToPoi nter();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));

then 'error' only receives the first character of the System::String*.

But when I do

const char* umstring = (const
char*)Marshal::StringToHGlobalAnsi(merror).ToPoint er();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));

it gets it all OK.
Why can't I do it with _TCHAR*s?


StringToHGlobalAuto() isn't like the _tcs CRT apis... it has no clue about
_UNICODE/_MBCS definitions, since it is just a real .NET API. In practice,
it is fairly useful, since whether it returns unicode or ansi strings
depends on the runtime platform (i.e. the OS), and cannot be controlled at
compile time.

Hence, you'll need to add your own #ifdef guard around and call
StringToHGlobalAnsi or StringToHGlobalUni as appropriate.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
4
by: repstat | last post by:
Hi I have a project which is going to be doing some string manipulation which needs to be pretty fast. The user interface is going to be written in C#. I am going to write the string handling...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
5
by: Michael D. Reed | last post by:
I have legacy DLL code written in C++ that is essentially C code, an implementation of a set of formulas used in our industry. There are no DB or GUI interfaces, just exposed methods. I would...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
1
by: stillh2os | last post by:
Hello. I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.