Equivalent of C++ reference type in C#
Question
I am using a dll written in C++ in a C# application. What is the equivalent for
char const *
unsigned short
in C#
thanks
Solution
A
char*
in C++ can have different meanings, e.g. a pointer to an array of bytes or an ANSI encoded null-terminated string. So it depends on the meaning of your data how the value can be marshaled to C#. The only answer that is definitively not wrong is: it's anIntPtr
.A
unsigned short
in C++ is usually a 16-bit unsigned integer:UInt16
(orushort
in C#).
OTHER TIPS
I looked at the code for your question and I think I got it worded better (check the intent). If so, you are looking for:
- Either
string
orbyte[]
, depending on how the variable is used in the C code. ushort
, assumingunsigned short
produced by your C compiler is 16 bits. In C#,ushort
is always 16 bits (anduint
is always 32 bits). Congrats to MS for finally giving us some consistency here. :)
To call a method exposed via an DLLImport, you'll want to use the IntPtr type for pointers.
Remember in C++ char* is actually a pointer to memory, usually represented by a 4 byte int.