开发者

Marshal ByRef Variable-Length Array from COM to C#

I am having trouble getting the managed sig correct for this COM interface any suggestions?

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


You need to marshal the string array yourself. The declaration should look like this:

[PreserveSig]
int QueryVolumeGuidPathNames(out IntPtr pathArray, out uint numberOfPaths);

And the code ought to resemble this:

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);

Untested of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