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.
If I am missing something, or you would like me to include something else, please let me know in the comments below. Thanks! |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() carlo giordano 1 discussion post
|
hi , c++ type ctime how can be converted in c# ?
Aug 31, 2010
• #4
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() cesc2 1 discussion post
|
CTime, CTimeSpan convert C#?
Aug 6, 2012
• #6
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@cesc: Sorry, I'm not familiar with those types, and I've never had to convert them.
Aug 7, 2012
• #7
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Joe Rodriguez 1 discussion post
|
unsigned char c++ convert to c#?
Dec 18, 2013
• #10
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
"unsigned char" is a byte in C#. A C# byte is an unsigned 8bit type, just the unsigned char.
Dec 18, 2013
• #11
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@Jody: Thanks, I must have missed that one. It's on the list now.
May 29, 2015
• #13
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Chas3 1 discussion post
|
How do we get those nasty Timestamps from ULONGLONG?
Jul 13, 2015
• #14
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Was this helpful? (1) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||