?
Solved

C# and C

Posted on 2003-03-22
10
Medium Priority
?
1,696 Views
Last Modified: 2008-02-01
Hi

I have a C# struct with a byte array as one of the members.  I want to pass that array to a C device driver and then the device driver needs to fill in the byte array, but when I get the struct back and display one of the byte fields it's 0, or any other member of the struct.

My struct in C#:

[StructLayout(LayoutKind.Sequential)]
public struct myStruct
{
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
   byte[] bArray;             //If I make this only one byte it works.
}

My struct in C:

typedef struct _MYSTRUCT {
    PUCHAR Data;
} MYSTRUCT, *PMYSTRUCT;


The thing is I just want to get the whole string back from the driver (C), if there's another way, let me know.

In short I want to be able to get a PUCHAR from my driver to a STRING in my C# app, and a STRING back to a PUCHAR in my driver.

Thanks
0
Comment
Question by:Bulls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8202584
Just declare it as STRING, and specify that the CharSet=CharSet.Ansi so that only 8bit-chars are being used.
0
 

Expert Comment

by:Skeptimist
ID: 8209123
It is very cumbersome to use the standard marshalling and interop features of the CLR to interface with your device drivers.  You are better off serializing your struct or array data into a byte array in C#, locking the array (to get a pointer), and passing the pointer as a 32-bit value to the driver.  Once in the driver, you can typecast the pointer to an array or struct or whatever.  As far as dealing with strings, I have found that it is best to convert C# strings to and from byte arrays (8-bit ASCII), which is simply a typecast for each character going in and coming out of the driver.  To keep things robust, you might come up with a scheme such as adding a header to each buffer to identify the struct and/or length of the data coming into the driver.  I have also found that using an intermediate DLL (written in C++) to connect the managed environment to a driver can make everything easier to work with.  Doing things this way takes a bit more work initially, but it helps avoid a lot of headache in the long run.
0
 

Author Comment

by:Bulls
ID: 8217925
Hi

AvonWyss, your answer didn't work and Skeptimist, I'm not exactly sure what you mean.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bulls
ID: 8218735
Hi

At the moment I've got the following struct in my C header:

typedef struct _SERIAL_DATA {
   ULONG DeviceOpen;
   char Data[256];
} SERIAL_DATA, *PSERIAL_DATA;

In my C# app:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.ANSI)]
public struct SERIAL_DATA
{
   public int DeviceOpen;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
   public byte[] Data;
}


I send the C# struct into my device driver which is in C, then the device driver fills it like this:

SERIAL_DATA Data;

Data.DeviceOpen = 1;
memcpy(&Data.Data, Extension->Data, sizeof(Extension->Data));

And it gets send back to my C# app, and when I want to use the data, there's nothing!

Please help me!

Thanks
Cheers
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8220229
Bulls, the first struct you posted was different...:

typedef struct _MYSTRUCT {
   PUCHAR Data;
} MYSTRUCT, *PMYSTRUCT;

For this struct, the approach with the STRING should work perfectly. ;-)
0
 

Author Comment

by:Bulls
ID: 8220528
I know, but it didn't, and I had to change the struct to fit in the whole string, otherwise it gets cut of at 4 chars, so I had to change it.  Any ideas?

Thanks
0
 

Expert Comment

by:Skeptimist
ID: 8220566
The reason it got cut off at four characters is because the struct in your driver contains a *pointer* to a string, not a string itself.  You are sending it a struct contaning a string, not a pointer to a string.  Like I said before, it is much easier to just convert everything to arrays of byte and pass pointers (of those arrays) down to the driver, rather than trying to get structs to match up on both sides.  Especially when those structs contain pointers to other data elements.
0
 

Accepted Solution

by:
Bulls earned 0 total points
ID: 8222785
Hi Skeptimist,

Could you maybe give me a short code example, please?

Thanks
0
 

Expert Comment

by:modulo
ID: 8510909
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/Q_20613454.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question