Porting from C++, p/Invoke with structs, SendMessage() and WM_COPYDATA

A

alexwhitman

Hi,

I'm trying to port a c++ class to c# and in the c++ class I have a
struct defined as:
struct SNARLSTRUCT {
SNARL_COMMANDS cmd;
long id;
long timeout;
long lngData2;
char title[SNARL_STRING_LENGTH];
char text[SNARL_STRING_LENGTH];
char icon[SNARL_STRING_LENGTH];
};

SNARL_COMMANDS is just an enum and SNARL_STRING_LENGTH in this case is
1024.

I've recreated the struct in c# as:

[StructLayout(LayoutKind.Sequential)]
public struct SNARLSTRUCT {
public SNARL_COMMANDS cmd;
public long id;
public long timeout;
public long lngData2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=SNARL_STRING_LENGTH)]
public byte[] title;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=SNARL_STRING_LENGTH)]
public byte[] text;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=SNARL_STRING_LENGTH)]
public byte[] icon;
};

First of all, is this correct?
If it is, how do I go about moving data from a string to the byte
arrays and make sure they're a valid c-string representation.

Secondly, once this is correct and working, how do I use the struct as
the data part of a COPYDATASTRUCT to be sent to another window via
SendMessage() with the WM_COPYDATA message? I've seen lots of
examples on how to send simple types such as ints and strings, but not
structs.

Examples would be great (I'm a learn by example person) but pointing
to relevant docs would also be helpful.

Thanks,
Alex.
 
P

pfc_sadr

why in the hell would you wat to keep C code?

why don't you just stick a big fat dildo up your ass, run around on
the streets and say 'look at me look at me I can take a pointer in my
ass'



'
 

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