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. .NET (Core and Framework)
  4. Converting .NET String to something usable by old unsafe C/C++ Code

Converting .NET String to something usable by old unsafe C/C++ Code

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpc++data-structureshelpquestion
4 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.
  • D Offline
    D Offline
    DVryce
    wrote on last edited by
    #1

    I have a class that has been written in plain C++ that makes calls into a library that exports many functions that take char array parameters. What I want to know is how I can get / convert the data in a .NET String object into a plain char array so that I can pass the value into the function call? Basically any help converting the new .NET types into older style "unsafe" types would be helpful. Thanks.

    J 1 Reply Last reply
    0
    • D DVryce

      I have a class that has been written in plain C++ that makes calls into a library that exports many functions that take char array parameters. What I want to know is how I can get / convert the data in a .NET String object into a plain char array so that I can pass the value into the function call? Basically any help converting the new .NET types into older style "unsafe" types would be helpful. Thanks.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      You can use one of two things, either the StringBuilder or you can marshal the string data. If you need a buffer of some sort, like you need for getting a special folder you need to use StringBuilder Code courtesy Tomas Restrepo

      [ DllImport("shell32.dll") ]
      private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, bool fCreate);

      StringBuilder dirPath = new StringBuilder(256,1024);
      bool res = SHGetSpecialFolderPath(IntPtr.Zero, dirPath, CSIDL_APPDATA, true);

      If you just need to pass a string in to a function but do not modify it use marshalling. I don't know what will happen if you try to modify it, it could reflect the change, or it could blow up on you. From Inside C# by Tom Archer

      [DllImport("user32.dll", CharSet=CharSet.Unicode)]
      static extern int MessageBox(
      IntPtr hWnd,
      [MarshalAs(UnmanagedType.LPWStr)]
      string msg,
      [MarshalAs(UnmanagerType.LPWStr)]
      string caption,
      int type
      );

      MessageBox(0, "Hello, World!", "This is called from a C# app!", 0);

      Both code snippets are untested, but should demonstrate how to do it. I made a minor change in Tom Archer's code and that was to reflect that handles, such as HWNDs should be IntPtr's instead of int's. HTH, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)

      D 1 Reply Last reply
      0
      • J James T Johnson

        You can use one of two things, either the StringBuilder or you can marshal the string data. If you need a buffer of some sort, like you need for getting a special folder you need to use StringBuilder Code courtesy Tomas Restrepo

        [ DllImport("shell32.dll") ]
        private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, bool fCreate);

        StringBuilder dirPath = new StringBuilder(256,1024);
        bool res = SHGetSpecialFolderPath(IntPtr.Zero, dirPath, CSIDL_APPDATA, true);

        If you just need to pass a string in to a function but do not modify it use marshalling. I don't know what will happen if you try to modify it, it could reflect the change, or it could blow up on you. From Inside C# by Tom Archer

        [DllImport("user32.dll", CharSet=CharSet.Unicode)]
        static extern int MessageBox(
        IntPtr hWnd,
        [MarshalAs(UnmanagedType.LPWStr)]
        string msg,
        [MarshalAs(UnmanagerType.LPWStr)]
        string caption,
        int type
        );

        MessageBox(0, "Hello, World!", "This is called from a C# app!", 0);

        Both code snippets are untested, but should demonstrate how to do it. I made a minor change in Tom Archer's code and that was to reflect that handles, such as HWNDs should be IntPtr's instead of int's. HTH, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)

        D Offline
        D Offline
        DVryce
        wrote on last edited by
        #3

        Thanks for the good information... but is there a way I can convert to an unsafe type BEFORE actually passing the data into a function? If I've got a String* or Stringbuilder* in Managed C++ and want to convert it into an unmanged type like char* or LPStr, how is that possible? -dvryce

        J 1 Reply Last reply
        0
        • D DVryce

          Thanks for the good information... but is there a way I can convert to an unsafe type BEFORE actually passing the data into a function? If I've got a String* or Stringbuilder* in Managed C++ and want to convert it into an unmanged type like char* or LPStr, how is that possible? -dvryce

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Can't say I know how. I haven't looked at MC++ too much, only because I don't have any old code I have to work with that would warrant MC++ to me. Perhaps the DOTNET mailing list could be of some help? Its been a great resource for myself and many others. It is a high volume mailing list though so you'll probably want to subscribe to the digest version, or use the web-based interface. Sorry I couldn't be of more help, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)

          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