Román
Román

Reputation: 1953

How do I convert a string array to a LPCWSTR in .NET to pass to a Win32 API function?

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?

Upvotes: 2

Views: 1673

Answers (1)

P Daddy
P Daddy

Reputation: 29547

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.

Upvotes: 4

Related Questions