如何在纯 C 中使用 IDispatch 调用 COM 对象

发布于 2024-07-23 06:46:09 字数 242 浏览 5 评论 0原文

我需要使用 R 工具(R Windows 统计程序)中包含的 gcc 编译器来编译我的一些代码,问题是我需要在代码中使用 IDispatch 来创建访问 COM 对象的方法,并且gcc 编译器不支持我用来执行此操作的大部分代码,这些代码基本上是 C++ 代码。

所以我的问题是如何在 C 中使用 IDispatch 创建 COM 对象,而不必依赖 MFC、.NET、C#、WTL 或 ATL。 我相信如果我这样做,我将能够毫无问题地编译我的代码。

I need to compile some code of mine using the gcc compiler included in the R tools (R the statistical program for windows), the problem is that I need to use IDispatch in my code to create an access the methods of a COM object, and the gcc compiler doesn't support much of the code that I'm using to do so, which is basically C++ code.

So my question is how can I use IDispatch in C to create the COM object without having to depend on MFC, .NET, C#, WTL, or ATL. I believe that if I do so I will be able to compile my code without any problem.

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

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

发布评论

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

评论(3

小傻瓜 2024-07-30 06:46:09

CodeProject 上有一篇很棒的文章,题为“COM in plain C”。

以下是第 1 部分的链接

在那篇文章和作者的后续文章中,有很多关于在 C 中使用 COM 的非常好的信息(我认为该系列中有 3 或 4 篇文章)。

编辑:
我错了,有8部分!

第 2 部分
第 3 部分
第 4 部分
第 5 部分
第 6 部分
第 7 部分
第 8 部分

There is a great article on CodeProject entitled "COM in plain C".

Here is the link to Part 1.

There is a lot of very good info on working with COM in C in that article and the author's subsequent follow-ups (I think there are 3 or 4 in the series).

Edit:
I was wrong, there are 8 parts!

Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8

清晰传感 2024-07-30 06:46:09

一般来说,C++ IDispatch 接口只是一个函数指针表。
在 C 中,它看起来像这样:

typedef struct {
  HRESULT(*pQueryInterface)(void* this, REFIID riid, void **ppvObject);
  ULONG(*pAddRef)(void* this);
  ULONG(*pRelease)(void* this);
  HRESULT(*pGetTypeInfoCount)(void* this, unsigned int* pctInfo);
  HRESULT(*pGetTypeInfo)(void* this, unsigned int iTInfo,LCID lcid, ITypeInfo** ppTInfo);
  HRESULT(*pGetIDsOfNames)(void* this, REFIID riid, OLECHAR** rgszNames, unsigned int cNames, LCID lcid, DISPID* rgDispId);
 HRESULT(*pInvoke)(void* this, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, unsigned int* puArgErr);
} IDispatch_in_C;

请注意,每个方法都有一个 THIS 指针作为第一个参数,并且您将需要定义更多类型,例如 ITypeInfo、REFIID、DISPID 等。

所以,这是一项艰巨的任务。 但可以用纯 C 创建 C++ 接口。

In general, a C++ IDispatch interface is just a table of function pointers.
In C, it would look something like:

typedef struct {
  HRESULT(*pQueryInterface)(void* this, REFIID riid, void **ppvObject);
  ULONG(*pAddRef)(void* this);
  ULONG(*pRelease)(void* this);
  HRESULT(*pGetTypeInfoCount)(void* this, unsigned int* pctInfo);
  HRESULT(*pGetTypeInfo)(void* this, unsigned int iTInfo,LCID lcid, ITypeInfo** ppTInfo);
  HRESULT(*pGetIDsOfNames)(void* this, REFIID riid, OLECHAR** rgszNames, unsigned int cNames, LCID lcid, DISPID* rgDispId);
 HRESULT(*pInvoke)(void* this, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, unsigned int* puArgErr);
} IDispatch_in_C;

Note that every method has a THIS pointer as the first parameter, and that you will need to define more types, such as ITypeInfo, REFIID, DISPID, etc, etc.

So, its a big task. But it is possible to create C++ interfaces in pure C.

森罗 2024-07-30 06:46:09

您也可以使用 disphelper 库。

Also you can use disphelper library.

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