Составная структура и объединение из C в C # - PullRequest
0 голосов
/ 11 октября 2019

Код C:

struct WbemQualifier {
    CIMSTRING name;/* [relative,charset(UTF16),string] */
    uint8_t flavors;
    enum CIMTYPE_ENUMERATION cimtype;
    union CIMVAR value;/* [switch_is(cimtype)] */
}/* [public,nopull,nopush] */;

struct WbemQualifiers {
    uint32_t count;
    struct WbemQualifier **item;/* [ref] */
}/* [public,nopull,nopush] */;

struct WbemPropertyDesc {
    uint32_t cimtype;
    uint16_t nr;
    uint32_t offset;
    uint32_t depth;
    struct WbemQualifiers qualifiers;
}/* [public] */;

struct WbemProperty {
    CIMSTRING name;/* [relative,charset(UTF16),string] */
    struct WbemPropertyDesc *desc;/* [relative] */
}/* [public] */;

struct CIMSTRINGS {
    uint32_t count;
    CIMSTRING *item;/* [string,charset(UTF16),ref] */
}/* [nopush,nopull,public] */;

/* bitmap DEFAULT_FLAGS */
#define DEFAULT_FLAG_EMPTY ( 1 )
#define DEFAULT_FLAG_INHERITED ( 2 )

struct WbemClass {
    uint8_t u_0;
    CIMSTRING __CLASS;/* [relative,null_is_ffffffff,string,charset(UTF16)] */
    uint32_t data_size;
    struct CIMSTRINGS __DERIVATION;
    struct WbemQualifiers qualifiers;
    uint32_t __PROPERTY_COUNT;
    struct WbemProperty *properties;
    uint8_t *default_flags;
    union CIMVAR *default_values;
}/* [public,flag(LIBNDR_FLAG_NOALIGN),noprint,nopull,nopush] */;

struct WbemMethod {
    CIMSTRING name;/* [null_is_ffffffff,string,charset(UTF16),relative] */
    uint32_t u0;
    uint32_t u1;
    struct WbemQualifiers *qualifiers;/* [relative] */
    struct WbemClassObject *in;/* [relative,subcontext(4)] */
    struct WbemClassObject *out;/* [relative,subcontext(4)] */
}/* [public] */;

struct WbemMethods {
    uint16_t count;
    uint16_t u0;
    struct WbemMethod *method;
}/* [public,flag(LIBNDR_FLAG_NOALIGN)] */;

struct WbemInstance {
    uint8_t u1_0;
    CIMSTRING __CLASS;/* [charset(UTF16),string,relative] */
    uint8_t *default_flags;/* [unique] */
    union CIMVAR *data;/* [unique] */
    uint32_t u2_4;
    uint8_t u3_1;
}/* [flag(LIBNDR_FLAG_NOALIGN),public,nopull,noprint,nopush] */;

struct WbemClassObject {
    uint8_t flags;
    CIMSTRING __SERVER;/* [charset(UTF16),string,ref] */
    CIMSTRING __NAMESPACE;/* [ref,string,charset(UTF16)] */
    struct WbemClass *sup_class;/* [unique] */
    struct WbemMethods *sup_methods;/* [unique] */
    struct WbemClass *obj_class;/* [unique] */
    struct WbemMethods *obj_methods;/* [unique] */
    struct WbemInstance *instance;/* [unique] */
}

Код C #:

private enum CIMTYPE_ENUMERATION
{
    CIM_EMPTY = 0,
    CIM_SINT8 = 16,
    CIM_UINT8 = 17,
    CIM_SINT16 = 2,
    CIM_UINT16 = 18,
    CIM_SINT32 = 3,
    CIM_UINT32 = 19,
    CIM_SINT64 = 20,
    CIM_UINT64 = 21,
    CIM_REAL32 = 4,
    CIM_REAL64 = 5,
    CIM_BOOLEAN = 11,
    CIM_STRING = 8,
    CIM_DATETIME = 101,
    CIM_REFERENCE = 102,
    CIM_CHAR16 = 103,
    CIM_OBJECT = 13,
    CIM_FLAG_ARRAY = 0x2000,
    CIM_ARR_SINT8 = CIM_FLAG_ARRAY | CIM_SINT8,
    CIM_ARR_UINT8 = CIM_FLAG_ARRAY | CIM_UINT8,
    CIM_ARR_SINT16 = CIM_FLAG_ARRAY | CIM_SINT16,
    CIM_ARR_UINT16 = CIM_FLAG_ARRAY | CIM_UINT16,
    CIM_ARR_SINT32 = CIM_FLAG_ARRAY | CIM_SINT32,
    CIM_ARR_UINT32 = CIM_FLAG_ARRAY | CIM_UINT32,
    CIM_ARR_SINT64 = CIM_FLAG_ARRAY | CIM_SINT64,
    CIM_ARR_UINT64 = CIM_FLAG_ARRAY | CIM_UINT64,
    CIM_ARR_REAL32 = CIM_FLAG_ARRAY | CIM_REAL32,
    CIM_ARR_REAL64 = CIM_FLAG_ARRAY | CIM_REAL64,
    CIM_ARR_BOOLEAN = CIM_FLAG_ARRAY | CIM_BOOLEAN,
    CIM_ARR_STRING = CIM_FLAG_ARRAY | CIM_STRING,
    CIM_ARR_DATETIME = CIM_FLAG_ARRAY | CIM_DATETIME,
    CIM_ARR_REFERENCE = CIM_FLAG_ARRAY | CIM_REFERENCE,
    CIM_ARR_CHAR16 = CIM_FLAG_ARRAY | CIM_CHAR16,
    CIM_ARR_OBJECT = CIM_FLAG_ARRAY | CIM_OBJECT,
    CIM_ILLEGAL = 0xfff,
    CIM_TYPEMASK = 0x2FFF
};

