Solved

Using COM with C++ and IDispatch?

Posted on 2002-06-10
4
3,375 Views
Last Modified: 2013-11-25
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
Comment
Question by:sinister
4 Comments
 

Expert Comment

by:assermark
ID: 7069668
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
 
LVL 22

Accepted Solution

by:
ambience earned 100 total points
ID: 7074680
>> 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
 
LVL 11

Expert Comment

by:griessh
ID: 7261779
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
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332919
Force accepted

** Mindphaser - Community Support Moderator **

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

17 Experts available now in Live!

Get 1:1 Help Now