Sentinel
Sentinel

Reputation:

P/Invoke for pointer to array of char

I have a DLL that I need to P/Invoke the following method: DWORD Foo( int a, int *b, char *c );

Per the documentation, parameter 'c' is an out parameter that must be a char array of size 16. A null terminated string is placed into it.

What is the P/Invoke definition for parameter 'c' (I've got the others fine)? How is the content of 'c' read after the call?

Upvotes: 0

Views: 2831

Answers (1)

Egor
Egor

Reputation: 1838

I prefer to use StringBuilder for these things actually. It's far easier to deal with than char arrays or immutable strings. Just make sure you initialize it with enough capacity (16 in this case) for the DLL to fill. Typically it marshalls over just fine, but you may have to set the character set in your DLLImport declaration.

Here is a little more info on this: Marshaling between Managed and Unmanaged Code from MSDN magazine (Stringbuilder section)

Upvotes: 1

Related Questions