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. Strings and Interop

Strings and Interop

Scheduled Pinned Locked Moved C#
csharpcomdata-structurestutorialquestion
7 Posts 4 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.
  • B Offline
    B Offline
    Beringer
    wrote on last edited by
    #1

    Can someone please tell me how to create the appropriate buffer to hold characters in a LPCTSTR buffer in C#. I have tried using Strings but the results are chaotic to say the least. For instance, SendMessage, often will use a LPCTSTR in the LPARAM to return a collection of characters. How do I create the appropriate array in C#. Eric

    B J D 3 Replies Last reply
    0
    • B Beringer

      Can someone please tell me how to create the appropriate buffer to hold characters in a LPCTSTR buffer in C#. I have tried using Strings but the results are chaotic to say the least. For instance, SendMessage, often will use a LPCTSTR in the LPARAM to return a collection of characters. How do I create the appropriate array in C#. Eric

      B Offline
      B Offline
      Beringer
      wrote on last edited by
      #2

      I'm sorry to be more clear, How do I get a string safely into an IntPtr since I have defined my SendMessage with LPARAM as an IntPtr? Thanks, Eric

      A 1 Reply Last reply
      0
      • B Beringer

        Can someone please tell me how to create the appropriate buffer to hold characters in a LPCTSTR buffer in C#. I have tried using Strings but the results are chaotic to say the least. For instance, SendMessage, often will use a LPCTSTR in the LPARAM to return a collection of characters. How do I create the appropriate array in C#. Eric

        J Offline
        J Offline
        James Gupta
        wrote on last edited by
        #3

        Try casting it to a C# string from a lpcstr

        1 Reply Last reply
        0
        • B Beringer

          I'm sorry to be more clear, How do I get a string safely into an IntPtr since I have defined my SendMessage with LPARAM as an IntPtr? Thanks, Eric

          A Offline
          A Offline
          Andy Moore
          wrote on last edited by
          #4

          Try System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(Auto/Uni). This will do the trick. Andy Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

          B 1 Reply Last reply
          0
          • B Beringer

            Can someone please tell me how to create the appropriate buffer to hold characters in a LPCTSTR buffer in C#. I have tried using Strings but the results are chaotic to say the least. For instance, SendMessage, often will use a LPCTSTR in the LPARAM to return a collection of characters. How do I create the appropriate array in C#. Eric

            D Offline
            D Offline
            DigitalKing
            wrote on last edited by
            #5

            Use StringBuilder from the System.Text namespace.

            [DllImport("kernel32.dll")]
            static extern bool GetVolumeNameForVolumeMountPoint(string lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);

            public static string GetVolumeName(string MountPoint)
            {
            StringBuilder sb = new StringBuilder();
            GetVolumeNameForVolumeMountPoint(MountPoint, sb, 100);
            return sb.ToString();
            }

            B 1 Reply Last reply
            0
            • D DigitalKing

              Use StringBuilder from the System.Text namespace.

              [DllImport("kernel32.dll")]
              static extern bool GetVolumeNameForVolumeMountPoint(string lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);

              public static string GetVolumeName(string MountPoint)
              {
              StringBuilder sb = new StringBuilder();
              GetVolumeNameForVolumeMountPoint(MountPoint, sb, 100);
              return sb.ToString();
              }

              B Offline
              B Offline
              Beringer
              wrote on last edited by
              #6

              I tried a StringBuilder but I got empty strings back. I think it has to do with the LPARAM parameter needing to be LPCSTR type pointer and StringBuilder is in appropriate. What I am trying to do is use the SendMessage API to get the textline in an edit control with EM_GETLINE. The LPARAM parameter in this case is an LPCSTR pointer to a buffer of characters and I just don't know how to allocate the memory for this to happen correctly. Thanks, Eric

              1 Reply Last reply
              0
              • A Andy Moore

                Try System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(Auto/Uni). This will do the trick. Andy Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

                B Offline
                B Offline
                Beringer
                wrote on last edited by
                #7

                Thanks Andy, this seems to work! Eric

                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