如何从VC ++ .EXE应用程序调用COM DLL(在VC ++中)的函数? [英] How to call functions of a COM DLL (In VC++) from a VC++ .EXE application?

查看:186
本文介绍了如何从VC ++ .EXE应用程序调用COM DLL(在VC ++中)的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个COM DLL(说,xyz.dll)在VC ++编码。我想创建一个调用EXE应用程序(calling.exe),它将调用COM DLL(xyz.dll)的功能。



我按照链接中的步骤 http://www.codeproject.com/kb/DLL/XDllPt1.aspx
但是我不能连接DLL和EXE,因此不能调用COM DLL的功能。我完全是新的COM和VC ++编程。任何人都可以帮助我。



我使用Visual Studio 2005。



我遵循的确切步骤--------



STEP 1 :创建了一个具有DLL项目.dll)项目和调用者应用程序Project(calling.exe)模板MFC应用程序(基于对话框)。将此call.exe作为启动项目。



步骤2:右键单击Calls.exe项目解决方案探索。 配置属性 - > C / C ++ - >常规 - >其他包含指令添加了DLL项目的路径。



步骤3:再次右键单击calling.exe应用程序项目转到属性 - >配置属性 - >链接器 - >输入 - >其他依赖添加了用于构建的DLL Project的.Lib文件的路径。



strong> * 右键单击calling.exe应用程序项目 *,属性 - >常用属性 - >引用 - >添加对DLL的引用

步骤5: * 将xyz.dll文件复制到应用程序项目目录。 *



步骤6:我的DLL有许多头文件及其相应的源文件。所以,将DLL项目中的所有头文件添加到我的calling.exe应用程序。在call.exe应用程序的.CPP程序之一中的OnInitDialog()函数中,我调用了DLL的函数。



只是语句



Cx objname;
objname.func();



这里Cx是DLL中的类名。



我没有对EXISTING DLL项目的配置设置做任何更改,因为它是已经由专家准备的DLL,我只写调用applaction来调用此DLL中存在的函数。



预先感谢。

解决方案

您所遵循的说明是用于在普通DLL而不是COM DLL中调用函数。要访问COM DLL,您需要通过COM。



您不能链接到DLL的lib文件或包含任何头文件,并且您不需要移动



首先,确保DLL通过运行regsvr32注册。

  regsvr32c:\ ... \..\xyz.dll;插入正确的路径

然后在项目的stdafx.h中添加一个#import指令,

  #importc:\..\..\xyz.dll//插入正确的路径

在文件视图中右键单击stdafx.cpp并选择 compile
这将生成您需要访问您的DLL的包装器智能指针类。
智能指针类与DLL中的接口具有相同的名称,但在末尾有Ptr。



用a查看文件。 tlh扩展名和与您的Debug目录中的DLL相同的名称。它从C ++命名空间声明开始。
这是您要从DLL创建的对象所在的命名空间。



假设命名空间是 XYZ 您想要实例化一个 Cx 对象,该对象暴露了 Ix 接口。
你会这样做:

  try {
XYZ :: IxPtr obj;
obj.CreateInstance(__ uuidof(XYZ :: Cx));
obj-> func();
} catch(_com_error e){
printf(Error:%S \\\
,e.Description());
printf(Error:%S \\\
,e.ErrorMessage());
}

然后,您可以继续使用它,就像一个普通的指针。
当你完成它,你不会删除它,但会超出范围后自动销毁。


I have a COM DLL (say, xyz.dll) coded in VC++. I want to create a calling EXE application (calling.exe) which will call the functions of the COM DLL (xyz.dll).

I followed the steps in the link http://www.codeproject.com/kb/DLL/XDllPt1.aspx. But I am not able to connect the DLL and EXE and hence not able to call the functions of the COM DLL. I am totally new to COM and VC++ programming. Can anyone kindly help me with.

I am using Visual Studio 2005.

These are the exact steps I followed--------

STEP 1: Created a solution having the DLL project (xyz.dll) project and a caller application Project (calling.exe) of template MFC Application (Dialog based). Made this calling.exe as the startup project..

STEP 2: Went to the properties by right clicking on the calling.exe Project in solution explorer. Configuration properties --> C/C++ --> General--> Additional Include Directives and added the path to the DLL Project..

Step 3: Again Right Click on the calling.exe application Project went to Properties--> Configuration properties --> Linker --> Input --> Additional Dependencies and added the path to the .Lib file for the built DLL Project.

STEP 4: *Right click on calling.exe application Project*, Properties --> Common Properties --> References --> Added reference to the DLL.

STEP 5: *Copied the xyz.dll file to the application project directory.*

STEP 6: My DLL has many header files and its corresponding source files. So, Added all the header files present in the DLL Project to my calling.exe application program. Within the OnInitDialog() function present in one of the .CPP program of the calling.exe application, I called the functions of DLL.

Just the statements

Cx objname; objname.func();

Here Cx is the name of the class in the DLL.

I did not do any changes with the configuration settings of the EXISTING DLL project because it is The DLL which is already prepared by an expert and I am writing just the calling applaction to call the functions present in this DLL.

THANKS IN ADVANCE.

解决方案

The instructions you've followed are for calling functions in an ordinary DLL, not a COM DLL. To access a COM DLL you need to go through COM.

You don't link to the DLL's lib file or include any headers, and you don't need to move the DLL.

First, make sure the DLL is registered by running regsvr32 on it.

regsvr32 "c:\..\..\xyz.dll" ; insert the correct path

Then add an #import directive to your project's stdafx.h, containing the path to the DLL.

#import "c:\..\..\xyz.dll" // insert the correct path

Right click stdafx.cpp in the file view and choose compile. This will generate the wrapper "smart pointer" classes you need to access your DLL. The smart pointer classes have the same names as the interfaces in your DLL, but with "Ptr" on the end.

Look at the file with a .tlh extension and the same name as your DLL in your Debug directory. It begins with a C++ namespace declaration. This is the namespace in which the objects you are going to create from the DLL reside.

Say the namespace is XYZ and you want to instantiate a Cx object, which exposes the Ix interface. You would do:

try {
    XYZ::IxPtr obj;
    obj.CreateInstance(__uuidof(XYZ::Cx));
    obj->func();
} catch (_com_error e) {
    printf("Error: %S\n", e.Description());
    printf("Error: %S\n", e.ErrorMessage());
}

You can then continue to use it just like an ordinary pointer. You don't delete it when you have finished with it though, it will be destroyed automatically when it goes out of scope.

这篇关于如何从VC ++ .EXE应用程序调用COM DLL(在VC ++中)的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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