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 7 8204
i've got the same problem, please help
--
kind regards, muechel
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
"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
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
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
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
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 > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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...
| |