CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: Basic question about creating COM objects in C++

  1. #1
    Join Date
    Jun 2000

    Basic question about creating COM objects in C++

    How do I create an instance of a COM object in C++ if I don't know its CLSID or IID at compile time?

    In case I've got my terminology mixed up - which is entirely possible, since I'm somewhat new to COM - I'll try to explain myself again.

    What I mean is, I know how to create an instance of an ATL/COM object that I've written, but it requires me to include the header and .c files from the ATL object. For instance, if I create an ATL/COM object called "Test", then I need to include "Test.h" and "Test_i.c" in my client program, otherwise I get "unresolved external symbol" on the IID and CLSID that I need to create the object with CoCreateInstance.

    How do I create one like in VB, where you don't have to anything about the object in advance? Presumably, if you can read the ID string from a file for VB, then you can read the GUID and interface IDs from a file (or the registry) for VC?

    Also, while I'm thinking about it, if I don't know at compile time what type of interface I'm using, how do I call functions on the interface pointer? Normally I'd have a variable that points to the interface ("ITest* m_pTest"), but whta do I do when I don't know? A "void*" isn't going to work, is it?

    Sorry for being so unfocused, but I can't find *any* examples in MSDN.

    Thanks in advance...


  2. #2
    Join Date
    Nov 1999
    Dresden / Germoney

    Re: Basic question about creating COM objects in C++


    You can open your control (.dll, or .ocx) in OLE Viewer (run OLE Viewer, choose "View Type Library.." from menu), where you can see all Components with their CLSID's implemented in the dll, and all interfaces these components support.

    First, to create the instance you only need the CLSID or a ProgID. More on the IID later.

    The CLSID is the unique identifier of the component. You "nailed it down" when you created the component (probably, the wizard made it for you). After being registered, the Component is always available under this CLSID. The CLSID is actually all you need to know to create an instance.

    CLSID's are not comfortable - esp. to our VBasic friends. That's why, the component may register a ProgID as well. While the CLSID is a 128-Bit number, written down as {AC05D395-9FDB-485c-972C-461BEFE58235}, the ProgID is some text, typically in the format "DBCommands.DBCommands.1". This is the favourite way to create an object in VBasic

    I' don't know the correct syntax, it's something like set x=CreateObject "DBCommands.DBCommands.1"

    Note that CLSID's are guaranteed to be unique, a feature you don't have with ProgID's.


    Interfaces & IID's

    After you created the component, you can start asking for several interfaces supported by this component. Interfaces, again, are identified by GUID's, this time called IID. This works by the magic of the IUnknown interface, which all components must support.

    The client must know the interfaces (i.e. their IID, and their declaration), or else....

    This is where IDispatch comes into play. The IDispatch interface allwos the client to a) ask what methods and properties are supported by the component, and b) call these methods, set and get these properties.
    All scripting languages can use only the IDispatch interface. Similar for VBasic, although it now can work with non-dispatch interfaces as well.

    In VBasic environment, there is a tool "Add ..." (it says "component", or "class", or something), where you can make the declaration of a component known to the VBasic environment.

    Phhh.. I assume this is not all you wanted to know... but tell me first could you follow?


  3. #3
    Join Date
    Apr 2000
    Frederick, Maryland

    Re: Basic question about creating COM objects in C++

    May be i cant understand ur problem correctly. But to create object of COM component without using IID use __uuidof keyword which retrieves the GUID attach to the expression.

    Take a look at the following program

    #include <windows.h>
    #include <iostream.h>
    #include <atlbase.h>
    #include "..\ATL2\ATL2.h"

    int main()
    HRESULT hr = CoInitialize(NULL);

    if (SUCCEEDED(hr))
    IATL2Inter* pIATL2 = NULL;
    hr = CoCreateInstance(__uuidof(ATL2Inter), NULL, CLSCTX_INPROC_SERVER,
    __uuidof(IATL2Inter), reinterpret_cast<void**>(&pIATL2));

    if (SUCCEEDED(hr))
    CComBSTR bstrUser = "Zeeshan";
    CComBSTR bstrPass = "Amjad";

    cout << "Can not get pointer " << endl;


    return 0;

    My component is dialog based which i called from console based application.

    Hope it helps.

  4. #4
    Join Date
    Apr 2001

    Re: Basic question about creating COM objects in C++

    A COM built using ATL wizard can be reused in two different way:
    1) using .H and .C files;
    2) using .TLB file.

    If your COM is supposed to be used from VC++ enviroment you can hold to any of the ways.

    But if you are going to reuse your component from different prog lang or visual enviroment you have to use TLB files. You just import this file, declare your interface and call methods of the interface.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)