Arrays 将ByRef可变长度数组从COM封送到C#

Arrays 将ByRef可变长度数组从COM封送到C#,arrays,com,interface,marshalling,pinvoke,Arrays,Com,Interface,Marshalling,Pinvoke,我很难使这个COM接口的托管sig正确无误。有什么建议吗 MIDL_INTERFACE("6788FAF9-214E-4b85-BA59-266953616E09") IVdsVolumeMF3 : public IUnknown { public: virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE QueryVolumeGuidPathnames( /* [size_is][size_is][string][ou

我很难使这个COM接口的托管sig正确无误。有什么建议吗

MIDL_INTERFACE("6788FAF9-214E-4b85-BA59-266953616E09")
IVdsVolumeMF3 : public IUnknown
{
public:
    virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE QueryVolumeGuidPathnames( 
        /* [size_is][size_is][string][out] */ __RPC__deref_out_ecount_full_opt_string(*pulNumberOfPaths) LPWSTR **pwszPathArray,
        /* [out] */ __RPC__out ULONG *pulNumberOfPaths) = 0;
};

您需要自己封送字符串数组。声明应如下所示:

[PreserveSig]
int QueryVolumeGuidPathNames(out IntPtr pathArray, out uint numberOfPaths);
代码应该类似于:

IntPtr pathPtr;
int count;
var result = new List<string>();
int hr = obj.QueryVolumeGuidPathNames(out pathPtr, out count);
if (hr < 0) throw new COMException("Oops", hr);
for (int ix = 0; ix < count; ++ix) {
    IntPtr strPtr = Marshal.ReadIntPtr(pathPtr, ix * IntPtr.Size);
    result.Add(Marshal.PtrToStringUni(strPtr));
    Marshal.FreeCoTaskMem(strPtr);
}
Marshal.FreeCoTaskMem(pathPtr);
IntPtr路径ptr;
整数计数;
var result=新列表();
int hr=obj.QueryVolumeMeGuidPathNames(out pathPtr,out count);
如果(hr<0)抛出新的COMException(“Oops”,hr);
对于(int-ix=0;ix
当然没有经过测试