Solved

Using COM with C++ and IDispatch?

Posted on 2002-06-10
4
3,401 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

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

Suggested Solutions

Title # Comments Views Activity
ClickOnce Install - Shortcut Question 3 73
Recommendation vb6 to vb.net or others 14 114
Beginner to Unreal Engine 4 5 62
What namespace do I need to import? 2 14
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

862 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

27 Experts available now in Live!

Get 1:1 Help Now