Solved

Using a VB based COM object from VC++

Posted on 1998-11-24
4
229 Views
Last Modified: 2013-11-25
I have a COM object available as a DLL.  The COM is authored in VB.  How do I incorporate this COM object into VC++?  
My code looks something like this:

struct IDB: public IUnknown
{
   STDMETHOD_(void, OpenDB) () = 0;
   STDMETHOD_(char*, ErrorMsgOut) () = 0;
};

static const IID IID_IDB =
{ 0x06122e34, 0x82ba, 0x11d2, { 0xbc, 0xc4, 0x00, 0x60, 0x97, 0x50, 0xcb, 0x8d } };

funct(void)
{
   CLSID clsid;
    LPCLASSFACTORY pClf;
    LPUNKNOWN pUnk;
    IDB* pDB;
    HRESULT hr;

    if ((hr = ::CLSIDFromProgID(L"Histo.ClsHisto", &clsid)) != NOERROR) {
        TRACE("unable to find Program ID -- error = %x\n", hr);
        return;
    }
    if ((hr = ::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER,
        NULL, IID_IClassFactory, (void **) &pClf)) != NOERROR) {;
        TRACE("unable to find CLSID -- error = %x\n", hr);
        return;
    }

    pClf->CreateInstance(NULL, IID_IUnknown, (void**) &pUnk);
    pUnk->QueryInterface(IID_IDB, (void**) &pDB); // All three
    pDB->OpenDB();
    char * msg = pDB->ErrorMsgOut();

    pClf->Release();
    pUnk->Release();
    pDB->Release();
 
    AfxMessageBox(msg);
}

Is this the best way to do this?  I get pointers to the interface but the functions (OpenDB, GetMsg) don't execute. I'm not sure I'm using the correct GUID for the interface.  I'm using the ID for the class within the  object.  I'm a little confused on which registry values to use.
Thanks,
Jeff
0
Comment
Question by:sieglej
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1178575
The best way is opening the ClassWizard, selecting 'New Class' and choose 'From TypeLib'. Then, supply your dll as the source for the type library, and VC++ should generate the necessary wrapper classes...
0
 
LVL 86

Expert Comment

by:jkr
ID: 1178576
Ooops, sorry, i have been a bit on the MFC path...
Well, usually VC uses IDispatch, which is usually created like this:

    sc  =   ::CoCreateInstance  (   clsid,  
                                    NULL,
                                    CLSCTX_SERVER,
                                    IID_IDispatch,
                                    ( void**) &pDB
                                );

then, the methods are usually called using the 'Invoke()' method of an IDispatch.
If you have a _real_ COM interface (different from IDispatch), the IDL code could be helpful...
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1178577
Alternatively, you can #import "vbcom.dll" and use the generated classes. The generated classes have member functions you can call that will either use IDispatch or vtable methods.
0
 

Author Comment

by:sieglej
ID: 1178578
Do you have an example of using #import in VC++?  All I have provided to me is a DLL file.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now