Returning a String from Managed to Unmanaged Code
-
Hey, I am trying to get a string returned from my managed code to my unmanaged code. Basically what I have so far is an interface as follows:
[ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("")] public interface IGet { void GetString([MarshalAs(UnmanagedType.LPWStr)] out string Value); } public class PropertyGet : IGet { public PropertyGet(Item item) { m_item = item; } public void GetString(out string Value) { Property prop = m_item.GetProperty(); Value = prop.ToString(); } }
In the managed codeHRESULT CFileProperties(BSTR fileName, IGet pIGet) { WCHAR wszWideBuffer[1024]; // NULL_CHARACTER is appropriate for wide characters wszWideBuffer[0] = NULL_CHARACTER; pIGet->GetString(wszWideBuffer); }
So in the managed code right now, the wszWideBuffer is filled with some wierd characters. So something is not marshalling correctly and with my limited experience I am not sure where to look for the problem. Most articles seem to handle passing strings the other way (i.e. getting a string back from the unmanaged which works in my property setting code.) Any help would be appreciated. Brian If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life. -
Hey, I am trying to get a string returned from my managed code to my unmanaged code. Basically what I have so far is an interface as follows:
[ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("")] public interface IGet { void GetString([MarshalAs(UnmanagedType.LPWStr)] out string Value); } public class PropertyGet : IGet { public PropertyGet(Item item) { m_item = item; } public void GetString(out string Value) { Property prop = m_item.GetProperty(); Value = prop.ToString(); } }
In the managed codeHRESULT CFileProperties(BSTR fileName, IGet pIGet) { WCHAR wszWideBuffer[1024]; // NULL_CHARACTER is appropriate for wide characters wszWideBuffer[0] = NULL_CHARACTER; pIGet->GetString(wszWideBuffer); }
So in the managed code right now, the wszWideBuffer is filled with some wierd characters. So something is not marshalling correctly and with my limited experience I am not sure where to look for the problem. Most articles seem to handle passing strings the other way (i.e. getting a string back from the unmanaged which works in my property setting code.) Any help would be appreciated. Brian If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.You need to pass the address of
wszWideBuffer
.String
is already a reference type (a managed pointer, if you will), soout string
is a pointer to a pointer, in essense. So, you should pass&wszWideBuffer
. On a side note, you might consider marshaling your string asUnmanagedType.BStr
(which is a length-prefixed wide character array). This is more common in COM and necessary for OLE automation (if you plan on support automation clients like VB6 and older).Microsoft MVP, Visual C# My Articles
-
You need to pass the address of
wszWideBuffer
.String
is already a reference type (a managed pointer, if you will), soout string
is a pointer to a pointer, in essense. So, you should pass&wszWideBuffer
. On a side note, you might consider marshaling your string asUnmanagedType.BStr
(which is a length-prefixed wide character array). This is more common in COM and necessary for OLE automation (if you plan on support automation clients like VB6 and older).Microsoft MVP, Visual C# My Articles
Thanks for the response. I did try that and it caused me some issues with building the unmanaged code. However I was able to change the string to a stringbuilder which is listed as a both an in and out in the built type library. This was suggested in the big purple book that I borrowed from a friend. Thanks, Brian If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.