Home arrow C++ arrow Page 2 - Your First COM Client In Visual C++

Your First COM Client In Visual C++

Interested in making your own COM client in C++ to call a DLL written in Visual Basic? In this article Neville provides us with an excellent example using credit card validation.

Author Info:
By: Neville Mehta
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
August 04, 2002
  1. · Your First COM Client In Visual C++
  2. · Our Project
  3. · Steps 3 to 7
  4. · Conclusion

print this article

Your First COM Client In Visual C++ - Our Project
(Page 2 of 4 )

Before we start creating a client for the COM Dynamic Link Library (dll) let me tell you a bit about its structure. My Dynamic Link Library (dll) is created in VB and contains various methods, which help us check the validity of a credit card number using the common MOD 10 algorithm:

Project Name: ValidateCard.vbp
Our Dynamic Link Library (dll) code would be part of a project.

Class Name: Validate.cls
When we create a COM Dynamic Link Library (dll) in VB a class is automatically created for us where we have to write our code.

DLL Name: ValidateCard.dll
When we complete writing the entire source in our class we compile the class and as a result of that, a Dynamic Link Library (dll) file is created. This file is in true sense our In–process COM component.

Method: ValidateCard (num As String , type As String) As Long
This is the VB function we are going to use to validate a card. It takes in two String parameters, which are the CreditCardNumber and the CreditCardType respectively, and returns back a long integer value. It returns back '1' for Valid cards and '2' for Invalid cards.

Ok, now since you have got a good overview of the COM Dynamic Link Library (dll) we are going to use, let's start creating our client. The Microsoft VC++ interface is quite similar to the VB interface. You have a toolbar with various controls, which you can just drag and drop onto your interface or form. The coding is surely more complicated in VC++, but as far as design is concerned, I think both are quite alike.

Here are the 7 Steps you need to carefully follow in order to create our VC++ client. I will explain each step in detail, giving examples of actual VC++ code:

1. Create An Interface using MFC AppWizard

Open VC++. Select New from the File menu.

Select MFC AppWizard (exe) from the projects tab. Also specify the Project name and Location. Lets assume that the project name will be 'CreditCardClient' and the location will be 'c:\CreditCardClient'. Click on OK when done.

Choose the Dialog Based option where they ask you which type of application would you want to create. Leave the rest as it is and click on Finish. When you do so the wizard will create all the files required to run the (exe) client.

Now add the controls to the form, which is present in front of you. We will make it look like a credit card validation form. Add two textboxes, which would help trap the users entry: one for the card number and the other for the card type. Also add two labels to let the user know what to add in which textbox. Finally, add a button, which is clicked by the client when he finishes entering his data.

Before proceeding let's just right-click on any of the controls and choose properties from the menu that appears. Yes, we have the properties window in front of us where we can change that controls caption, ID and a few other properties. Now, to make your control look meaningful please change all the of captions to appropriate names, as shown in the screenshot below.

Also, let's assume that you enter 'IDC_CARDNO' as the ID for the textbox, which accepts the card number and 'IDC_CARDTYPE' as the ID for the textbox, which accepts the cards type. For all other controls you could add any ID and captions you wish, because they wont be used. Lastly, make sure the ID's are unique for each control.

Our completed form layout

You don't need to be a rocket scientist to work out when the validation should take place. Yes, on the click of the button. So we also have to somehow trap the Click event of the button we have added, right? Just double click on the button you have created and a window will popup, which says 'Add Member Function'. Enter the Member Function Name as 'OnValidate' and click OK.

You suddenly find yourself in the middle of some function somewhere in our C++ code -- that's the Click event function of the button. Whatever you add in this function will execute when the user clicks that button.

2. Import the Dynamic Link Library (dll) file

Click on the FileView tab of the Project Workspace window. Expand the Header Filesfolder and open the StdAfx.h file and add the following line.

#import "D:\Path\Of\dll\ValidateCard.dll"

I've included our modified version of StdAfx.h as part of the support material for this article. Note exactly we have added this line. If this line were added anywhere else in the code then it wouldn't compile.

We use the import keyword followed by the entire path of the Dynamic Link Library (dll) file to add its reference in our code. Expand the source files folder from the FileView tab. We have a file called StdAfx.cpp there. If you happen to view the source for this file, it will contain just one statement, which includes the StdAfx.h header file into it. Yes, our purpose is solved. Just right-click on the StdAfx.cpp file and choose the 'Compile' option.

As soon as we compile that file (successfully if you have followed my instructions) the compiler creates a wrapper class. This wrapper class is going to help us access the functionalities of our COM server. All of this information is present in the type library files created by the compiler. The compiler creates two type library files; one is with the extension of .tlh (which is the type library header file) and the other with the extension of .tli (which is the type library implementation file). The name of the wrapper class in this case would be the same as the class name chosen while creating the COM server. In our case it would be 'Validate'.

In order to access the functionality of the COM server in our client, we have to tell our code that we will be using the compiler generated wrapper class. Choose the source files folder from the FileView Tab. Click on a file called CreditCardClientDlg.cpp. Add this line just before the class definition:

using namespace Validate
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials