使用 C++ 中的 COM 对象在 C#.NET 中返回对象 []

发布于 2024-08-02 21:08:34 字数 574 浏览 5 评论 0原文

我有一个 COM 对象,我试图从 C++(而不是 .NET)使用它,并且所有示例程序和手册都是假设使用 C#.NET 或 VB.NET 编写的。 COM 对我来说是个新东西,所以我有点不知所措。我在 TLB 上使用 #import,但正在努力处理用作参数的变体。我有一种特殊的方法,根据 C#.NET 中的文档和示例程序,应该返回一个对象[]。然后我应该将该数组中的第一个条目转换为 ControlEvent,然后它告诉我如何处理数组中的其余对象。 C#.NET 示例如下所示:

object [] objEvent = (object []) Ctl.GetEvent();
ControlEvent ev = (ControlEvent) objEvent[0];

在我的例子中,GetEvent 返回一个 _variant_t,我需要知道如何将其转换为 object[] 以便我可以进一步处理。我什至不清楚如何在 C++ 中表达“对象”。我看到 _variant_t 文档向我展示了我可以将变体转换为的一百万种东西,但它们似乎都没有转换为我可以使用的任何东西。我希望得到一些帮助,将上述 C#.NET 代码转换为 Visual C++,

谢谢。

I have a COM object that I'm trying to use from C++ (not .NET), and all of the example programs and manual are written assuming the use of C#.NET or VB.NET. COM is new to me so I'm a bit overwhelmed. I'm using #import on the TLB but am struggling to deal with the variants that are used as parameters. I have one particular method, that according to the docs and the example programs in C#.NET, is supposed to return an object[]. Then I'm supposed to cast the first entry in this array to a ControlEvent which then tells me what to do with the rest of the objects in the array. The C#.NET example looks like:

object [] objEvent = (object []) Ctl.GetEvent();
ControlEvent ev = (ControlEvent) objEvent[0];

In my case, GetEvent is returning me a _variant_t and I need to know how to convert this to an object[] so that I can further process. Its not clear to me even how I express 'object' in C++. I see _variant_t documentation showing me a million things I can convert the variant to, but none of them seem to be converting to anything I can use. I'm hoping for some assistance converting the above C#.NET code to Visual C++

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2024-08-09 21:08:34

通常,您会查看变体的 vt 成员来了解它实际上是什么类型。在这种情况下,我希望它是一个数组,因此您会期望 vartype 是 VT_ARRAY 的某种变体(通常它与成员的类型进行按位或运算)。然后,您将获得包含实际保存数组的 SAFEARRAY 实例的 parray 成员,并使用普通的安全数组函数从数组中获取数据。

Typically, you look at the vt member of the variant to see what type of thing it actually is. In this case I would expect it to be an array, so you would expect that the vartype would be some variation on VT_ARRAY (usually it is bitwise OR'ed with the type of the members). Then, you get the parray member which contains the SAFEARRAY instance that actually holds the array, and use the normal safe array functions to get the data out of the array.

故事未完 2024-08-09 21:08:34

我还没有这样做,但是通过阅读 _variant_t 类的文档(以及下面纠正我原始帖子的评论),我认为您应该阅读 vt 字段_variant_t 实例的(实际上是 VARIANT 实例的 VARTYPE vt 字段:_variant_t 实例直接派生自 < code>VARIANT) 来查看它包含什么类型的内容,如 VARIANT 结构的参考文档中所述。如果您知道变体中包含什么类型的事物,请使用相应的特定于类型的运算符来读取它。

如果您在不了解 COM 的情况下尝试使用 COM,您将会受到一些伤害(您可能需要一本描述这一点的书);例如,您可能很需要了解 IUnknown 接口和 AddRef 方法。

I haven't done this, but from reading the documentation for the _variant_t class (and the comments below which corrected my original post), I think you should read the vt field of the _variant_t instance (actually the VARTYPE vt field of the VARIANT instance: the _variant_t instance directly derives from VARIANT) to see what type of thing it contains, as described in the reference documentation for the VARIANT struct. One you know what type of thing is contained in the variant, use the corresponding type-specific operator to read it.

You'll be in for some hurt if you try to use COM without understanding it (and you may want a book which describes that); you may well need to know about the IUnknown interface and the AddRef method, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文