Click here to Skip to main content
15,905,683 members
Home / Discussions / C#
   

C#

 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Guffa23-Aug-05 12:15
Guffa23-Aug-05 12:15 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
David Stone23-Aug-05 12:33
sitebuilderDavid Stone23-Aug-05 12:33 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Guffa23-Aug-05 12:53
Guffa23-Aug-05 12:53 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Daniel132423-Aug-05 13:14
Daniel132423-Aug-05 13:14 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
Daniel132423-Aug-05 13:29
Daniel132423-Aug-05 13:29 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
David Stone23-Aug-05 12:34
sitebuilderDavid Stone23-Aug-05 12:34 
GeneralRe: Email in C# 2005 (.net 2.0) Pin
senorbadger10-Mar-09 7:12
senorbadger10-Mar-09 7:12 
QuestionHow to copy struct to byte [] ? Pin
queisser23-Aug-05 11:04
queisser23-Aug-05 11:04 
I need to call an unmanaged function that takes, basically, a void pointer to a buffer. I've got the transfer to the unmanaged DLL working by passing a byte [] and letting .NET marshal. However, the buffer may contain different data structures on different calls since it's a kind of generic entry point with the exact function specified in the first field. In C I would do something like this:

char buf[1000];

struct Header {
int cmd;
int flags;
int param;
};

struct Cmd1SpecificData {
double param1ForCommand1;
short param2ForCommand1;
short param3ForCommand1;
// and so on...
};

void foo()
{
Header *header = (Header *)buf;
Cmd1SpecificData = (Cmd1SpecificData *)(header + 1);
header->cmd = 1;
header->flags = 999;
Cmd1SpecificData->param1ForCommand1 = 123.456;

APICALL(buf);
}

APICALL can now look at the first field and figure out what to do.



In C# I don't quite know how to do this. I have a byte [] which is the equivalent of the char buf[] but how do I cast pointers into the byte []? I couldn't find a simple way to copy data from a struct to a byte [] or how to use a MemoryStream to stream the data into the byte [].

My questions:

1) What's the best way of getting the contents of a struct into a byte array? Note that I'll need to get different structures chained together so I might need to put the contents of the structs at specific offsets in the byte array.

2) Is there a fundamentally better way of getting .NET data to an API of this sort? Maybe I should do the marshalling myself and convert everything to a pointer before calling the unmanaged code?

Thanks,
Andrew Queisser

AnswerRe: How to copy struct to byte [] ? Pin
Bojan Rajkovic23-Aug-05 19:44
Bojan Rajkovic23-Aug-05 19:44 
AnswerRe: How to copy struct to byte [] ? Pin
leppie23-Aug-05 21:47
leppie23-Aug-05 21:47 
GeneralSelection Frames Pin
Shawn Dwyer23-Aug-05 10:06
Shawn Dwyer23-Aug-05 10:06 
GeneralDatagrid help! Pin
dgap23-Aug-05 8:27
dgap23-Aug-05 8:27 
GeneralRe: Datagrid help! Pin
Guffa23-Aug-05 9:21
Guffa23-Aug-05 9:21 
QuestionHow can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 7:40
professionalTesic Goran23-Aug-05 7:40 
AnswerRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 8:05
Daniel132423-Aug-05 8:05 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 8:12
professionalTesic Goran23-Aug-05 8:12 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 8:39
Daniel132423-Aug-05 8:39 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:12
professionalTesic Goran23-Aug-05 9:12 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Daniel132423-Aug-05 9:17
Daniel132423-Aug-05 9:17 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:25
professionalTesic Goran23-Aug-05 9:25 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
lmoelleb23-Aug-05 21:35
lmoelleb23-Aug-05 21:35 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Dave Kreskowiak23-Aug-05 8:42
mveDave Kreskowiak23-Aug-05 8:42 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Tesic Goran23-Aug-05 9:46
professionalTesic Goran23-Aug-05 9:46 
GeneralRe: How can I prevent my control to be serialized in design-mode? Pin
Dave Kreskowiak23-Aug-05 10:00
mveDave Kreskowiak23-Aug-05 10:00 
QuestionHow can I check inside the control where I am - in design mode or run-time mode? Pin
Anonymous23-Aug-05 7:34
Anonymous23-Aug-05 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.