473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

managed string -> unmanaged string

I use the following code to convert a managed string to an unmanaged one:

_TCHAR error[255];
const char* umstring = (const char*)Marshal::StringToHGlobalAnsi
(merror).ToPointer();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));

I can't use StringToHGlobalAuto because it only ever returns the first
character.
I asked why a bit ago, and somebody said you can't use StringToHGlobalAuto
because of something I can't remember, but in any case it doesn't work so
don't say that.
But they also said I need to put my own unicode checking around that, but I
can't see what the point would be - as aren't all managed strings unicode
anyway?
This leads me to believe it must be doing whatever conversions for me I
require anyway.
Is it?
If not, what is the required unicode checking?
Nov 17 '05 #1
10 5133
=?Utf-8?B?Qm9uag==?= wrote:
I use the following code to convert a managed string to an unmanaged
one:
In general: For string conversion see:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/kb/311259/

_TCHAR error[255];
const char* umstring = (const char*)Marshal::StringToHGlobalAnsi
(merror).ToPointer();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));


This works *only* if you have an ANSI/MBCS project!
If your projects is compiled with _UNICODE/UNICODE then it will not work!

To support TCHAR you have to do:

<code>
//#include <vcclr.h>

System::String * merror = S"Hello world";
_TCHAR error[255];
#ifdef _UNICODE
const __wchar_t __pin * umstring = PtrToStringChars(merror);
#else
const char* umstring = (const char*)Marshal::StringToHGlobalAnsi
(error).ToPointer();
#endif
_tcscpy(error, umstring);
#ifndef _UNICODE
Marshal::FreeHGlobal(IntPtr((void*)umstring));
#endif
</code>

You should also check the string length!!!!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
Thanks very much for the example, it looks rockin'.
Just one question. What does "__pin" do?
"Jochen Kalmbach" wrote:
=?Utf-8?B?Qm9uag==?= wrote:
I use the following code to convert a managed string to an unmanaged
one:


In general: For string conversion see:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/kb/311259/

_TCHAR error[255];
const char* umstring = (const char*)Marshal::StringToHGlobalAnsi
(merror).ToPointer();
_tcscpy(error, umstring);
Marshal::FreeHGlobal(IntPtr((void*)umstring));


This works *only* if you have an ANSI/MBCS project!
If your projects is compiled with _UNICODE/UNICODE then it will not work!

To support TCHAR you have to do:

<code>
//#include <vcclr.h>

System::String * merror = S"Hello world";
_TCHAR error[255];
#ifdef _UNICODE
const __wchar_t __pin * umstring = PtrToStringChars(merror);
#else
const char* umstring = (const char*)Marshal::StringToHGlobalAnsi
(error).ToPointer();
#endif
_tcscpy(error, umstring);
#ifndef _UNICODE
Marshal::FreeHGlobal(IntPtr((void*)umstring));
#endif
</code>

You should also check the string length!!!!

--
Greetings
Jochen

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

Nov 17 '05 #3
> You should also check the string length!!!!

See: Convert from System::String* to TCHAR
http://blog.kalmbachnet.de/?postid=18

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #4
=?Utf-8?B?Qm9uag==?= wrote:
Thanks very much for the example, it looks rockin'.
Just one question. What does "__pin" do?


It prevents the pointer from GC.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #5
Jochen,
Thanks very much for the example, it looks rockin'.
Just one question. What does "__pin" do?


It prevents the pointer from GC.


Actually, it prevents the GC from _moving_ the object pointed to by the
__pin pointer, which is slighly different.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #6
but it won't delete it using one of its own threads though either, no?

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
Jochen,
> Thanks very much for the example, it looks rockin'.
> Just one question. What does "__pin" do?


It prevents the pointer from GC.


Actually, it prevents the GC from _moving_ the object pointed to by the
__pin pointer, which is slighly different.

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #7
Bonj,
but it won't delete it using one of its own threads though either, no?


That's a different thing altogether... it's not the presence of the pinning
pointer that prevents garbage collection, per se, but the fact that the
object is reachable through some reference to it. In this case, the pointer
you created the pinning pointer from should suffice.

