Solved

getting method's names and params by object and invoke (com)

Posted on 2001-06-06
15
311 Views
Last Modified: 2013-11-25
how can i get an object's methods names and the parameters they should get ? and how do u invoke it without knowing the number of parameters since u invoke a different method each time in vb ?
callbyname needs a specific number of params, u have to write it out for him or use Array but it's the same thing
i'm trying to do it in c++ for the moment but
it seems very strange that u can't do it right in vb
0
Comment
Question by:Guen
  • 4
  • 2
  • 2
  • +5
15 Comments
 
LVL 2

Expert Comment

by:kmv
ID: 6158820
IDispatch interface have 4 functions:
GetTypeInfoCount
GetTypeInfo
GetIDsOfNames
Invoke

First 3 functions are used to get information about function and its parameters. Most important of them is GetIDsOfNames but all of them are used of course.

All object used in VB have interfaces inherited from IDispatch.

Same way is used for internal (not COM) objects.
If you need more info - read MSDN and ask questions.

Good luck.
0
 
LVL 1

Expert Comment

by:morgan_peat
ID: 6158833
Set a reference to 'TypeLib Information' in VB.
Then do something like the following:

    Dim oTLI As tli.TLIApplication
    Dim oInfo As tli.InterfaceInfo
    Dim oObject As Object
    Dim oMemberInfo As tli.MemberInfo
   
   
    ' Create some new object
    Set oObject = CreateObject("ADODB.Connection")
   
    ' Get info about it
    Set oTLI = New tli.TLIApplication
    Set oInfo = oTLI.InterfaceInfoFromObject(oObject)
   
    ' Have a look at item 21 (I know this is Open)
    Set oMemberInfo = oInfo.Members.Item(21)
   
    Debug.Print oMemberInfo.Name
    Debug.Print oMemberInfo.Parameters.Count


You can also get more info about the parameters from the Parameters Collection.
The TLI should give you enough information to be able to use CallByName.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6158908
interesting...
0
Technology Partners: 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!

 
LVL 10

Expert Comment

by:caraf_g
ID: 6159108
It doesn't really make sense to do something like this in VB. VB is a RAD development language, not a full-fledged 3rd Generation language like C++

Amorphous blobs of objects that you'd have to go and probe in order to find out what exactly they do and how exactly to approach them? Seems a bit odd in VB.

In C++ you can query an object's interface and find out what it does. In VB, typically, you would add a reference to a COM component to your project, and then you *know* what it can do. No probing needed.
0
 
LVL 1

Expert Comment

by:morgan_peat
ID: 6159607
The only reason I can think of (which is a valid reason) is to examine the objects that are registered on the machine.

You could browse the CLSID key, or get the user to type in a PROGID, and show methods / properties of the objects.

Why you wouldn't use the object browser, I don't know...
0
 
LVL 2

Expert Comment

by:kmv
ID: 6162230
What do you want: to know how it works inside VB (how compiler does it) or how to know what parameters are used in each method of VB?
In VB you can call procedures by name. In C/C++ - by address in memory. Do you want to call procedure in VB by addr or something like this?
Could you tell us exactly what do you want to know?

Thank you.
0
 

Author Comment

by:Guen
ID: 6163197
well, i'm writing a method tester, it recieves the object name, creates it and supplies the user with all method names and parameters according to the method chosen
and then activate it with the parameters, so not only
i can't get that info in vb, callByName can't accept a vector of arguments, it needs spelling out so only
a disgusting select-case will work...
0
 
LVL 2

Expert Comment

by:kmv
ID: 6168010
Unfortunatelly, I don't know how to help you. May be there are some undocumented functions but I think that it is impossible. It is done on compilator's level. You can only use callByName for functions with same arguments (sin, cos, tan etc.) and use select-case.

You can do it for COM objects with IDispatch interface.
For user defined objects you need some information about its structure. Possible you need to parse source code of object and get information which you need.

If you have more questions you are welcome.

Good luck.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7133333
Hi Guen,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept kmv's comment(s) as an answer.

Guen, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Author Comment

by:Guen
ID: 7133570
I don't think I got an answer to this question, an I solved it by myself.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7133592
Hi guen,
Can you post your solution?  That would let us refund your points and save this Question to the PAQ  -- for future reference.  Thanks!
-- Dan
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 7136910
Im not sure, thought morgan_peat's one, answer in finding the params for an object.
0
 
LVL 2

Expert Comment

by:kmv
ID: 7164103
ok. Once more.
2Guen.
1. Objects created in VB as instance of VB class are COM objects.
2. VB usually works with objects using IDispatch COM interface.
I enumerated methods of IDispatch interface in my first message. When I developed 2 years ago some special object to asynchronize COM objects I used scheme with IDispatch interface. Using it you can get all information about methods and parameters of COM object, set its values and call any method by its name.
Its enouph to read MSDN about IDispatch to get this information.
But using VB you cannot work with IDispatch directly (you can only get reference to IDispatch but cannot call its methods). All functionality with IDispatch interface is covered by VB framework. You cannot use next code in VB
Dim obj As Object
Dim adoObj As New ADODB.Connection
Dim mem(100) As Long
Dim pMem As Long
Set obj = adoObj ' here is reference to IDispatch
pMem = VarPtr(mem(0))
obj.GetIdsOfNames(0, "Open", 1, 0, pMem) ' here you'll get error message - object doesn't support method. It means that VB itself use method name "GetIdsOfNames" to pass it through IDispatch interface method GetIdsOfNames. As far as IDispatch is not inherited by itself, vb function call of method GetIdsOfNames returns error. VB calls necessary methods to make late binding via IDispatch itself authomatically. You cannot make some trick here.
You can do it using your own COM object written, e.g. on C/C++.

I thought that information which I gave above (in my previous posts) is enouph to find good solution, isn't it?
If you didn't understand it - you could ask more detailed question to clarify situation.

Best regards.
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 7707627
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- PAQ/no refund re DanRollins Comment 07/06/2002 03:02AM PST
Please leave any comments here within the
next seven days.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 7758271
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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