Brain2000
asked on
Calling COM Objects In C++ Using CoCreateInstance(...)
Variants of this question have been asked several times. How do you call functions from a COM object created with Visual Basic (or anything else for that matter) from C++?
Well, it's not too difficult if you implement MFC and use InvokeHelper(...) and CreateControl(...) functions. However, I'm trying to do it using straight C++ without MFC. The really tough part is I don't have a header file either, so I don't even have an abstact class to work with!
Here's the equivalent of what I'm trying to do in VB:
Dim myobj As VicNet.VicNetCtl
Set myobj = GetObject(,"VicNet.VicNetC tl")
myobj.Function1("abc")
Pretty simple, eh? Create a control called "VicNet.VicNetCtl" and then call a function that it exposes. Now, how do you do it in C++ without MFC or a header file with an abstract class definition?
Here's what I have so far:
void main(int argc,char *argv[])
{
CLSID vicnetCLSID;
IDispatch *myobj;
CoInitialize(NULL);
CLSIDFromProgID(L"VicNet.V icNetCtl", &vicnetCLS ID);
CoCreateInstance(vicnetCLS ID,NULL,CL SCTX_INPRO C_SERVER,I ID_IDispat ch,(void **)&myobj);
myobj->Function1("abc");
CoUninitialize();
}
Of course, the first thing that breaks is IID_IDispatch is not declared. So perhaps I should use IID_IUnknown? Also, our pointer myobj doesn't know about Function1(). So how do I call the function?
Maybe I'm missing the obvious, but I can't seem to think right now.
Well, it's not too difficult if you implement MFC and use InvokeHelper(...) and CreateControl(...) functions. However, I'm trying to do it using straight C++ without MFC. The really tough part is I don't have a header file either, so I don't even have an abstact class to work with!
Here's the equivalent of what I'm trying to do in VB:
Dim myobj As VicNet.VicNetCtl
Set myobj = GetObject(,"VicNet.VicNetC
myobj.Function1("abc")
Pretty simple, eh? Create a control called "VicNet.VicNetCtl" and then call a function that it exposes. Now, how do you do it in C++ without MFC or a header file with an abstract class definition?
Here's what I have so far:
void main(int argc,char *argv[])
{
CLSID vicnetCLSID;
IDispatch *myobj;
CoInitialize(NULL);
CLSIDFromProgID(L"VicNet.V
CoCreateInstance(vicnetCLS
myobj->Function1("abc");
CoUninitialize();
}
Of course, the first thing that breaks is IID_IDispatch is not declared. So perhaps I should use IID_IUnknown? Also, our pointer myobj doesn't know about Function1(). So how do I call the function?
Maybe I'm missing the obvious, but I can't seem to think right now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oooh... oooh wait a minute... I think I've almost got it. The IID_ITheServer UUID is generated using mktyplib.exe from the .ODL file. That's just fine and dandy except for one thing. The OLE/COM viewer creates an .IDL file and when I run it through, it gives me syntax errors. How do you compile an .IDL so you get your header files with your IID_Ixxxxxx defines?
ASKER
Aha! It seems that MKTYPLIB compiles .ODL files and MIDL compiles .IDL files. The zen is starting to become clearer to me. I'm having issues compiling the file because some of the libraries that it is importing seem to be missing some dependancies.
ASKER
Looking into this problem further, it seems that there is some bugs in Microsoft's Word 97 files when generating .IDL's from them. I tried this on someone else's system with Office 97 and MSDEV 6, and got the same problems in the same places. I guess it's time to call MS and find out what they were thinking.
After I get the .IDL compiled and generate my header files, if I have any problems calling a function, can I e-mail you directly to get your opinion?
After I get the .IDL compiled and generate my header files, if I have any problems calling a function, can I e-mail you directly to get your opinion?
ASKER