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. BSTR to c# string

BSTR to c# string

Scheduled Pinned Locked Moved C#
csharpcomquestion
5 Posts 3 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.
  • I Offline
    I Offline
    iashishi
    wrote on last edited by
    #1

    Hi, I have a COM application that sends out _bstr_t to a .NET interop. The strings that I get for ASCII characters are fine in .NET,but I get junk values for unicode characters. Do I need to make any modifications to the string that I get in .NET to display peoper characters? Thanks in advance, Ashish Iddya

    T S 2 Replies Last reply
    0
    • I iashishi

      Hi, I have a COM application that sends out _bstr_t to a .NET interop. The strings that I get for ASCII characters are fine in .NET,but I get junk values for unicode characters. Do I need to make any modifications to the string that I get in .NET to display peoper characters? Thanks in advance, Ashish Iddya

      T Offline
      T Offline
      Tulika Shrivastava
      wrote on last edited by
      #2

      have u changed the project settings to UNICODE in ur COM application???If not then try to change the settings in the preprocessor defition and Project options to Unicode.

      I 1 Reply Last reply
      0
      • T Tulika Shrivastava

        have u changed the project settings to UNICODE in ur COM application???If not then try to change the settings in the preprocessor defition and Project options to Unicode.

        I Offline
        I Offline
        iashishi
        wrote on last edited by
        #3

        Yup, have done that. The COM application seems to pass the characters correctly. I tried returning the unicode string back to COM from .NET, & it returned the correct string. .NET seems to handle the string differently, if I pass 5 unicode characters, I get 10 characters(all junk but there are 5 commas in them). Any ideas??!?

        1 Reply Last reply
        0
        • I iashishi

          Hi, I have a COM application that sends out _bstr_t to a .NET interop. The strings that I get for ASCII characters are fine in .NET,but I get junk values for unicode characters. Do I need to make any modifications to the string that I get in .NET to display peoper characters? Thanks in advance, Ashish Iddya

          S Offline
          S Offline
          StealthyMark
          wrote on last edited by
          #4

          You have to decorate these strings in your .net interop interface/class/struct with a MarshalAs(UnmanagedType.BStr) attribute.

          I 1 Reply Last reply
          0
          • S StealthyMark

            You have to decorate these strings in your .net interop interface/class/struct with a MarshalAs(UnmanagedType.BStr) attribute.

            I Offline
            I Offline
            iashishi
            wrote on last edited by
            #5

            Tried adding MarshalAs attribute. It didnt make any difference. Here is the code I am using.. public bool CreateFile([MarshalAs(UnmanagedType.BStr)]string file,string app) { FileStream fileStream = new FileStream(file,FileMode.OpenOrCreate); /* write some stuff in the file here*/ } Unfortunately, it is not creating a proper filename if the string is unicode. The above function works correctly if I execute it directly from .NET(without MarshalAs attribut) Any suggestion is welcome..This has me stumped for over a week now. Thanks Ashish Iddya

            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