How to call a method which is in a string 
Author Message
 How to call a method which is in a string


Here is the solution for the question I asked few days ago.
First thanks Russ McClelland for his answer, it provide us a really good
start to this solution.

Sometimes you need to call a method which names is known during execution.
So you have the name in a string and can't do a obj.mymethod. For example,
if your program must call service (=method) of different DLL depending on
request put dynamically in a database.... which is my case -(
So use it only if you can not do a     obj.method          call !!!

1) Include in your reference "TypeLib Information"

2) Code
 a) Create your object MyObj
 b) Create a TLIapp object: TLI.TLIApplication
 c) Invoke its method InvokeHook
InvokeHook(  MyObj as object, _
   method as string, _
   InvocType as integer, _
   [parameters in reverse order if any])

Code Example:
We suppose that:
a) the called sub has this prototype: MySub(param1 as integer, param2 as
b) MySub is in Proj.Class in a DLL (This DLL is included in your references)

Private Sub Command1_Click()
    Dim MyObj As Object
    Dim TLIapp As TLI.TLIApplication

    Dim param1 As Integer
    Dim param2 As String
    Dim Meth as string

    Set obj = CreateObject("proj.class") 'You can do = New proj.Class
    Set TLIapp = New TLI.TLIApplication
    Meth = "MySub"             'Filled dynamically
    call TLIapp.InvokeHook(obj, "MySub", INVOKE_FUNC, param2, param1)
   'parameters must be in reverse order.

    Set TLIapp = Nothing
    Set obj = Nothing
End Sub

TLIApplication can also provide list of method in your object, memberid
see TLIApplication.InterfaceInfoFromObject...members.item(index)...memberid


Artefact Informatique.

Wed, 27 Dec 2000 03:00:00 GMT  
 How to call a method which is in a string
oups !!! you must read
call TLIapp.InvokeHook(obj, Meth, INVOKE_FUNC, param2, param1)
call TLIapp.InvokeHook(obj, "MySub", INVOKE_FUNC, param2, param1)


Wed, 27 Dec 2000 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Using string to specify method to call on object

2. OLE automation - calling method function with string par by reference

3. Calling method which name is in a string

4. Calling Class Method / Property with String Variable

5. I'd use string methods more if string were a structure and not a class

6. Public Method in a class calling a private method in same class yields ByRef error

7. Calling a COM call back object's method fails if it is in Windows 98

8. Help with API Calls please.........I am at wits end :(

9. I am an .OCX: Who called me

10. I am an .OCX: Who called me ?

11. Calling A Function (What am I doing wrong)

12. Call Function of am Modul = Error !


Powered by phpBB® Forum Software