Search code examples
c#.netmarshalling

How to marshall LPSTR** in .NET?


I have a method in unmaged COM object which I'm trying to marshall:

STDMETHOD(SomeMethod)(LPSTR** items, INT* numOfItems) = 0;

But I can't figure out the right way to marshal out LPSTR** items. It's supposed to be a list of items. However if try to do something like this:

[PreserveSig] 
int SomeMethod([MarshalAs(UnmanagedType.LPStr)]ref StringBuilder items, ref uint numOfItems);

I only get the very first letter of the very first item and nothing else.

How can I marshal LPSTR** variable correctly?


Solution

  • I cannot check it right now, but signature should look like this:

    [PreserveSig]
    int SomeMethod(
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 1)] out string[] items,
        out int numOfItems);
    

    Of course, it is doesn't help, you can always perform manual marshalling via Marshal class (as Sinatr suggested).