Solved

Executing a external method

Posted on 2002-04-12
15
318 Views
Last Modified: 2013-11-25
Hello experts,

Can I execute, using SendMessage (or another API), a method of a different Running Application?
Something like CallByName but executing a external method.
This Running Application hasn't events, it only has methods and functions, and it's running...

Thank you.

 
0
Comment
Question by:abautistat
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6936375
a)
Therefore the external application must support a public interface. A mechanism in the MS world is an COM interface. Therefore the external application is for instance an ActiveX exe or an Application which uses an AtiveX dll. These applications/Dlls provides a public
interface. A COM interface is a strongly-typed contract between software components to provide a small but useful set of semantically related operations (methods).

b)
Another way to communicate (indirectly) with applications is the way you mentioned above. Therefore the windows application must 'look for' window messages that are adressed to the application through the window handle of the operating system. Therefore you can send messages and transfer data to the other application. Indirecly this calls functions in the other application to process the messages.

c)
Another interface to transfer information and commands  toanother application is DDE (Dynamic data exchange). Both programs therefore must support DDE. DDE is a progenitor of the OLE/COM mechanism.

d)
The last way i know is a simple communication over a DLL which is used by both application. This DLL is the communication partner for both parties.

Hope this helped you ;-D
Wok


 
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6936405
If you use SendMessage the other application has to intercept that message in it's message loop, and it has to know what that message looks like (the kind of message and it's lparam and wparam).

If the other APP is a VB app you will have to use subclassing to tap into the message queue. If you are not familiar with subclassing, let me tell you it is a tricky thing to do and you have to understand how windows operates at it's root (message loops and queues). It can be done in VB and I have used it sucessfully more than one time.

Another way is :

In the running application you can define let us say a checkbox with a specific caption, say 'receiver'
Then your calling app has to find the window handle of this checkbox (and the form it is contained in) and send a message to this textbox's handle
Call SendMessageTimeout(hWndReceiver, BM_SETCHECK, 1, 0, 0, 100, result)

In the running app you can take action in the change event of the checkbox (receiver)




0
 

Author Comment

by:abautistat
ID: 6936445
Thank you for comments...
I need explain my problem:

The expernal app is a VB app, concretly EXE ActiveX and I haven't any form (evidently I haven't controls).

I have two class modules and one more module. This ActiveX run because it implements for others ActiveX.exe and others standard EXE app.

I need launch a method within the first EXE ActiveX. Who launch it? A independent and external VB app.

(Sorry for my bad english, but my mother language is spanish).

Thank you again...
0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6936478
Use WolfgangKoenig's method a (just call a member of the interface)
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6936584
So you can use method 1....
When your external activX EXE has that an COM Interface which is the name of the Project Description of your ActivX EXE!

This Interface Name can be found by another Exe when the application looks for registered AxtiveX COM/OLE objects. In VB IDE you can simply do an early binding for your ne EXE:
(MenuIDE) Project --> Links/References
And then look for your exteral ActX EXE Description and choose it (Checkbox)

When you have set a reference to the external ActivX EXE then you can build an object var of the 2 classes that are a part of the exteral ActX EXE for instance:

Private Test As TestCOM.Class1

Then you can call a public method that are supported by the Class1 or 2...:

Set Test = New TestCOM.Class1
Call Test.testmsg

WoK;<D
0
 

Author Comment

by:abautistat
ID: 6942432
Sorry but I don't want to create a NEW instance.

I want to execute a method of a existing instance.
This class is a single use class and it can be instanced several timer for others app.

I want to do something like this:

- ActiveX_1 instanced ActiveX_2
- Then ActiveX_3 execute, for example,  a Logout method of ActiveX_2 instance.

0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6944064
You have to have an intermediate object that holds references to the several instances. Then you call methods on this object that relays the calls to the specific server.

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6944315
Therefore you can use:
Set Test = LoadObject("TestCOM.Class1")

Creates a ref handle to an existing instance of
"TestCOM.Class1" ...

WoK;<D
0
 

Author Comment

