Solved

Using COM with C++ and IDispatch?

Posted on 2002-06-10
4
3,525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What .NET website keeps me current? 9 80
Outlook 13 98
Input parameteres to DragOver 2 56
Why use this lambda? 12 93
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…
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

736 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