typedef union _Value {
signed char c;
unsigned char b;
signed short s;
unsigned short w;
signed long l;
unsigned long u;
float f;
double *d;
char *p;
} Value;
typedef struct _Field {
WORD nFieldId;
BYTE bValueType;
Value Value;
} Field;
typedef struct _Packet {
WORD nMessageType;
WORD nSecurityType;
BYTE bExchangeId;
BYTE bMarketCenter;
int iFieldCount;
char cSymbol[20];
Field FieldArr[1];
} Packet;
What are the C# equivalent of these C++ structs?
I am migrating some code from C++ to C# and having problems to migrate these structures. I had tried a few things but I always ended up having marshalling problems.
noun, plural C's or Cs, c's or cs. the third letter of the English alphabet, a consonant. any spoken sound represented by the letter C or c, as in cat, race, or circle. something having the shape of a C. a written or printed representation of the letter C or c.
C programming has basically two operators which can increment ++ and decrement -- the value of a variable. It can change the value of an operand (constant or variable) by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation.
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.
I'm assuming the 'char' is being used as an 8 bit number, if so, then here are you're mappings:
signed char c; -> SByte c;
unsigned char b; -> Byte b;
signed short s; -> Int16 s;
unsigned short w; -> UInt16 w;
signed long l; -> Int32 l;
unsigned long u; -> UInt32 u;
float f; -> Single f; (though 'float' still works)
double *d; -> Double d; (was this meant to be a pointer???)
char *p; -> String s; (assuming its a string here, in the marshaling you can tell it whether it is ASCII or wide char format)
With this info it should be relatively easy to translate those strucutres (just make sure you keep them as a struct and give it the attribute "[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]", which will make sure that the marshaller keeps all the data in the same order.
Also, I recommend looking through both the classes and attributes in System.Runtime.InteropServices as they do provide quite a few methods for automating marshaling of data to c/c++ code (and it doesn't require "unsafe" c# code either).
Some of the other posts already have great information, I thought I would share a quick tip. I had to go through this kind of issue recently. It may be obvious but if you own the code to both sides of the interface I found that commenting out all but the one fields and making sure that works and then adding them back slowly one by one was a much safer way to get this working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With