by:abautistat
ID: 6944388
What kind of function is LoadObject?
I can't find it.
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6944689
Sorry i mean GetObject here is example code:
--------------------------------------------
GetObject Function Example
...........................
This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet (MyXL). It uses the worksheet's Application property to make Microsoft Excel visible, to close it, and so on. Using two API calls, the DetectExcel Sub procedure looks for Microsoft Excel, and if it is running, enters it in the Running Object Table. The first call to GetObject causes an error if Microsoft Excel isn't already running. In the example, the error causes the ExcelWasNotRunning flag to be set to True. The second call to GetObject specifies a file to open. If Microsoft Excel isn't already running, the second call starts it and returns a reference to the worksheet represented by the specified file, mytest.xls. The file must exist in the specified location; otherwise, the Visual Basic error Automation error is generated. Next the example code makes both Microsoft Excel and the window containing the specified worksheet visible. Finally, if there was no previous version of Microsoft Excel running, the code uses the Application object's Quit method to close Microsoft Excel. If the application was already running, no attempt is made to close it. The reference itself is released by setting it to Nothing.

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
               ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
               ByVal wParam as Long, _
               ByVal lParam As Long) As Long

Sub GetExcel()
   Dim MyXL As Object   ' Variable to hold reference
                        ' to Microsoft Excel.
   Dim ExcelWasNotRunning As Boolean   ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
   On Error Resume Next   ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
   Set MyXL = Getobject(, "Excel.Application")
   If Err.Number <> 0 Then ExcelWasNotRunning = True
   Err.Clear   ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
   DetectExcel

' Set the object variable to reference the file you want to see.
   Set MyXL = Getobject("c:\vb4\MYTEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
   MyXL.Application.Visible = True
   MyXL.Parent.Windows(1).Visible = True
    Do manipulations of your  file here.
   ' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
   If ExcelWasNotRunning = True Then
      MyXL.Application.Quit
   End IF

   Set MyXL = Nothing   ' Release reference to the
                        ' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
   Const WM_USER = 1024
   Dim hWnd As Long
' If Excel is running this API call returns its handle.
   hWnd = FindWindow("XLMAIN", 0)
   If hWnd = 0 Then   ' 0 means Excel not running.
      Exit Sub
   Else            
   ' Excel is running so use the SendMessage API
   ' function to enter it in the Running Object Table.
      SendMessage hWnd, WM_USER + 18, 0, 0
   End If
End Sub

WoK;)
0
 

Author Comment

by:abautistat
ID: 6947188
WoK,
I can't use GetObject with a class created with VB, also I have a lot of instances of this class (this class is created like Single Use) therefore I couldn't know what instance is for my interest.

Thank you for your comments...
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6947574
Hi abautistat,
You can get references to currently running ActiveX programs or Dlls ....
The problem is that you can't specify which currenlty running instance (program) oif the ActiveX (exe/dll) you will get ...

So you can use method 1....
When your external activX EXE has that an COM Interface which is the name of the Project Description
of your ActivX EXE!

This Interface Name can be found by another Exe when the application looks for registered AxtiveX COM/OLE
objects. In VB IDE you can simply do an early binding for your ne EXE:
(MenuIDE) Project --> Links/References
And then look for your exteral ActX EXE Description and choose it (Checkbox)

When you have set a reference to the external ActivX EXE then you can build an object var of the 2 classes
that are a part of the exteral ActX EXE for instance:

Private Test As TestCOM.Class1
Test =

Then you can call a public method that are supported by the Class1 or 2...:

Set Test = Getobject(, "TestCOM.Class1")
Call Test.testmsg

...

WoK
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 7754994
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:
- points to WolfgangKoenig
Please leave any comments here within the
next seven days.
0
 

Author Comment

by:abautistat
ID: 7767740
Sorry, but Wok don't give me the solution.

I know very well to reference an ActiveX. But this isn't the problem...

I want to execute a method of an EXTERNAL application of mine. I know the name of de ActiveX (this was executed by third application) and I know the name of the method...
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7812330
Refund/PAQ

SpideyMod
Community Support Moderator @Experts Exchange

abautistat,
If you provide feedback to the experts, they are usually more than happy to assist to bring the question to a successful close.  When cleanup volunteers such as twalgrave see questions that appear to be abandoned, they figure you got your answer and frequently suggest to award the points.  Thanks for replying and all should be straight now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now