473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Processing byte[] returned from C DLL

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
Nov 16 '05 #1
7 1475
"Nicholas Paldino [.NET/C# MVP]" wrote:
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

Nov 16 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maury Markowitz" <Ma************@discussions.microsoft.com> wrote in
message news:40**********************************@microsof t.com...
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

Nov 16 '05 #3
"Nicholas Paldino [.NET/C# MVP]" wrote:
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

Nov 16 '05 #4
"Nicholas Paldino [.NET/C# MVP]" wrote:

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

Nov 16 '05 #5
"Nicholas Paldino [.NET/C# MVP]" wrote:

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

Nov 16 '05 #6
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maury Markowitz" <Ma************@discussions.microsoft.com> wrote in
message news:07**********************************@microsof t.com...
"Nicholas Paldino [.NET/C# MVP]" wrote:
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

Nov 16 '05 #7
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maury Markowitz" <Ma************@discussions.microsoft.com> wrote in
message news:07**********************************@microsof t.com...
"Nicholas Paldino [.NET/C# MVP]" wrote:
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jmoy | last post by:
I have some data (say in a file) that needs to be handled byte by byte. Source code I have looked at does this by treating the data as a stream of 'char's. However, the standard does not require a...
4
by: Svetoslav Vasilev | last post by:
Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte. The field values in a DataTable as we all know are returned...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
1
by: Maury Markowitz | last post by:
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...
0
by: Claire | last post by:
My application has a thread reading byte arrays from an unmanaged dll(realtime controller monitoring). The array represents an unmanaged struct containing a series of header fields plus a variable...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
5
by: paul | last post by:
Hi all, Could some kind soul peruse the following code and see if there is anything wrong with it? Its producing output, but its only occupying the first third of the output array; to give an...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
0
by: eitanyan | last post by:
I am trying to create a Java to .Net interop and the way I am doing it is by creating a C# com object and a native unmanaged c++ dll that uses JNIEnv of java. the java is loading the native c++ dll...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.