Question

I am using a dll written in C++ in a C# application. What is the equivalent for

  1. char const *
  2. unsigned short

in C#

thanks

Was it helpful?

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 an IntPtr.

  • A unsigned short in C++ is usually a 16-bit unsigned integer: UInt16 (or ushort 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:

  1. Either string or byte[], depending on how the variable is used in the C code.
  2. ushort, assuming unsigned short produced by your C compiler is 16 bits. In C#, ushort is always 16 bits (and uint 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top