windows Windows环境下从C或C++访问COM接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/410005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:47:04  来源:igfitidea点击:

Accessing COM interface from C or C++ in Windows environment

c++cwindowscom

提问by Aaron

I'm relatively new to the Component Object Model specification - I have a simple question:

我对组件对象模型规范比较陌生 - 我有一个简单的问题:

  • How can I access a COM interfacefrom a C or C++ application
  • 如何从 C 或 C++ 应用程序 访问COM 接口

For instance, accessing Microsoft Excel COM interface to perform basic operations, without user intervention.

例如,访问 Microsoft Excel COM 接口执行基本操作,无需用户干预。

Kind regards

亲切的问候

采纳答案by cbrulak

Actually, you will need to instantiate the object using the COM interface.

实际上,您需要使用 COM 接口实例化对象。

This is fairly complicated, more than we can just answer here.

这相当复杂,我们在这里无法回答。

here is a good primer: http://www.codeproject.com/KB/COM/comintro.aspx

这是一个很好的入门:http: //www.codeproject.com/KB/COM/comintro.aspx

Another one: http://www.codeguru.com/cpp/com-tech/activex/tutorials/article.php/c5567

另一个:http: //www.codeguru.com/cpp/com-tech/activex/tutorials/article.php/c5567

回答by kenny

I would suggest looking into ATL if you are using C++, and using C++ if using C. It is very easy to use the #import construct to access COM objects in DLLs or EXEs.

如果您使用 C++,我建议查看 ATL,如果使用 C,我建议使用 C++。使用 #import 构造访问 DLL 或 EXE 中的 COM 对象非常容易。

回答by Scott Saad

There is a wonderful article at Microsoft that seems to address this exact question. If you want to stick with straight C++ (i.e. MFC/ATL), you should check out the article as it has loads of sample code:

微软有一篇精彩的文章似乎解决了这个确切的问题。如果你想坚持使用直接的 C++(即 MFC/ATL),你应该查看这篇文章,因为它有大量的示例代码:

How to automate Excel from C++ without using MFC or #import

如何在不使用 MFC 或 #import 的情况下从 C++ 自动化 Excel