Click to See Complete Forum and Search --> : C# and C++ Data Type


rchiu5hk
September 2nd, 2009, 05:18 AM
Now I am writing C sharp programs which will interact the events from application gateway in network socket programming.

The interface in the application gateway is in C++.

Is it possible that I use C Sharp?
Moreover, as to the data types in C++, how to match these in C Sharp?
especially in uint.... in C++, but how about in c sharp??

RaleTheBlade
September 2nd, 2009, 10:28 AM
You will need to use PInvoke in order to invoke C/C++ libraries from C#. The datatypes will be marshaled as needed by the P/Invoke layer but you will still need to have a good idea of what they are in C#.

MSDN on PInvoke
http://msdn.microsoft.com/en-us/library/aa719104%28VS.71%29.aspx (http://msdn.microsoft.com/en-us/library/aa719104%28VS.71%29.aspx)

You may also be able to find more information on it elsewhere. Its pretty easy to do if your not passing complex data structures around. The easiest way Ive found to find a particular size for a datatype in C# is to find its size in C++ using sizeof(), see how many bytes it is, and then use an equivalent in size data type in C#. Just make sure you use the proper signed or unsigned type. Also, bit fields get tricky but you can usually substitute a single System.UInt32 value for them and just set the bits of the UInt32 manually to get what you want. Its sort of tough to explain but if you need help with it just post again :)

Mutant_Fruit
September 2nd, 2009, 11:18 AM
If you're P/Invoking native libraries then bear in mind that the size of those native types can change depending on the platform. So it's not enough to check the sizes on your local machine, you need to know what the machine you'll be operating on is using.