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. StringBuilder, byte[], lpstr, interop

StringBuilder, byte[], lpstr, interop

Scheduled Pinned Locked Moved C#
helpquestioncomtutorial
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.
  • T Offline
    T Offline
    Tym
    wrote on last edited by
    #1

    Can't quite get this right. I have: [StructLayout(LayoutKind.Sequential)] public struct MIDIHDR { [MarshalAs(UnmanagedType.LPStr)] public String Data; public uint Length; public uint RecBytes; public uint User; public uint Flags; public uint Next; public uint reserve; public uint Offset; public uint Reserved; } I am having problems getting the right MIDIHDR.data value, I think. I am building the data as follows: I am building the string with a StringBuilder object. I StringBuilder.Append() anywhere from 4 to 16 bytes. I set MIDIHDR.data = StringBuilder.ToString() and MIDIHDR.Length = StringBuilder.Length When I test it by sending the MIDIHDR to a midiOutLongMsg call, I have gotten either no sound, or the same, wrong note, no matter what string I send. example bytes {153, 41, 127, 0} StringBuilder.ToString() outputs "153411270" with a length of 9 What i believe this is doing is converting the byte with a value of 153 to a string of length 3 ("153"). Is there a way to build this string as just the 4 bytes? Can I, for example, cast the byte to char before appending to the string? Am I gonna have problems with the unicode/ansi conversion in the string and it's length as its size in the struct??? Or is the marshalling of the struct really taking care of all this and I just have an error elsewhere? ( I may not understand the proper way to use midiOutLongMsg() to send multiple ShortMsg's simultaneously...) I was making headway with marshalling/interop and have been successful with everything but this.:confused: I have used the same byte data to successfully make calls to midiOutShortMsg. The bytes are actually stored in a struct of 4 bytes coerced into a union as an int. So it would be neat if I could just build the string using this int value to represent the 4 bytes, but for now I just wanna get it to work. i am going to keep playing and researching, but I thought I'd stick the question here to see if I get a nibble. Any help or insight would be greatly appreciated! Thanks! Tym! Make love, not chocolate chip cookies with nuts. At least make the cookies without nuts for crying out loud.

    J 1 Reply Last reply
    0
    • T Tym

      Can't quite get this right. I have: [StructLayout(LayoutKind.Sequential)] public struct MIDIHDR { [MarshalAs(UnmanagedType.LPStr)] public String Data; public uint Length; public uint RecBytes; public uint User; public uint Flags; public uint Next; public uint reserve; public uint Offset; public uint Reserved; } I am having problems getting the right MIDIHDR.data value, I think. I am building the data as follows: I am building the string with a StringBuilder object. I StringBuilder.Append() anywhere from 4 to 16 bytes. I set MIDIHDR.data = StringBuilder.ToString() and MIDIHDR.Length = StringBuilder.Length When I test it by sending the MIDIHDR to a midiOutLongMsg call, I have gotten either no sound, or the same, wrong note, no matter what string I send. example bytes {153, 41, 127, 0} StringBuilder.ToString() outputs "153411270" with a length of 9 What i believe this is doing is converting the byte with a value of 153 to a string of length 3 ("153"). Is there a way to build this string as just the 4 bytes? Can I, for example, cast the byte to char before appending to the string? Am I gonna have problems with the unicode/ansi conversion in the string and it's length as its size in the struct??? Or is the marshalling of the struct really taking care of all this and I just have an error elsewhere? ( I may not understand the proper way to use midiOutLongMsg() to send multiple ShortMsg's simultaneously...) I was making headway with marshalling/interop and have been successful with everything but this.:confused: I have used the same byte data to successfully make calls to midiOutShortMsg. The bytes are actually stored in a struct of 4 bytes coerced into a union as an int. So it would be neat if I could just build the string using this int value to represent the 4 bytes, but for now I just wanna get it to work. i am going to keep playing and researching, but I thought I'd stick the question here to see if I get a nibble. Any help or insight would be greatly appreciated! Thanks! Tym! Make love, not chocolate chip cookies with nuts. At least make the cookies without nuts for crying out loud.

      J Offline
      J Offline
      Jeremy Kimball
      wrote on last edited by
      #2

      It's too damned early for me to really play with this too much, but my gut is telling me you should be using a byte array (byte[]) rather than a String/StringBuilder. Reason being that the LPSTR in the MIDIHDR is really a char* (read:byte array). Have you taken a stab at using byte arrays yet? You may not even need the MarshalAs attribute in that case...not sure. Jeremy Kimball

      T 2 Replies Last reply
      0
      • J Jeremy Kimball

        It's too damned early for me to really play with this too much, but my gut is telling me you should be using a byte array (byte[]) rather than a String/StringBuilder. Reason being that the LPSTR in the MIDIHDR is really a char* (read:byte array). Have you taken a stab at using byte arrays yet? You may not even need the MarshalAs attribute in that case...not sure. Jeremy Kimball

        T Offline
        T Offline
        Tym
        wrote on last edited by
        #3

        Thanks for the advice. I was leaning that way, I got as far as using a byte array, but still using the marshalling attribute, which gave me a runtime error. I think you will prove to be right, it makes sense. Thanks again, I'll let ya know if it works when I get a chance to try it... Tym!

        1 Reply Last reply
        0
        • J Jeremy Kimball

          It's too damned early for me to really play with this too much, but my gut is telling me you should be using a byte array (byte[]) rather than a String/StringBuilder. Reason being that the LPSTR in the MIDIHDR is really a char* (read:byte array). Have you taken a stab at using byte arrays yet? You may not even need the MarshalAs attribute in that case...not sure. Jeremy Kimball

          T Offline
          T Offline
          Tym
          wrote on last edited by
          #4

          Still can't get it to work... I have tried combinations of : byte[] char[] String StringBuilder no MarshalAs MarshalAs LPStr, ByValTStr but I get the following error each time except when building the buffer with a StringBuilder and declaring data as a string, don't need any marshalling: MIDIHDR can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. I'm thinking I just need build the string differently, so I'm gonna see what I can find on StringBuilder and hope it gets me somewhere. Thanks!

          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