views:

763

answers:

1

Hi all

I have a piece of C# code that needs to convert a string array to a LPCWSTR to pass to a Win32 API function. I can't find nothing in the Marshal class that makes it straightforward. Does anybody knows how to do that?

+2  A: 

You should declare the API function as taking a string array. Since it's declared as constant, you should add an In attribute so that it's not marshalled back after the call. If the function truly only has a unicode version as you've specified (i.e., it takes LPCWSTR and not LPCTSTR), then you should declare it with CharSet=Unicode. Similar to:

[DllImport("kernel32.dll", CharSet=Unicode)]
static extern bool Foo([In] string[] stuff);

What API function are you trying to call? If you post it, I can give you a good P/Invoke signature for it. Or you can check pinvoke.net, which has a pretty good pre-compiled list (community-generated) of P/Invoke signatures.

P Daddy