Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two windows applications that are running. I then launch and connect to a third application via a plugin. In the plugin, I am trying to invoke a method on one of the first two applications, i.e. that application is the target object for the invoke. Every example I can find creates a new instance first, and gets the object reference back from a call to Activator.CreateInstance. I want the instance that is already running. I can get the assembly using GetAssembly. From that I can loop through the members and get the type. How do I get the target object to use in the InvokeMember? Using GetObject(, ODTuner.Application) does not work and I want to stay away from COM anyway (It gives me the "ActiveX Cannot Create Component" error.

Dim odt As ODTuner
Dim asm As Assembly
Dim odtType As System.Type

'temporarily create a new instance to get the correct type
'this would not be necessary if I could get the object reference of the currently running ODTuner
Dim myodt As New ODTuner
odtType = myodt.GetType 'temporarily create a new instance to get the correct type
myodt = Nothing

asm = Assembly.GetAssembly(odtType) 'gets the currently loaded assembly (odtuner.exe)

Dim t As Type = Nothing
Dim Types(), TypeInfo As Type
Types = asm.GetExportedTypes()

For Each TypeInfo In Types
msg = msg & TypeInfo.Name & vbCrLf
MsgBox(TypeInfo.Name)
If TypeInfo.Name = "ODTuner" Then
t = TypeInfo
'Else
' t = Nothing
End If
Next

Dim Result As Object

Dim args() As Object
args = {"Filter_25354"}

Result = t.InvokeMember("ODExecute", BindingFlags.Public Or BindingFlags.InvokeMethod Or BindingFlags.Instance, Nothing, odt, args)

Basically, I need to know what to set odt equal to? If I do

Dim odt as New ODTuner

It does invoke ODExecute, but cannot do anything because the necessary data is in the already running instance. I tried setting odt = t (where t is the type), but when I do this I get an error saying that the "Object does not match target type"
Posted
Updated 16-Oct-13 19:46pm
v3
Comments
Bernhard Hiller 17-Oct-13 2:13am    
What do you want to do with the object of the already running application?
You'll likely need someting totally different from what you are trying to achieve now.
Sergey Alexandrovich Kryukov 17-Oct-13 3:10am    
Agree...
—SA
Member 10297511 17-Oct-13 2:27am    
I believe I need it as the target object of the call to t.InvokeMember(). I could also do MethodInfo.Invoke, but it still requires a target object, which in both cases (InvokeMember or Invoke) is supposed to be "the object on which to invoke the specified member". Maybe I do not understand what should be passed here (I'm an engineer, not a developer), but I do know that if I create a new instance, it does invoke the method correctly. I guess I need to know what to pass to InvokeMember as the target object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900