[StructLayout(LayoutKind.Sequential)]
public struct WbemQualifier
{
    public IntPtr name; /* [relative,string,charset(UTF16)] */
    [MarshalAs(UnmanagedType.U1)] public byte flavors;
    public IntPtr cimType;
    public IntPtr value;/* [switch_is(cimtype)] */
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemQualifiers
{
    [MarshalAs(UnmanagedType.U4)] public uint count;
    public IntPtr item; //WbemQualifier
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemPropertyDesc
{
    [MarshalAs(UnmanagedType.U4)] uint cimtype;
    ushort nr;
    [MarshalAs(UnmanagedType.U4)] uint offset;
    [MarshalAs(UnmanagedType.U4)] uint depth;
    IntPtr qualifiers; //WbemQualifiers
}

[StructLayout(LayoutKind.Sequential)]
struct WbemProperty
{
    public IntPtr name;/* [relative,string,charset(UTF16)] */
    public IntPtr desc; //WbemPropertyDesc
}

[StructLayout(LayoutKind.Auto)]
public struct CIMSTRINGS
{
    [MarshalAs(UnmanagedType.U4)]
    public uint count;
    [MarshalAs(UnmanagedType.ByValArray)]
    public byte[] item; //String
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemMethods
{
    public ushort count;
    public ushort u0;
    public IntPtr method;
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemInstance
{
    public byte u1_0;
    public IntPtr __CLASS;/* [charset(UTF16),string,relative] */
    public IntPtr default_flags;/* [unique] */
    public IntPtr data;/* [unique] */
    public uint u2_4;
    public byte u3_1;
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemClass
{
    [MarshalAs(UnmanagedType.U1)]
    public byte u_0;

    public IntPtr __CLASS;/* [charset(UTF16),relative,string,null_is_ffffffff] */

    [MarshalAs(UnmanagedType.U4)]
    public uint data_size;

    public IntPtr __DERIVATION; //CIMSTRINGS
    public IntPtr qualifiers; //WbemQualifiers

    [MarshalAs(UnmanagedType.U4)]
    public uint __PROPERTY_COUNT;

    public IntPtr properties; //WbemProperty
    public IntPtr default_flags;
    public IntPtr default_values;
}

[StructLayout(LayoutKind.Sequential)]
public struct WbemClassObject
{
    [MarshalAs(UnmanagedType.U1)]
    public byte Flags;
    public IntPtr Server;
    public IntPtr Namespace;
    public IntPtr sup_class;
    public IntPtr sup_methods;
    public IntPtr obj_class; //WbemClass
    public IntPtr obj_methods;
    public IntPtr instance;

    public WbemClassObject(int flag)
    {
        this.Flags = 0;
        this.Server = IntPtr.Zero;
        this.Namespace = IntPtr.Zero;
        this.sup_class = IntPtr.Zero;
        this.sup_methods = IntPtr.Zero;
        this.obj_class = IntPtr.Zero;
        this.obj_methods =IntPtr.Zero;
        this.instance = IntPtr.Zero;
    }
}

Когда я запускаю код C, экземпляр WbemClassObject устанавливается, как показано на рисунке ниже.

enter image description here

Когда я маршал в C #, u_o, __CLASS, datasize членов в obj_class (WbemClass структура) IntPtr идет правильно, но__DERIVATION переменная, которая существует позже члена размера данных, устанавливает ошибку. Точно так же, если вы посмотрите на рисунки, некоторые переменные включают значение других переменных. Интересно ...

enter image description here

enter image description here

Я внедряю в Ubuntu и .Net Core 3.0.

У кого-нибудь есть идеи?

...