Convert c struct to c # from dll

I am using a dll written in c. I have imported all the functions that I need into my C # program. Most of the functions I can work fine, but I have problems with several. The functions I come across require a structure entry. I assume that I need to define this structure in C #, which I did, but I am starting to confuse myself, so I will leave everything I tried. The line is quite long, so I will just look like this: c:

typedef struct chain_link_info
{
  unsigned short command;
  unsigned long* buff_id;
  FLAGS_TYPES    flags;  // this is a union that i will list below
} CHAIN_LINK_INFO;

typedef union flags_type
{
  unsigned long ulong;

  struct
  {
    unsigned short std_flags;
    unsigned short high
  } ushort;

  struct
  {
    unsigned int a : 1;
    unsinged int b : 1;
    unsinged int c : 1;
    unsinged int d : 1;
    unsinged int e : 2;
    unsinged int f : 1;
    unsinged int g : 1;
    unsinged int h : 1;
    unsinged int i : 1;
    unsinged int j : 1;
    unsinged int k : 1;
    unsinged int l : 1;
    unsinged int m : 1;
    unsinged int n : 1;
    unsinged int o : 1;
    unsigned int high_word :16
  } std_bits;
} FLAGS_TYPE;

What is the correct way to define these columns in C #? Thanks you

+3
source share
3 answers

#, [StructLayout] , C. FLAGS_TYPE [FieldOffset] . , [FieldOffset (0)].

UPDATE: , , , . 32- , .

+1

C , . , , 8 . [StructLayout (LayoutKind.Explicit)] [FieldOffset (x)] . unsigned long, std_flags a 0. b 1. high high_word 2.

+1

FieldOffsetis a byte block. So, you can express upto byte only through FieldOffset. You can create your own FieldOffset.

0
source

Source: https://habr.com/ru/post/1782489/


All Articles