COM SAFEARRAY的GUID从C ++返回到C# [英] COM SAFEARRAY of GUID's returned from C++ to C#

查看:247
本文介绍了COM SAFEARRAY的GUID从C ++返回到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在运行到需要传递一个SAFEARRAY(GUID)从C ++到C#一个返回值的问题。

I'm currently running into an issue of needing to pass a SAFEARRAY(GUID) as a return value from C++ to C#.

目前的C#侧使用。一个互操作的dll从Tlbimp.exe将(类型库导入)生成的

Currently the C# side is using an Interop dll generated from Tlbimp.exe (Type Library Importer).

IDL是:

HRESULT GetGuids(
    [out]SAFEARRAY(GUID)* guids);



我也试过[出来,RETVAL]

I've also tried [out, retval]

函数签名是:

HRESULT
WINAPI
MyClass::GetGuids(SAFEARRAY** guids)

如果我用 SafeArrayCreate() SafeArrayCreateVector()

SAFEARRAY* psa
psa = SafeArrayCreate(VT_CLSID, 1, rgsabound);



我得到一个空 SAFEARRAY 指针,该指针应该说明 E_OUTOFMEMORY 这是不正确。

I get a NULL SAFEARRAY pointer, which is supposed to indicate E_OUTOFMEMORY which is incorrect.

我发现那是什么 VT_CLSID 仅供奥莱属性集,而不是SAFEARRAY的:
http://poi.apache.org/apidocs/org/apache/poi/hpsf/Variant.html 它表明CLSID为

What I did find was that VT_CLSID is only for Ole property sets and not SAFEARRAY's: http://poi.apache.org/apidocs/org/apache/poi/hpsf/Variant.html Its indicated that CLSID is

我也尝试构建安全阵列的替代方法:
SafeArrayAllocDescriptor() SafeArrayAllocData()

I've also tried the alternate means of constructing the safe array with: SafeArrayAllocDescriptor() and SafeArrayAllocData().

hResult = SafeArrayAllocDescriptor(1, guids)
hResult = SafeArrayAllocData(*guids);

这让我创建磁盘阵列,但 SafeArrayPutElement填充时,() 我得到的一个80070057 HRESULT(参数不正确)。这可能是由于它采用 VT_CLSID 参数以及事实

This lets me create the array, but when populating it with SafeArrayPutElement() I get an HRESULT of 0x80070057 (The parameter is incorrect). This is probably due to the fact it takes the VT_CLSID parameter as well

我可以手动<$填充C $ C> SafeArrayAccessData()

GUID* pData = NULL;
hResult = SafeArrayAccessData(*guids, (void**)&pData);



但我从C#端的错误:
的值,不在范围内在预期的范围

but I get an error from the C# side: "The value does not fall within the expected Range"

我不知道如何完成或者由retval的或out参数返回SAFEARRAY(GUID)到C#中所需的功能。

I'm not sure how to accomplish the desired functionality of returning a SAFEARRAY(GUID) to C# either by a retval or out parameter.

这似乎应该是简单的 - 有在IDL许多领域我已经通过GUID的没有任何UDT的或编组。直到我需要把它们放入一个SAFEARRAY一切工作正常。

It seems it should be simple - there are many areas in the IDL where I'm already passing GUID's without any UDT's or marshalling. Everything works fine until I need to pass them in a SAFEARRAY.

任何帮助表示赞赏,提前
谢谢

Any help is appreciated, Thanks in advance

推荐答案

您是绝对正确的 - 问题是,VT_CLSID没有在任何VARIANT或SAFEARRAY允许的。它归结为GUID不是自动化兼容的类型。

You're absolutely right - the problem is that VT_CLSID isn't allowed in either VARIANT or SAFEARRAY. It boils down to GUID not being an Automation-compatible type.

我经常需要做的,你想同样的事情。解决此问题的最简单的方法是将GUID转换为字符串,然后传递SAFEARRAY(VT_BSTR)。它违背了粮食某种程度上做这种转换,但我想你可以把认为有编组的反正怎么回事,这种转换是一种封送的。

I often need to do the same thing that you're trying. The easiest way around the problem is to convert the GUID to a string and then pass SAFEARRAY(VT_BSTR). It goes against the grain somewhat to do this conversion, but I suppose you could take the view that there's marshaling going on anyway and this conversion is a type of marshaling.

这篇关于COM SAFEARRAY的GUID从C ++返回到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