Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Returning a String from Managed to Unmanaged Code

Returning a String from Managed to Unmanaged Code

Scheduled Pinned Locked Moved C#
helpjson
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SanShou
    wrote on last edited by
    #1

    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 code HRESULT 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.

    H 1 Reply Last reply
    0
    • S SanShou

      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 code HRESULT 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.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You need to pass the address of wszWideBuffer. String is already a reference type (a managed pointer, if you will), so out string is a pointer to a pointer, in essense. So, you should pass &wszWideBuffer. On a side note, you might consider marshaling your string as UnmanagedType.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

      S 1 Reply Last reply
      0
      • H Heath Stewart

        You need to pass the address of wszWideBuffer. String is already a reference type (a managed pointer, if you will), so out string is a pointer to a pointer, in essense. So, you should pass &wszWideBuffer. On a side note, you might consider marshaling your string as UnmanagedType.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

        S Offline
        S Offline
        SanShou
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups