C++
  Home arrow C++ arrow Page 3 - Your First COM Client In Visual C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Your First COM Client In Visual C++
By: Neville Mehta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2002-08-04

    Table of Contents:
  • Your First COM Client In Visual C++
  • Our Project
  • Steps 3 to 7
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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");
    return;
    }


    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("");

    GetDlgItemText(IDC_CARDNO,cardnumber);
    GetDlgItemText(IDC_CARDNO,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");
    }
    else
    {
    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.

    CoUninitialize();

    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.

    More C++ Articles
    More By Neville Mehta


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT