如何在我的C ++程序中使用COM DLL [英] How to use COM dll in my C++ program

查看:180
本文介绍了如何在我的C ++程序中使用COM DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的C ++库中使用COM DLL。
我想要的方式是#import的dll的.tlb文件,我做了:

I wish to use a COM dll in my C++ library. The way I figured going about it, is to #import the dll's .tlb file, which I did :

#import "mycom.tlb" no_namespace

问题是,我不知道放在哪里这个声明。应该是在H文件还是CPP文件中?或者可能是stdafx.h文件?
我试着将它放在.cpp文件中,只是为了测试。

The problem is , I don't quite know where to place this declaration. should it be inside the H file or the CPP file? or maybe the stdafx.h file? I tried placing it in the .cpp file , just for testing.

我有此成员声明:

ILogicSecuredPtr m_pbLogic;

(其中ILogicSecured是我在COM dll中使用的接口)

(where ILogicSecured is the interface I want to work with in my COM dll)

然后我添加这个在构造函数中实例化接口:

Then I added this in the constructor to instantiate the interface :

CoInitialize(NULL);
m_pbLogic(__uuidof(LogicSecured)); 

(其中LogicSecured是实现接口的对象的名称)

(where LogicSecured is the name of the object that implements the interface)

在析构函数中,我添加了:

In the destructor I added :

CoUninitialize();

这不会编译,无论我在哪里放置#import声明。
它只是不能识别ILogicSecured对象。
我在H文件中得到这个错误:

This won't compile however, no matter where I try to place the #import declaration. it just doesn't recognize the ILogicSecured object. I get this error in the H file :

Error   2   error C2146: syntax error : missing ';' before identifier 'm_pbLogic'



我还应该提到当I F12(在Visual Studio中)在ILogicSecuredPtr声明,它需要我到tlh文件只是罚款。所以我知道它承认它。

I should also mention that when I F12 (in Visual Studio) on the ILogicSecuredPtr declaration, it takes me to the tlh file just fine. So I know it recognizes it.

我在这里做错了什么?


Roey

Thanks alot. Roey

推荐答案

问题是,当编译器解析.h文件时, $ c> #import 。因为你的项目很小,最好的办法是把 #import 插入 stdafx.h

The problem is that when the compiler parses the .h file it has not seen the #import yet. Since your project is small your best bet is to put #import into stdafx.h.

当你按F12 Visual Studio使用Intellisence数据库信息,形成解析所有的源,可能不同于编译顺序。所以很典型的Intellisence知道什么东西被声明和编译器不能同时编译它。

When you press F12 Visual Studio uses Intellisence database information that is formed parsing all the sources in order that might be different from the compilation order. So it's quite typical to have Intellisence know where something is declared and the compiler to not compile it at the same time.

这篇关于如何在我的C ++程序中使用COM DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