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

How to pass CString from unmanaged code to managed code?

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 .Net functionality, i.e. calling methods in managed code.
How do I pass CString variables for input and output usage, i.e. providing
and retrieving char arrays?

Regards,
Klaus
Nov 17 '05 #1
7 8201
i've got the same problem, please help
--
kind regards, muechel
Nov 17 '05 #2
For unmanaged to managed, I was under the impression that MC++ would
automatically marshall char * to System.String. I suppose you might need to
use .c_str() to get that. "IJW", as far as I can remember.

For going managed to unmanaged, you need to allocate space for the string,
you ccould try a function like this:

inline CString ToStr( System::String *strParam )
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
CString str = static_cast<LPCTSTR>( const_cast<void*>(static_cast<const
void*>( ptr ) ) );
Marshal::FreeHGlobal( ptr );
return str;
}
"Müchel" <Mu*****@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
i've got the same problem, please help
--
kind regards, muechel

Nov 17 '05 #3

"Stu Smith" <st*****@remove.digita.com> schrieb im Newsbeitrag
news:uo*************@tk2msftngp13.phx.gbl...
For unmanaged to managed, I was under the impression that MC++ would
automatically marshall char * to System.String. I suppose you might need to use .c_str() to get that. "IJW", as far as I can remember.

For going managed to unmanaged, you need to allocate space for the string,
you ccould try a function like this:

inline CString ToStr( System::String *strParam )
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
CString str = static_cast<LPCTSTR>( const_cast<void*>(static_cast<const void*>( ptr ) ) );
Marshal::FreeHGlobal( ptr );
return str;
}


It seems that this example just copies characters, i.e. using parameter
passing by reference means copying the data twice?

Regards,
Klaus
Nov 17 '05 #4
Yes, to go from managed to unmanaged you have to copy the string; you
certainly can't get a pointer to managed memory as the object might (will)
move. The only safe thing you can do is copy the string.

As for copying the data twice, well System::String doesn't specify how it
stores the characters (ie it can store them internally how it likes). You've
got two real options for getting a sequence of chars out; one is to use the
ToCharArray method (probably involves copying) and the other is to use one
of the marshall methods.

If you got the managed char array you could pin it and then copy it to a
CString, but that's two copies. If you use Marshall you can get an unmanaged
string but if you want it wrapped as a CString you have to do another copy.

I guess Microsoft could write a single-copy marshall routine that goes
straight to a CString if they wanted to.

If anyone does know a better method of marshalling strings from managed to
unmanaged I'd be happy to be enlightened as we use the double-copy code
below.
"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...

"Stu Smith" <st*****@remove.digita.com> schrieb im Newsbeitrag
news:uo*************@tk2msftngp13.phx.gbl...
For unmanaged to managed, I was under the impression that MC++ would
automatically marshall char * to System.String. I suppose you might need

to
use .c_str() to get that. "IJW", as far as I can remember.

For going managed to unmanaged, you need to allocate space for the string, you ccould try a function like this:

inline CString ToStr( System::String *strParam )
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
CString str = static_cast<LPCTSTR>(

const_cast<void*>(static_cast<const
void*>( ptr ) ) );
Marshal::FreeHGlobal( ptr );
return str;
}


It seems that this example just copies characters, i.e. using parameter
passing by reference means copying the data twice?

Regards,
Klaus

Nov 17 '05 #5
Pinning and directly copying/converting to Ansi from the internal bugger of
a System::String results in only one copy. You can do this now.

E.g. See the section called "Converting Managed Strings to Character Arrays"
in
http://www.msdnaa.net/Resources/Display.aspx?ResID=1002

Ronald Laeremans
Visual C++ team

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
Yes, to go from managed to unmanaged you have to copy the string; you
certainly can't get a pointer to managed memory as the object might (will)
move. The only safe thing you can do is copy the string.

As for copying the data twice, well System::String doesn't specify how it
stores the characters (ie it can store them internally how it likes). You've got two real options for getting a sequence of chars out; one is to use the ToCharArray method (probably involves copying) and the other is to use one
of the marshall methods.

If you got the managed char array you could pin it and then copy it to a
CString, but that's two copies. If you use Marshall you can get an unmanaged string but if you want it wrapped as a CString you have to do another copy.
I guess Microsoft could write a single-copy marshall routine that goes
straight to a CString if they wanted to.

If anyone does know a better method of marshalling strings from managed to
unmanaged I'd be happy to be enlightened as we use the double-copy code
below.
"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...

"Stu Smith" <st*****@remove.digita.com> schrieb im Newsbeitrag
news:uo*************@tk2msftngp13.phx.gbl...
For unmanaged to managed, I was under the impression that MC++ would
automatically marshall char * to System.String. I suppose you might
need
to
use .c_str() to get that. "IJW", as far as I can remember.

For going managed to unmanaged, you need to allocate space for the

string, you ccould try a function like this:

inline CString ToStr( System::String *strParam )
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
CString str = static_cast<LPCTSTR>(

const_cast<void*>(static_cast<const
void*>( ptr ) ) );
Marshal::FreeHGlobal( ptr );
return str;
}


It seems that this example just copies characters, i.e. using parameter
passing by reference means copying the data twice?

Regards,
Klaus


Nov 17 '05 #6
Well I am now enlightened. I didn't know you could access the internal
'bugger' but you learn something everyday.

I'll have an experiment with that once we're out of code-freeze.

Thanks.
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:Od*************@TK2MSFTNGP11.phx.gbl...
Pinning and directly copying/converting to Ansi from the internal bugger of a System::String results in only one copy. You can do this now.

E.g. See the section called "Converting Managed Strings to Character Arrays" in
http://www.msdnaa.net/Resources/Display.aspx?ResID=1002

