Converting managed String to unmanaged char* 
Author Message
 Converting managed String to unmanaged char*

I have a problem converting a string I get from a C# function to a char* which I
use in my ATL Server.

Does anyone have a solution for this problem?

Regards

Thomas Petersen
MCSD Developer



Wed, 27 Aug 2003 23:40:01 GMT  
 Converting managed String to unmanaged char*
Hi Thomas,
<<
I have a problem converting a string I get from a C# function to a char*
which I
use in my ATL Server.

Does anyone have a solution for this problem?

If you want to convert it manually, you can use the
System::Runtime::InteropServices::Marshal class, specifically the
StringToHGlobal() and FreeHGlobal() methods.

How are you using it C# in your ATLServer application? I'm curious...

--
Tomas Restrepo [VC++ MVP]

http://www.mvps.org/windev/



Thu, 28 Aug 2003 00:46:51 GMT  
 Converting managed String to unmanaged char*

Quote:
>>If you want to convert it manually, you can use the
>>System::Runtime::InteropServices::Marshal class, specifically the
>>StringToHGlobal() and FreeHGlobal() methods.

Yeps, I saw that in an earlier posting, but now my problem is this:

The StringToHGlobal returns an int (pointer to memory, which I have seen holds my
string), yet my casting to LPWSTR fails. The resulting string is empty! :-(

What am I doing wrong here?

<snip>
MyAtlServerTestClass::MenuClass *MyMenu; //C# class
String *s = MyMenu->Menu;                //Get the HTML string for the menu
LPWSTR MyString = (LPWSTR)(void*)
system::Runtime::InteropServices::Marshal::StringToHGlobalUni(s);
//MyString is empty now!
</snip>

Quote:
>>How are you using it C# in your ATLServer application? I'm curious...

Actually I'm not doing it yet, I'm researching the possibilities. The issue is,
that my company has a lot of VB programmers, but we have to have the performance
of the ATLServer.
Therefore I use the ATLServer for an easy ISAPI and performance filter, and the
VB guys use C# for better coding. I'll probably have to assist them for that
too... :-)

Any experiences with that setup?

Regards

Thomas Petersen
MCSD Developer



Thu, 28 Aug 2003 05:49:37 GMT  
 Converting managed String to unmanaged char*
I'm sorry for the inconvenience, but I've found out, that I can't trust
the "Locals" window. A char* doesn't show it's pointer location, nor the content
of the location...   :-(

In other words: It works in the code!!

Quote:
-----Original Message-----
>>If you want to convert it manually, you can use the
>>System::Runtime::InteropServices::Marshal class, specifically the
>>StringToHGlobal() and FreeHGlobal() methods.

Yeps, I saw that in an earlier posting, but now my problem is this:

The StringToHGlobal returns an int (pointer to memory, which I have seen holds my
string), yet my casting to LPWSTR fails. The resulting string is empty! :-(

What am I doing wrong here?

<snip>
MyAtlServerTestClass::MenuClass *MyMenu; //C# class
String *s = MyMenu->Menu;                //Get the HTML string for the menu
LPWSTR MyString = (LPWSTR)(void*)
system::Runtime::InteropServices::Marshal::StringToHGlobalUni(s);
//MyString is empty now!
</snip>

Quote:
>>How are you using it C# in your ATLServer application? I'm curious...

Actually I'm not doing it yet, I'm researching the possibilities. The issue is,
that my company has a lot of VB programmers, but we have to have the performance
of the ATLServer.
Therefore I use the ATLServer for an easy ISAPI and performance filter, and the
VB guys use C# for better coding. I'll probably have to assist them for that
too... :-)

Any experiences with that setup?

Regards

Thomas Petersen
MCSD Developer

.



Thu, 28 Aug 2003 06:07:43 GMT  
 Converting managed String to unmanaged char*
Hi Thomas,

Quote:
>Yeps, I saw that in an earlier posting, but now my problem is this:
>The StringToHGlobal returns an int (pointer to memory, which I have seen
holds my
>string), yet my casting to LPWSTR fails. The resulting string is empty! :-(
> What am I doing wrong here?

I don't see anything particularly wrong with your code... You might want to
avoid the double cast... as Single cast to LPWSTR is enough, really. FWIW,
this simple example works for me:
   String* s = S"This is a String!";
   wchar_t* us = (wchar_t*)Marshal::StringToHGlobalUni(s);
   std::wcout << us;

so it obviously works. Are you sure that MyMenu->Menu is returning a
non-empty, non-null string?

Quote:
>Actually I'm not doing it yet, I'm researching the possibilities. The issue
is,
>that my company has a lot of VB programmers, but we have to have the
performance
>of the ATLServer.
>Therefore I use the ATLServer for an easy ISAPI and performance filter, and
the
>VB guys use C# for better coding. I'll probably have to assist them for
that
>too... :-)
>Any experiences with that setup?

Not really, but I think it's an scenario that's a little confusing, and It'd
be interesting to now if you've found a big performance benefit with it, as
compared with using ASP.NET with code behind.... I can offer a couple of
comments, though:

1- Avoid compiling all of your ATLServer code with /CLR, instead, use a thin
layer of managed code (possible built using unmanaged classes as wrappers)
between your C# code and the core of your ATLServer code, otherwise, you
risk giving up a large portion of the performance benefits of your ATLServer
code.

2- Be wary of having too many join points where unmanaged code calls into
managed code and back. Managed-Unmanaged transitions are expensive, for one
thing, and it might make your code harder to mantain later.

Let me know how it goes! I'd love to hear how it turns out!

--
Tomas Restrepo [VC++ MVP]

http://www.mvps.org/windev/



Thu, 28 Aug 2003 06:16:56 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

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

3. Managed String to unmanaged

4. Managed and unmanaged strings

5. String * to unmanaged const char *

6. Converting a char string into a wchar string.

7. Managed C++ String to char*

8. How to convert a managed string to a BSTR

9. managed c++: how convert String in LPCSTR ?

10. Managed array of managed arrays of char

11. Converting char array of literals chars to escape chars

12. char * to wchar_t (char string to wide string)

 

 
Powered by phpBB® Forum Software