Difficult to interop structure RRS feed

  • Question

  • Dear All,

    This is one of the structure that is really hard to interop correctly in csharp. There is no #pragma pack(1). Built under 32 bits environment. Can anyone please help? TIA...

    extern "C"  __declspec(dllexport)
    typedef struct Control
     int Mode;
     char  cInfo[100];
     BYTE  Info;
     char cData[16][16];

    Friday, August 10, 2012 1:06 PM


  • According to this C++ tiny program, the padding bytes (3 bytes) are at the end of the structure.

    #define _WIN32_WINNT _WIN32_WINNT_WIN7
    #define NOMINMAX
    #include <Windows.h>
    #include <iostream>
    typedef struct Control
     int Mode;
     char  cInfo[100];
     BYTE  Info;
     char cData[16][16];
    //Structure to analyze:
    #define OUTPUT_FIELD(f, s) std::wcout << L#f L":  " << reinterpret_cast<UINT>(&s.f) - reinterpret_cast<UINT>(&s) << std::endl
    int main()
        STRUCT_TO_CHECK s;
        std::wcout << L"------- Structure Member Offsets -------" << std::endl;
        OUTPUT_FIELD(Mode, s);
        OUTPUT_FIELD(cInfo, s);
        OUTPUT_FIELD(Info, s);
        OUTPUT_FIELD(cData, s);
        std::wcout << L"Last member's size:  " << sizeof(s.cData) << std::endl;
        std::wcout << L"Structure size:  " << sizeof(s) << std::endl;

    NOTE:  You can reuse this code by modifying the STRUCT_TO_CHECK typedef and then modifying the OUTPUT_FIELD lines with the names of the members of the structure.

    Output in my Windows 7 PC 64-bit:

    ------- Structure Member Offsets -------
    Mode:  0
    cInfo:  4
    Info:  104
    cData:  105
    Last member's size:  256
    Structure size:  364

    Jose R. MCP
    Code Samples

    Friday, August 10, 2012 2:51 PM