Ronald Laeremans
Visual C++ team

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
Yes, to go from managed to unmanaged you have to copy the string; you
certainly can't get a pointer to managed memory as the object might (will)
move. The only safe thing you can do is copy the string.

As for copying the data twice, well System::String doesn't specify how it stores the characters (ie it can store them internally how it likes).

You've
got two real options for getting a sequence of chars out; one is to use

the
ToCharArray method (probably involves copying) and the other is to use one of the marshall methods.

If you got the managed char array you could pin it and then copy it to a
CString, but that's two copies. If you use Marshall you can get an

unmanaged
string but if you want it wrapped as a CString you have to do another

copy.

I guess Microsoft could write a single-copy marshall routine that goes
straight to a CString if they wanted to.

If anyone does know a better method of marshalling strings from managed to unmanaged I'd be happy to be enlightened as we use the double-copy code
below.
"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...

"Stu Smith" <st*****@remove.digita.com> schrieb im Newsbeitrag
news:uo*************@tk2msftngp13.phx.gbl...
> For unmanaged to managed, I was under the impression that MC++ would
> automatically marshall char * to System.String. I suppose you might

need to
> use .c_str() to get that. "IJW", as far as I can remember.
>
> For going managed to unmanaged, you need to allocate space for the

string,
> you ccould try a function like this:
>
> inline CString ToStr( System::String *strParam )
> {
> using System::Runtime::InteropServices::Marshal;
> System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
> CString str = static_cast<LPCTSTR>(
const_cast<void*>(static_cast<const
> void*>( ptr ) ) );
> Marshal::FreeHGlobal( ptr );
> return str;
> }

It seems that this example just copies characters, i.e. using parameter passing by reference means copying the data twice?

Regards,
Klaus



Nov 17 '05 #7
Yes, that apparently was the spell checker making the wrong change without
me catching it.

Ronald

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Well I am now enlightened. I didn't know you could access the internal
'bugger' but you learn something everyday.

I'll have an experiment with that once we're out of code-freeze.

Thanks.
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:Od*************@TK2MSFTNGP11.phx.gbl...
Pinning and directly copying/converting to Ansi from the internal bugger
of
a System::String results in only one copy. You can do this now.

E.g. See the section called "Converting Managed Strings to Character Arrays"
in
http://www.msdnaa.net/Resources/Display.aspx?ResID=1002

Ronald Laeremans
Visual C++ team

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
Yes, to go from managed to unmanaged you have to copy the string; you
certainly can't get a pointer to managed memory as the object might (will) move. The only safe thing you can do is copy the string.

As for copying the data twice, well System::String doesn't specify how it stores the characters (ie it can store them internally how it likes).

You've
got two real options for getting a sequence of chars out; one is to use the
ToCharArray method (probably involves copying) and the other is to use one of the marshall methods.

If you got the managed char array you could pin it and then copy it to
a CString, but that's two copies. If you use Marshall you can get an

unmanaged
string but if you want it wrapped as a CString you have to do another

copy.

I guess Microsoft could write a single-copy marshall routine that goes
straight to a CString if they wanted to.

If anyone does know a better method of marshalling strings from managed to unmanaged I'd be happy to be enlightened as we use the double-copy
code below.
"Klaus Bonadt" <Bo****@hotmail.com> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...
>
> "Stu Smith" <st*****@remove.digita.com> schrieb im Newsbeitrag
> news:uo*************@tk2msftngp13.phx.gbl...
> > For unmanaged to managed, I was under the impression that MC++ would > > automatically marshall char * to System.String. I suppose you
might need
> to
> > use .c_str() to get that. "IJW", as far as I can remember.
> >
> > For going managed to unmanaged, you need to allocate space for the
string,
> > you ccould try a function like this:
> >
> > inline CString ToStr( System::String *strParam )
> > {
> > using System::Runtime::InteropServices::Marshal;
> > System::IntPtr ptr = Marshal::StringToHGlobalAnsi( strParam );
> > CString str = static_cast<LPCTSTR>(
> const_cast<void*>(static_cast<const
> > void*>( ptr ) ) );
> > Marshal::FreeHGlobal( ptr );
> > return str;
> > }
>
> It seems that this example just copies characters, i.e. using

parameter > passing by reference means copying the data twice?
>
> Regards,
> Klaus
>
>



Nov 17 '05 #8

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

Similar topics

16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
0
by: Ola Sprauten | last post by:
I got a .NET class library project where I have written a managed wrapper for some unmanaged code. The managed wrapper is then used by a c# .net application. The problem I am having is that the...
0
by: Tony | last post by:
I have a managed C++ Console Application, and C++ Windows Forms application where I am trying to use an unmanaged header/lib file combo that exposes a class for functionality... The trouble I run...
1
by: SteveK | last post by:
Trying to wrap an unmanaged class. Here is the function I'm trying to wrap: bool Init(PCSTR filename, PCSTR username, PCSTR password, PCSTR working); In my managed C++ class, I have a method...
4
by: sklett | last post by:
I have dealt with converting managed C++ String* to PCSTR using the Marshal class, but now I need to get managed C++ String* converted to MFC CString. I can't seem to figure out how this is...
1
by: Klynt | last post by:
Project built using /CLR, but code is old and has not been converted specifically to "managed" (__gc or __value). Everything seemed to work great, until I got the following error. I have a...
0
by: stanley | last post by:
hello all, 1 - is it posiblle to pass ADO Recordset as a parameter from C++ managed code (.Net MC++) to a C++ unmanaged code (the old existing code) function?? how can i do it using the P-Invoce...
4
by: kimberly.walker | last post by:
Im very new to coding in C++ so use to coding in C#. My question is how to pass some values (string) from unmanaged code to managed code. I have two source files on a win32 console application one...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.