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