C# call WinApi?

前端 未结 2 772
粉色の甜心
粉色の甜心 2021-01-21 17:32

I am trying to call a WinAPI function DeviceIoControl in C# with code IOCTL_DISK_SET_DISK_ATTRIBUTES and pass struct SET_DISK_ATTRIBUTES. I am trying do it with thi

2条回答
  •  天涯浪人
    2021-01-21 18:25

    The original definition of SET_DISK_ATTRIBUTES:

    typedef struct _SET_DISK_ATTRIBUTES {
      DWORD     Version;
      BOOLEAN   Persist;
      BYTE      Reserved1[3];
      DWORDLONG Attributes;
      DWORDLONG AttributesMask;
      DWORD     Reserved2[4];
    } SET_DISK_ATTRIBUTES, *PSET_DISK_ATTRIBUTES;
    

    makes use of BOOLEAN data type, which is defined as a synonym of unsigned char (1 byte), as opposed to BOOL that is a synonym of int (4 bytes).

    C#'s bool is marshaled as BOOL by default.
    You need to force it into one byte:

    {
        ...
        [MarshalAs(UnmanagedType.I1)]
        public bool Persist;
        ...
    }
    

提交回复
热议问题