Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call functions of a COM DLL (In VC++) from a VC++ .EXE application?

Tags:

visual-c++

com

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.

like image 642
codeLover Avatar asked Oct 24 '10 12:10

codeLover


1 Answers

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.

like image 59
Martin Broadhurst Avatar answered Sep 24 '22 09:09

Martin Broadhurst