Managed String to unmanaged 
Author Message
 Managed String to unmanaged

I haven't been able to figure out how to convert a managed String into an
unmanaged array of single-byte characters.

For example:

void bar(const char* legacyStr); // bar is unmanaged

void foo(System::String* myStr)
{
  // Call bar, passing the data contained within myStr

Quote:
}

I'm trying to call bar from foo, passing the characters in myStr.  I know
that myStr only contains single-byte ASCII values so I'm not worried about
losing information in a conversion from wchar.

Can anyone help?



Wed, 10 Mar 2004 23:53:59 GMT  
 Managed String to unmanaged
AFAIK,The only way to do that would be to get the data into a wstring
(copying it one by one chars) and then use some sort of a wide char to multi
byte converter..
Something like

String* p = "Testing";
vector <wchar_t> charList;
for (int i = 0 ; i < p->Length;i++)
{
    charList.push_back(p->Chars[i]);

Quote:
}

wstring str;
str.assign(charList.begin(),charList.end());
USES_CONVERSION;
bar (W2A(str.c_str());
...
--
Girish Bharadwaj

Quote:

> I haven't been able to figure out how to convert a managed String into an
> unmanaged array of single-byte characters.

> For example:

> void bar(const char* legacyStr); // bar is unmanaged

> void foo(System::String* myStr)
> {
>   // Call bar, passing the data contained within myStr
> }

> I'm trying to call bar from foo, passing the characters in myStr.  I know
> that myStr only contains single-byte ASCII values so I'm not worried about
> losing information in a conversion from wchar.

> Can anyone help?



Fri, 12 Mar 2004 04:35:11 GMT  
 Managed String to unmanaged
Hi Girish,

Quote:
> AFAIK,The only way to do that would be to get the data into a wstring
> (copying it one by one chars) and then use some sort of a wide char to
multi
> byte converter..

Well, actually that's completely unnecessary, since the framework already
has all the facilities built in :) You can do it like this:

using namespace System::Runtime::InteropServices;
using namespace System;

System::String* str = "asdasdad"; // get the managed string instance
const char* us =
   (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
// pass us to unmanaged method

// then free string
Marshal::FreeHGlobal(IntPtr((void*)us));

HTH,
--
Tomas Restrepo



Fri, 12 Mar 2004 06:56:04 GMT  
 Managed String to unmanaged
Of course, Marshal..

--
Girish Bharadwaj

Quote:
> Hi Girish,

> > AFAIK,The only way to do that would be to get the data into a wstring
> > (copying it one by one chars) and then use some sort of a wide char to
> multi
> > byte converter..

> Well, actually that's completely unnecessary, since the framework already
> has all the facilities built in :) You can do it like this:

> using namespace System::Runtime::InteropServices;
> using namespace System;

> System::String* str = "asdasdad"; // get the managed string instance
> const char* us =
>    (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
> // pass us to unmanaged method

> // then free string
> Marshal::FreeHGlobal(IntPtr((void*)us));

> HTH,
> --
> Tomas Restrepo




Fri, 12 Mar 2004 09:03:52 GMT  
 Managed String to unmanaged
Or System.Text.Encoding.ASCII.GetBytes(string)

That's from memory, pardon if it's slightly off.

Ken


Quote:
> Of course, Marshal..

> --
> Girish Bharadwaj


> > Hi Girish,

> > > AFAIK,The only way to do that would be to get the data into a wstring
> > > (copying it one by one chars) and then use some sort of a wide char to
> > multi
> > > byte converter..

> > Well, actually that's completely unnecessary, since the framework
already
> > has all the facilities built in :) You can do it like this:

> > using namespace System::Runtime::InteropServices;
> > using namespace System;

> > System::String* str = "asdasdad"; // get the managed string instance
> > const char* us =
> >    (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
> > // pass us to unmanaged method

> > // then free string
> > Marshal::FreeHGlobal(IntPtr((void*)us));

> > HTH,
> > --
> > Tomas Restrepo




Fri, 12 Mar 2004 13:19:26 GMT  
 Managed String to unmanaged
Or use CString, which now has a constructor taking a System::String* if you
really want it simple.

Ronald Laeremans
Visual C++ compiler team


Quote:
> Or System.Text.Encoding.ASCII.GetBytes(string)

> That's from memory, pardon if it's slightly off.

> Ken



> > Of course, Marshal..

> > --
> > Girish Bharadwaj


> > > Hi Girish,

> > > > AFAIK,The only way to do that would be to get the data into a
wstring
> > > > (copying it one by one chars) and then use some sort of a wide char
to
> > > multi
> > > > byte converter..

> > > Well, actually that's completely unnecessary, since the framework
> already
> > > has all the facilities built in :) You can do it like this:

> > > using namespace System::Runtime::InteropServices;
> > > using namespace System;

> > > System::String* str = "asdasdad"; // get the managed string instance
> > > const char* us =
> > >    (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
> > > // pass us to unmanaged method

> > > // then free string
> > > Marshal::FreeHGlobal(IntPtr((void*)us));

> > > HTH,
> > > --
> > > Tomas Restrepo




Fri, 12 Mar 2004 13:28:44 GMT  
 Managed String to unmanaged
Umm.. Doesn't that return unsigned char __gc[] type.. I was unable to stuff
it into a vector<char>.. I had to do the byte by byte copy..
But, I like the fact that CString accepts String*..

--
Girish Bharadwaj


Quote:
> Or System.Text.Encoding.ASCII.GetBytes(string)

> That's from memory, pardon if it's slightly off.

> Ken



> > Of course, Marshal..

> > --
> > Girish Bharadwaj


> > > Hi Girish,

> > > > AFAIK,The only way to do that would be to get the data into a
wstring
> > > > (copying it one by one chars) and then use some sort of a wide char
to
> > > multi
> > > > byte converter..

> > > Well, actually that's completely unnecessary, since the framework
> already
> > > has all the facilities built in :) You can do it like this:

> > > using namespace System::Runtime::InteropServices;
> > > using namespace System;

> > > System::String* str = "asdasdad"; // get the managed string instance
> > > const char* us =
> > >    (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
> > > // pass us to unmanaged method

> > > // then free string
> > > Marshal::FreeHGlobal(IntPtr((void*)us));

> > > HTH,
> > > --
> > > Tomas Restrepo




Fri, 12 Mar 2004 20:40:24 GMT  
 Managed String to unmanaged
This did the trick.

Thanks very much,

Mark


Quote:
> Hi Girish,

> > AFAIK,The only way to do that would be to get the data into a wstring
> > (copying it one by one chars) and then use some sort of a wide char to
> multi
> > byte converter..

> Well, actually that's completely unnecessary, since the framework already
> has all the facilities built in :) You can do it like this:

> using namespace System::Runtime::InteropServices;
> using namespace System;

> System::String* str = "asdasdad"; // get the managed string instance
> const char* us =
>    (const char*)((Marshal::StringToHGlobalAnsi(str)).ToPointer());
> // pass us to unmanaged method

> // then free string
> Marshal::FreeHGlobal(IntPtr((void*)us));

> HTH,
> --
> Tomas Restrepo




Mon, 15 Mar 2004 11:58:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Converting managed String to unmanaged char*

2. conversion from managed System::String to unmanaged UUID?

3. better way to use strings in managed and unmanaged code

4. Managed and unmanaged strings

5. Exception across managed/unmanaged

6. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

7. Managed vs. Unmanaged

8. Debugging into managed c++ DLL that is being used by unmanaged code

9. managed and unmanaged object pointers in same dll

10. LINK : error LNK2020: unresolved token (0A000055) (managed to unmanaged)

11. Managed C++ call unmanaged DLL error

12. Performance of unmanaged C++ in a managed C++ app

 

 
Powered by phpBB® Forum Software