• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3790
  • Last Modified:

Using COM with C++ and IDispatch?

Greetings, the following is code that I am using to invoke methods using Automation (Visual Basic Active X exe). The method is called "Start" and is inside an class called pLineControl.

-------------------------------------------------
OLECHAR FAR* szStart = L"Start";
DISPPARAMS dispparams = {NULL, NULL, 0, 0};
VARIANTARG myargs[2];
IDispatch FAR* lpDispatch;
HRESULT hr;
DISPID dispid;
CLSID pClsid;
         
hr = OleInitialize (NULL);
if (hr != S_OK)
 return (-1);
 
if (CLSIDFromProgID (L"Pline.PlineControl",&pClsid) != S_OK)
 return (-1);


hr = CoCreateInstance (pClsid,NULL,CLSCTX_LOCAL_SERVER,
         IID_IDispatch,
         (void FAR* FAR*)lpDispatch );

if (hr != S_OK)
 return (-1);

hr=lpDispatch->GetIDsOfNames(IID_NULL,&szStart, 1,LOCALE_SYSTEM_DEFAULT,&dispid);

if (hr != S_OK)
 return (-1);

    dispparams.rgvarg = &myargs[0];
    dispparams.rgvarg[0].vt = VT_I4;
    dispparams.rgvarg[0].iVal = 12345;
    dispparams.rgvarg[1].vt = VT_I4;
    dispparams.rgvarg[1].iVal = 67890;
    dispparams.cArgs = 2;
    dispparams.cNamedArgs = 0;

hr = lpDispatch->Invoke (dispid,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dispparams,NULL,NULL,NULL);

if (hr != S_OK)
return -1;

     return 0;
-------------------------------------------------------
This code works but I was wondering if there is a better way to do it and also if there is a way to discover the number and type of parameters exposed by a method at run-time? I'm about average when its comes to COM so any help offered will be greatly appreciated. Thanks, SKP.
0
sinister
Asked:
sinister
1 Solution
 
assermarkCommented:
You need to read your typelib to enum parameters. Use LoadTypeLib to get a pointer to ITypeLib, then ITypeLib::GetTypeInfo, ITypeInfo::GetFuncDesc. Use ITypeInfo::GetDocumentation to get function name and FUNCDESC.cParams to obtain number of parameters. FUNCDESC.lprgelemdescParam contains datatype.

//Andreas
0
 
ambienceCommented:
>> if there is a better way to do it and also if there is a way to discover the number and type of parameters exposed by a method at run-time?

Yes definitely, the way you are doing is thru IDispatch, and is called late-binding and your client is called agnostic client, because you determine functions at runtime using GetIDSofName and invoke methods thru IDispatch table. All scripting languages work on this principle.

The other approach is early-binding and uses dual interfaces, (only possible from languages that can use pointers), you directly bind to the function pointer and invoke the function thru that pointer , that is possible  if only you have access to the type-library of the object. You can import that typelibrary using #import and that would create wrapper classes for your object, so you can directly call

pObj->Start(12345, 67890);

if its a dll, you can directly import the dll and no need to find a .tlb file, but for exe servers it might be required though i havent tried import an exe directly (maybe it works).

Secondly as assermark has mentioned it is possible to determine at runtime all the information from a typeibrary, All scripting languages do that (ever wonder how VB gets to know all infromation when you add reference to an activeX dll).

This is accomplished by a set of interfaces like ITypeLib, and as assermark has suggested you use LoadTypeLib to start the fun part..

hope this helps
0
 
griesshCommented:
Dear sinister

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "ambience"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
MindphaserCommented:
Force accepted

** Mindphaser - Community Support Moderator **

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now