tags:

views:

183

answers:

3

Hi all

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

+5  A: 
  • 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#).

dtb
LOADERDLL_API int CLoaderDLL::Open ( const char * pDevicePath )
Khayralla
`pDevicePath` looks like it represents a path which would be a string. So I believe this should be marshalled as an `LPStr`: http://msdn.microsoft.com/en-us/library/s9ts558h.aspx
dtb
+2  A: 

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. :)
280Z28
A: 

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.

Alan
You often don't want to use `IntPtr` in your marshalling unless you have to (sometimes imposed by the marshaller, sometimes by the underlying usage in the C code) - whenever `byte[]` or `string` work they are safer and have more meaning.
280Z28
I did not know that. Thanks.
Alan