Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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

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
  • 4
  • 2
  • 2
  • +5
1 Solution
IDispatch interface have 4 functions:

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.
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.
Valliappan ANSenior Tech ConsultantCommented:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.
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...
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.
GuenAuthor Commented:
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...
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.
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
GuenAuthor Commented:
I don't think I got an answer to this question, an I solved it by myself.
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
Valliappan ANSenior Tech ConsultantCommented:
Im not sure, thought morgan_peat's one, answer in finding the params for an object.
ok. Once more.
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.

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.
Finalized as proposed


Community Support Moderator
Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now