Solved

Using COM with C++ and IDispatch?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2013: API code to put hidden database icon in taskbar? 8 86
Specific format 21 206
What .NET website keeps me current? 9 77
object oriented programming comparison 5 77
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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.

839 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