[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

How to marshal returned unicode string pointers

Roshan

7/10/2007 11:59:00 PM

Hi All,

>From my C# code, I am calling a native method which returns a PWSTR. I
receive the return value as a IntPtr and then marshal it to a string.
I notice that I get the correct return value if I use
Marshal.PtrToStringAnsi but not if I use PtrToStringAuto or
PtrToStringUni. How do I marshal the return value if the return value
is actually a unicode string?

[DllImport("My.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.SysUInt)]
public static extern IntPtr GetUnicodeString(
[In] uint index);

Using it as

IntPtr myStr =GetUnicodeString(index);
String myString = Marshal.PtrToStringAnsi(myStr); // Works
String myString = Marshal.PtrToStringAuto (myStr); // Does not Work
String myString = Marshal.PtrToStringUni(myStr); // Does not Work

Does any one know how to return unicode strings?

Thanks,
RA