使用 Visual C++ 声明/定义和实例化 COM 对象?
我需要在 Visual C++ 中实例化 .dll 和本地计算机上的 com 对象,我知道它可以通过使用 CoCreateInstance("clsid") 来完成,但我对声明感到困惑。所以任何人都可以解释所涉及的所有步骤吗? 对于后期绑定和早期绑定
- 是否需要任何导入/包含
- 如何声明 com 对象?
- 创建实例之前所需的任何其他步骤(例如 CoInitialize?)
或提供涉及分步代码的任何具体参考
i need to instantiate com object which is .dll and on local machine in visual c++,i know that it can be done by using CoCreateInstance("clsid") ,but i am confused about declaration.so can anyone explain all steps involved?
for late binding as well as early binding
- is any import/inclusion required
- how to declare com object ?
- any other steps required before createinstance (e.g CoInitialize?)
or provide any specific reference involving step by step code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须调用
CoInitialize
,并且如果初始化成功,不要忘记调用CoUnitialize
。因此,您的代码将具有以下结构:
有关详细信息,请访问 COM 库 然后您应该阅读有关 CoInitialize 和 CoCreateInstance 函数,然后再使用它们。
本教程也可以帮助您: COM 简介 - 它是什么以及如何使用它。
First you have to call
CoInitialize
and don't forget to callCoUnitialize
if initialization was successful.So your code will have the following structure:
For more information visit MSDN. I recommend you to start with The COM Library and then you should read something about CoInitialize and CoCreateInstance functions before you use them.
This tutorial could help you too: Introduction to COM - What It Is and How to Use It.
#import
。如果您使用 #import 导入 typelib,您将使用 Native COM 框架,该框架隔离了一些具体细节,使生活变得更加轻松。在本机 COM 中,类似这样:
LibName::IMyInterfacePtr pInterface;
在原始 C++ 中:
但请参阅上文。
#import
is very much recommended. if you import the typelib with #import, you'll be using the Native COM framework, which isolates some gritty details and makes life generally easier.In Native COM, something like this:
LibName::IMyInterfacePtr pInterface;
In raw C++:
But see above.