This tip is shown in two parts. This Part II uses a DLL to provide application communication. In concept the DLL operates like a VB6 ActiveX exe. In Part I inner application communication is handled within the application. Click here to view Part I.
This November 2003 tip is created by Michael Culley. Michael writes:
This is the second part of the tip. It uses a slightly modified version of the exe used in the prior tip to receive the ActiveX exe call. A separate dll is used to pretend to be an ActiveX exe. When this dll receives a call it forwards it on the exe, if the exe is not running it is started. Although it is more complicated than making an ActiveX exe in VB6, the end result is the same. Only a string can be sent across to the app, but with serialization any parameters could be sent.
This code does not allow for a return value from the call other than an integer, which is a limitation of the WM_COPYDATA message that this code uses. The only method of getting a return value would be to send a message back to the dll.
Once all of the work is done then calls can be made to the app as if it is a VB6 ActiveX exe:
Dim x As Object
Set x = CreateObject("MyAppVB.Application")
x.SendMessage "This is a test!!!"
Early binding can also be used and the dll can be called from another .Net exe..
The idea originally came from an app I am writing for dental surgeries. Third party apps need to tell my app to open a patient's records, for example:
Dim x as New DentalApp.Application
The DLL has an OpenPatient method that receives 2 parameters. [The parameters could be passed in a structure.] The DLL then serializes this, say into "John!Smith", and forwards the string to the application. Thus the interface to the DLL is the same as the interface to a VB6 ActiveX exe. You need to customize the DLL's SendMessage function.
A VB .Net version is documented in this tip. Source code for the VB .Net, and C# versions is available for download. See below.
VB6 ActiveX Exe
Michael wrote a demo application called "AppThatWillCallMyApp" to illustrate the process of using a DLL to launch another application and pass a string message. The distributed source code contains compiled version of the applications and the DLL.
Launch AppThatWillCallMyApp. It can be found in the 'bin' folder of the distributed source.
AppThatWillCallMyApp - Click Send Message...
MyApp is launched and receives the 'Test message".
Change the message to something else while MyApp is running. In this example "Second Message" is sent. Click Send Message...
Second message is received by MyApp.
Use code similar to this to launch my app. This code is from the button click event in AppThatWillCallMyApp
Be sure to make a reference to the DLL. See Using a VB .Net Class Library for information on referencing a VB .Net DLL in an application.
This is the public interface in MyApp.DLL. You need to modify this code to launch your specific application - change "MyApp.Exe" to the name of your application.
The operative code in the DLL that launches MyApp.Exe. You may want to review this and underlying code to see how it works.
MyApp.DLL is registered for COM Interop so it can be accessed from VB6 or from . The Interop stuff can be removed if the third party app is only. Registering for COM Interop is fairly easy:
- First 'Register for COM Interop' must be checked under project properties,
Click Help for more COM Interop information.
- Create an interface and a class to implement the interface,
- The COM attribute (can't remember its name) needs to be applied to the class.
Select menu Project/References to open this dialog then check MyApp.
Application.SendMessage is exposed and shown in the Object Browser.
Download VB Code
Click here to download the VB.Net Source Code [45k]. Click here to download the C# Source Code [42k]. The code is unmodified as provided by Michael Culley.
Michael Culley develops software in Australia. He can be reached at: firstname.lastname@example.org. I have used Michael's VB6 subclassing code in these tips:
Tip Month TreeView Disable Node March 2001 Restrict a Form's Size and a Moved Event June 2001
Previous Tip Tip Index Next Tip