什么是C#相当于这些C ++结构的 [英] What are the C# equivalent of these C++ structs
问题描述
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;
什么是C#相当于这些C ++结构的?
What are the C# equivalent of these C++ structs?
我从迁移C ++一些代码,C#,不得不迁移这些结构问题。我曾尝试了一些东西,但我总是最后不得不编组的问题。
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.
推荐答案
我假设的'炭'被用作一个8位数字,如果是的话,那么这里有你重新映射:
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)
通过这个信息应该是比较容易翻译那些strucutres(只要确保你让他们为结构,并给它属性[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential) ,这将确保编组保持在同一顺序的所有数据。
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.
另外,我建议翻翻两个类和属性System.Runtime.InteropServices为他们提供自动化数据,C / C ++代码(它不要求不安全的C#代码其一)的封送处理相当多的方法。
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).
这篇关于什么是C#相当于这些C ++结构的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!