Home arrow C++ arrow Page 3 - 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++ - Steps 3 to 7
(Page 3 of 4 )

3. Initalize the COM Library

Before actually using the COM component we have to initialize the COM library. Use the CoInitialize function to do so. This method returns a variable of HRESULT type.

HRESULT hr = CoInitialize(NULL);

4. Retrieve the CLSID of the Component

We can't possibly remember the CLSID of each component, so the COM library gives us a method called CLSIDFromProgID. This method takes the Program ID as the parameter (which is readable and easily remembered) and gives us back an output parameter, which contains the CLSID. This method returns a variable of HRESULT type:

CLSID clsid;
hr = CLSIDFromProgID(OLESTR("ValidateCard.Validate"), &clsid);

The Program ID consists of the ProjectName and the Class Name separated by a dot. Also note that the first parameter in the method expects an OLESTR string thus we type cast our string to the OLESTR type.

5. Create an instance of the component

After we know the components CLSID we actually go onto creating an object of the COM component. If you have read my COM article then you should know that the only way to access components are by way of their interface. We have a method called CoCreateInstance(), which accepts 4 input parameters and returns an output parameter which is the interface pointer of the COM component. This method returns a variable of HRESULT type.

_Validate *valid;

Firstly we declare an interface pointer. Mind you, the interface name should be an underscore followed by the class name. If you try to make an object of the interface pointer using some other interface name you would get a compile time error. The pointer name can be whatever you like.

hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(_Validate), (LPVOID *)&valid);

Next, we call the CoCreateInstance method which takes the class ID as its first parameter. The next parameter tells the method if your component supports aggregation or not. Our component does not so we leave it as NULL. The third parameter tells the method what kind of component we are going to create an instance of. There are three options: an in process server (CLSCTX_INPROC_SERVER in that case), an out process server (CLSCTX_OUTPROC_SERVER) or a remote server (CLSCTX_REMOTE_SERVER). In our case it's an in process server, or CLSCTX_INPROC_SERVER.

The fourth parameter asks for the interface id. We know the interface name is an underscore followed by the class name, right? We have a method called __uuidof(). This method takes the interface name as its parameter and returns the interface id. We just make a call to that method and give the method what it requires -- the interface ID. The fifth parameter is an output parameter. It is of LPVOID pointer type. Our pointer thus has to be typecast to LPVOID *.

if (FAILED(hr))
AfxMessageBox("Server Creation Failed");

Add this code to catch any errors. If the creation of the component fails, then the hr variable of HRESULT data type would contain either the value S_OK or the value S_FALSE. If the creation of our component was successful, then the hr value passed to the FAILED method would return TRUE, otherwise it would return FALSE. We also use the return statement to stop the rest of the execution if the server creation if failed.

6. Using the COM Object

If everything has gone well then you have the COM components interface pointer with you. Before you call any method in the component, let's first trap what the user has entered in the edit boxes. We have a method called GetDlgItemText. This method takes the control ID as the first parameter and an output parameter where the value of the text field is actually stored. CString is just similar to a String object, which can store an alphanumerical value. The constructor of this String class takes what string the object should hold initially:

CString cardnumber("");
CString cardtype("");


Before we pass any string to the COM server we have to convert it to _bstr_t type. This is the binary string type. The constructor of this binary string class takes what string the object should hold initially:

_bstr_t cardnum(cardnumber);
_bstr_t cardtyp(cardtype);

We are now all set to call the COM server method. We just use the interface pointer to simply call the methods in the Dynamic Link Library (dll).

long validity = valid->ValidateCard(cardnum, cardtyp);

We call the ValidateCard method in the COM server which will return a long value. It takes two string parameters, which we pass in the form on binary strings.

Depending on what value the method returns back to us, we display the appropriate messages to the user. Remember, the method returns "1" if the card is valid and "0" if its invalid:

if (validity == 1)
AfxMessageBox("Valid Card");
AfxMessageBox("Invalid Card");

To display a message box to the user we calle the AfxMessageBox method. It takes a string as its parameter, and this string is displayed as the content of the message box.

7. Uninitialize the COM Libarary

Lets not forget to uninitialize the COM library, just as we initialized it in step 3. It's always a good practice to do so. This method takes no parameter and also returns nothing.


After following these 7 steps, you should have a fully working COM client that uses a DLL created in Visual Basic to perform some actions using the methods defined in that DLL.
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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials