How to configure LPSTR ** in .NET?

I have a method in an unmaged COM object that I am trying to sort:

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

But I can not find the right way to infer LPSTR ** elements. It should be a list of items. However, if you try to do something like this:

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

I get only the first letter of the very first element and nothing more.

How can I correctly transfer the LPSTR ** variable?

+5
source share
2 answers

I cannot verify this right now, but the signature should look like this:

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

Of course, this does not help, you can always do manual sorting through the Marshal class (as Sinatr suggested).

0
source

Try the following:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApplication49 { class Program { [StructLayout(LayoutKind.Sequential)] public struct UnmanagedStruct { [MarshalAs(UnmanagedType.ByValArray)] public IntPtr[] listOfStrings; } static void Main(string[] args) { UnmanagedStruct uStruct = new UnmanagedStruct(); IntPtr strPtr = uStruct.listOfStrings[0]; List<string> data = new List<string>(); while (strPtr != IntPtr.Zero) { string readStr = Marshal.PtrToStringAnsi(strPtr); data.Add(readStr); strPtr += readStr.Length; //I think it should be Length + 1 to include '\0' } } } } 
0
source

Source: https://habr.com/ru/post/1266017/


All Articles