G
Guest
Thanks to a tremendous amount of patient help from Rob over in the interop
group I have managed to successfully wrap a vendor-provided DLL and call it
successfully from managed C# code. Now I'd like to process the data returned,
a void* buffer which I have "cast" into a byte[] on the C# side.
The code, like many C programs, uses casting on top of the data buffer to
impose one or more typedef'ed structs onto the void*. For instance the first
4 bytes is always the same (status messages and such), while the rest of the
buffer might contain an array of strings or doubles depending on the call. In
case you're wondering, the void* is basically hooked to a socket, this is the
raw data coming back. The example code passes the pointer to the buffer
around, casting it from one struct to another as it learns more about payload.
I could use unsafe and do the pointer work, I'm sure this would work fine.
However I'd really like to stay inside the safe world -- although many have
suggested it's not worth it.
I've looked over the documentation from MS and poked about on MSDN, but as
usual I can't find a single example of this (maybe I don't know how to look).
What I have found is Buffer and various Convert classes and such, but this
seems like "the hard way", there isn't even a way that I can find to make an
int out of a byte[4]!
So I'm looking for examples/advice here -- given a buffer with a known, but
variant, structure inside it, what is the best way to go about extracting the
data?
Maury
group I have managed to successfully wrap a vendor-provided DLL and call it
successfully from managed C# code. Now I'd like to process the data returned,
a void* buffer which I have "cast" into a byte[] on the C# side.
The code, like many C programs, uses casting on top of the data buffer to
impose one or more typedef'ed structs onto the void*. For instance the first
4 bytes is always the same (status messages and such), while the rest of the
buffer might contain an array of strings or doubles depending on the call. In
case you're wondering, the void* is basically hooked to a socket, this is the
raw data coming back. The example code passes the pointer to the buffer
around, casting it from one struct to another as it learns more about payload.
I could use unsafe and do the pointer work, I'm sure this would work fine.
However I'd really like to stay inside the safe world -- although many have
suggested it's not worth it.
I've looked over the documentation from MS and poked about on MSDN, but as
usual I can't find a single example of this (maybe I don't know how to look).
What I have found is Buffer and various Convert classes and such, but this
seems like "the hard way", there isn't even a way that I can find to make an
int out of a byte[4]!
So I'm looking for examples/advice here -- given a buffer with a known, but
variant, structure inside it, what is the best way to go about extracting the
data?
Maury