Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

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!

Apr 12, 2010 (modified Mar 17, 2016)  • #1
User Image
billy.oneal
3 discussion posts
Just an FYI: Those are *not* C or C++ datatypes, they are Win32 API datatypes. Standard C++ does not define any of what you have above excepting `void *`.

Have a nice day :)
May 3, 2010  • #2
User Image
Xenovore
1 discussion post
Good list! (Yeah, there are other types but who uses those...)

Maybe add these though...? C++ has had a native boolean type "bool" for a while now, as well as a floating point types "float" and "double". (Never have understood why they typedef'ed those; I guess they wanted ALL their types to be uppercase...)

@Billy: This _is_ C# interop (i.e. Windows!) we're talking about, so naturally you're dealing with Win32 data types, that are typedef'ed in C++ and native in C#. Thanks though for pointing out the obvious! =P
Jun 28, 2010  • #3
User Image
carlo giordano
1 discussion post
hi , c++ type ctime how can be converted in c# ?
Aug 31, 2010  • #4
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
@Carlo: It looks like ctime is just an int64:
http://bytes.com/topic/c-sharp/answers/259755-convert-c-ctime-c-datetime
Aug 31, 2010  • #5
User Image
cesc2
1 discussion post
CTime, CTimeSpan convert C#?
Aug 6, 2012  • #6
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
@cesc: Sorry, I'm not familiar with those types, and I've never had to convert them. :(
Aug 7, 2012  • #7
User Image
Ronald Chua
1 discussion post
What about strings? I'm trying to convert System.String of C# to char* or unsigned char* in C++. But it was indicated that char* is SByte. I've tried char variable[], std::string, unsigned char*, and none of them worked. I tried converting the System.String to SByte using Convert static method, but then another error shows up, saying SByte can't be converted to SByte*.

Hope to get a response.
Dec 9, 2012  • #8
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
Strings can be very tricky! I would recommend just using IntPtr for the parameter while trying to figure it out. Once you get the IntPtr value use Marshal.PtrToStr functions to read the string value. I find this is more reliable than relying on the p/invoke marshalling in some situations. I hope this helps! :)
Dec 10, 2012  • #9
User Image
Joe Rodriguez
1 discussion post
unsigned char c++ convert to c#?
Dec 18, 2013  • #10
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
"unsigned char" is a byte in C#. A C# byte is an unsigned 8bit type, just the unsigned char.
Dec 18, 2013  • #11
User Image
Jody McAdams
1 discussion post
You're missing WORD. You have DWORD but no WORD. WORD is same as your listing for UINT16 so in C# it would be Uint16 or ushort.
May 28, 2015  • #12
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
@Jody: Thanks, I must have missed that one. It's on the list now. :)
May 29, 2015  • #13
User Image
Chas3
1 discussion post
How do we get those nasty Timestamps from ULONGLONG?
Jul 13, 2015  • #14
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
I'm not sure which timestamp type you're working with exactly, but this post has info on converting from a ULONGLONG to a FILETIME struct:
http://stackoverflow.com/questions/4570194/log-with-timestamps-that-have-millisecond-accuracy-resolution-in-windows-c
Jul 13, 2015  • #15
User Image
Suresh7
1 discussion post
Hi,
I have c++ code as below
typedef char MyChar;
public struct OutParameter
{
public MyChar Status;
}

The above c++ struct is returned by reference in function parameter.

Could you please provide me equivalent C# struct .
Nov 13, 2015  • #16
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
@Suresh: You just need to change "public MyChar Status" to "public chat Status" and it should work in C#. You might need to use the "byte" type instead of "char", but you'll have to test that.
Nov 16, 2015  • #17
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(1)  Login to Vote(-)