我在unmaged COM对象中有一个方法,我正在尝试使用这个方法:
STDMETHOD(SomeMethod)(LPSTR** items, INT* numOfItems) = 0;但我想不出正确的方法来整理LPSTR**项目。应该是一张物品清单。但是,如果尝试这样做:
[PreserveSig]
int SomeMethod([MarshalAs(UnmanagedType.LPStr)]ref StringBuilder items, ref uint numOfItems);我只收到第一件物品的第一封信,没有别的东西。
如何正确封送LPSTR**变量?
发布于 2017-03-28 15:16:18
我现在不能检查它,但是签名应该如下所示:
[PreserveSig]
int SomeMethod(
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 1)] out string[] items,
out int numOfItems);当然,这是没有帮助的,您可以通过元帅类执行手动编组(如Sinatr所建议的)。
发布于 2017-03-28 15:50:35
试试这个:
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'
}
}
}
}https://stackoverflow.com/questions/43072247
复制相似问题