If you've ever had to write any interop code to use an unmanaged library in your C# application, you know how tricky it can be to get the data types correct. I often find myself scouring the internet looking for the correct conversions, so I thought I would document everything I have learned so far. This is by no means a comprehensive list of all C++ data types, just the ones I find myself frequently writing interop code for.
| C++ Type | C# Type | Size |
|---|---|---|
| BOOL | bool | 1 byte |
| BYTE | byte | 1 byte |
| CHAR | byte | 1 byte |
| DECIMAL | Decimal | 16 bytes |
| DOUBLE | double | 8 bytes |
| DWORD | uint, UInt32 | 4 bytes |
| FLOAT | float, single | 4 bytes |
| INT, signed int | int, Int32 | 4 bytes |
| INT16, signed short int | short, Int16 | 2 bytes |
| INT32, signed int | int, Int32 | 4 bytes |
| INT64 | long, Int64 | 8 bytes |
| LONG | int, Int32 | 4 bytes |
| LONG32, signed int | int, Int32 | 4 bytes |
| LONG64 | long, Int64 | 8 bytes |
| LONGLONG | long, Int64 | 8 bytes |
| SHORT, signed short int | short, Int16 | 2 bytes |
| UCHAR, unsigned char | byte | 1 byte |
| UINT, unsigned int | uint, UInt32 | 4 bytes |
| UINT16, WORD | ushort, UInt16 | 2 bytes |
| UINT32, unsigned int | uint, UInt32 | 4 bytes |
| UINT64 | ulong, UInt64 | 8 bytes |
| ULONG, unsigned long | uint, UInt32 | 4 bytes |
| ULONG32 | uint, UInt32 | 4 bytes |
| ULONG64 | ulong, UInt64 | 8 bytes |
| ULONGLONG | ulong, UInt64 | 8 bytes |
| WORD | ushort | 2 bytes |
| void*, pointers | IntPtr | x86=4 bytes, x64=8 bytes |
If I am missing something, or you would like me to include something else, please let me know in the comments below. Thanks!