Processing byte[] returned from C DLL

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
 
N

Nicholas Paldino [.NET/C# MVP]

Maury,

If you have the managed definition of the structure, then you can
populate an IntPtr with a void * in the unsafe code. Then, you can call the
static PtrToStructure method on the Marshal class, and it will give you a
managed representation of the populated structure.

Hope this helps.
 
G

Guest

Nicholas Paldino said:
If you have the managed definition of the structure

I could, that's easy enough.
populate an IntPtr with a void * in the unsafe code.

Ok, that's easy enough too.
Then, you can call the
static PtrToStructure method on the Marshal class, and it will give you a
managed representation of the populated structure.

Ok, let me try that.

I also tried following some examples you posted a while back using the
fixed statement to "make" a pointer out of the buffer. That actually got me
some of the way into the system, but what I couldn't figure out is how to
cast the returned buffer (outside of the fixed scope) into the structure. I
suppose there is some syntax for this?

Maury
 
G

Guest

:

Worked like a champ. I'll see what happens when I get to strings though!

I decided to keep the public API strictly based on byte[], simply because
that way I don't keep exporting out the pointers to higher levels of the app.
I also found that getting the IntPtr isn't easy either, I had to fix the
array to a void* then call the constructor on that.

I maintain my opinion: this is WAY TOO HARD and WAY UNDERDOCUMENTED.
Interacting with C could should be easy, unconfusing, and if there's any
trick to it at all, very well documented. So far P/Invoke has failed on all
counts IMHO.

I might attack the doc problem myself to a small degree, I think my
experience so far might prove useful to others.

Maury
 
N

Nicholas Paldino [.NET/C# MVP]

Maury,

Depending on the structure's fields, you might be able to get away with
a cast. If the structure points to other locations in memory (as opposed to
having all of the information laid out sequentially), then you will have to
marshal the values yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top