In other words, we're talking two different things here: you don't usually
pin a pointer to prevent it from getting GC'd, you pin it to prevent it from
being *moved*.

Quite different, really.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #8
Right, so it doesn't matter what happens to the pointer, as long as the data
it points to is still there, but if the data gets moved, doom...
And that's what the garbage collector does isn't it.
But when the GC 'moves' a managed object legitimately, does it update the
pointer to point to the new location? Presumably this is transparent
normally in .NET but it can't happen with unmanaged pointers?

Cheers
"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
Bonj,
but it won't delete it using one of its own threads though either, no?


That's a different thing altogether... it's not the presence of the
pinning
pointer that prevents garbage collection, per se, but the fact that the
object is reachable through some reference to it. In this case, the
pointer
you created the pinning pointer from should suffice.

In other words, we're talking two different things here: you don't usually
pin a pointer to prevent it from getting GC'd, you pin it to prevent it
from
being *moved*.

Quite different, really.

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #9
Bonj,
Right, so it doesn't matter what happens to the pointer, as long as the data it points to is still there, but if the data gets moved, doom...
And that's what the garbage collector does isn't it.
It does both... move them, and remove them
But when the GC 'moves' a managed object legitimately, does it update the
pointer to point to the new location?


Only managed handles in places that are under the GC control (i.e the
managed heap, or the managed stack). It won't update pointers in places it
it can't control (like in the CRT heap, etc.)
That's the reason to have pinning pointers: ensuring the gc doesn't move the
object referenced in memory, so that the pointers you pass to it to
unmanaged code remain valid...

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #10
"Bonj" <benjtaylor at hotpop d0t com> wrote in message news:<#C**************@TK2MSFTNGP09.phx.gbl>...
Right, so it doesn't matter what happens to the pointer, as long as the data
it points to is still there, but if the data gets moved, doom...
And that's what the garbage collector does isn't it. Yes, after having deleted unreferneced objects, the GC compacts the
heap by moving all the remaining objects in contiguous space.
But when the GC 'moves' a managed object legitimately, does it update the
pointer to point to the new location? It doesn't update pointers, it updates managed references (or
handles). In managed C++, those handles are represented by "managed
pointers".
Presumably this is transparent
normally in .NET but it can't happen with unmanaged pointers?

Yes, that's the whole point of pinning pointers : temporaly prevent
the GC to move an object, so that unmanaged code can use the memory
without risk of access violation.

Arnaud
MVP - VC
Nov 17 '05 #11

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

Similar topics

0
by: Shailesh | last post by:
Metalink says that, whenever we migrate from dictionary managed to locally managed tablespaces, 'Migrated tablespaces are not subject to the UNIFORM/SYSTEM policy of newly created locally managed...
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
3
by: Dave | last post by:
I'm at a point where I would really like to focus in on learning .NET but am having a hard time deciding which language to use. I learned to program in C++ but have spent quite a bit of time using...
5
by: Gerhard Menzl | last post by:
When creating a Managed C++ DLL using the Visual Studio 7.1 Solution Explorer (by selecting Add New Project and then either choosing Class Library (.NET) or Windows Control Library (.NET)), the IDE...
3
by: Steve Marsden | last post by:
Hi I am an experienced C programmer and we have a large app written in C which I have just recompiled with /clr to start to add in managed code to use .NET framework and windows forms bit by...
5
by: Ney André de Mello Zunino | last post by:
Hello. I am developing a project in managed C++ for the first time. Things were going fine up until the point I had the need to mix managed and non-managed code. More specifically, what I am...
3
by: Adam | last post by:
I can't seem to find one spot on the net that specifies exactly what I need to do. Situation: Native dll needs to hold a static reference to a managed class in .net 2.0 (whidbey) which needs to...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
2
by: Kevin Sun | last post by:
I have a problem about non-managed code and managed code.Could you like to give me some suggestion? In a non-managed MFC application project, I add some managed codes and these files are setted...
1
by: akshaycjoshi | last post by:
I am reading CLR via C#. The author says Microsoft's C#, Visual Basic, JScript, J#, and the IL Assembler always produce modules that contain managed code (IL) and managed data...
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
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
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.