Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
GeneralSMS Server Pin
Zami_223-Aug-05 18:47
Zami_223-Aug-05 18:47 
GeneralSending sms Pin
Zami_223-Aug-05 18:43
Zami_223-Aug-05 18:43 
GeneralRe: Sending sms Pin
Luis Alonso Ramos23-Aug-05 19:31
Luis Alonso Ramos23-Aug-05 19:31 
QuestionCreating Controls Dynamically using a For Loop? Pin
Darktaz23-Aug-05 18:32
Darktaz23-Aug-05 18:32 
AnswerRe: Creating Controls Dynamically using a For Loop? Pin
Christian Graus23-Aug-05 18:40
protectorChristian Graus23-Aug-05 18:40 
AnswerRe: Creating Controls Dynamically using a For Loop? Pin
Luis Alonso Ramos23-Aug-05 19:35
Luis Alonso Ramos23-Aug-05 19:35 
Generalmanaging the calendar Pin
lynnafna23-Aug-05 17:27
lynnafna23-Aug-05 17:27 
GeneralRetrieve and change email account settings Outlook Pin
23-Aug-05 16:21
suss23-Aug-05 16:21 
Generalpassing datareader to a User control Pin
dotcomcrash200523-Aug-05 15:36
dotcomcrash200523-Aug-05 15:36 
GeneralRe: passing datareader to a User control Pin
tiger200523-Aug-05 16:52
tiger200523-Aug-05 16:52 
GeneralProblem with Form Dispose please help Pin
Darktaz23-Aug-05 13:03
Darktaz23-Aug-05 13:03 
GeneralRe: Problem with Form Dispose please help Pin
David Stone23-Aug-05 13:26
sitebuilderDavid Stone23-Aug-05 13:26 
GeneralRe: Problem with Form Dispose please help Pin
Darktaz23-Aug-05 13:45
Darktaz23-Aug-05 13:45 
GeneralRe: Problem with Form Dispose please help Pin
tiger200523-Aug-05 14:49
tiger200523-Aug-05 14:49 
GeneralRe: Problem with Form Dispose please help Pin
Darktaz23-Aug-05 15:12
Darktaz23-Aug-05 15:12 
Generalusing rasapi32 for PPPoE :~ Pin
devitj23-Aug-05 11:24
devitj23-Aug-05 11:24 
GeneralEmail in C# 2005 (.net 2.0) Pin
Daniel132423-Aug-05 11:24
Daniel132423-Aug-05 11:24 
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

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.