Forward a Command Line from one Application to Another Using a DLL - November 2003
    VB .Net and C# - Part II

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
x.OpenPatient "John","Smith"

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 versus .Net DLL

VB6 ActiveX Exe

.Net DLL

Forward Command Line From One Application to Another using a DLL - Demo

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.

Folder containing compiled demo programs
Launch AppThatWillCallMyApp. It can be found in the 'bin' folder of the distributed source.

Send "Test message"
AppThatWillCallMyApp - Click Send Message...

MyApp is launched and receives 'Test message".
MyApp is launched and receives the 'Test message".

Send a second 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 in MyApp
Second message is received by MyApp.

Operative Code

Application Operative Code
Use code similar to this to launch my app. This code is from the button click event in AppThatWillCallMyApp

Reference MyApp.DLL
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.

DLL Public Interface
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.

DLL Operative Code
The operative code in the DLL that launches MyApp.Exe. You may want to review this and underlying code to see how it works.

COM Interop Enabled

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:

Referencing the DLL in VB6


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

Michael Culley develops software in Australia. He can be reached at: buygoldquery@mikesdriveway.com. 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

Home

Updated 2011/10/06