在Windows环境中从C或C ++访问COM接口

时间:2021-10-26 05:21:59

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

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

  • How can I access a COM interface from 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

3 个解决方案

#1


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

#2


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对象非常容易。

#3


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

#1


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

#2


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对象非常容易。

#3


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