This tip is shown in two parts. This Part I uses inner application communication to send and receive a command line. In Part II a DLL is used for a similar purpose. Click here to view Part II.
This October 2003 tip is created by Michael Culley. Michael writes:
The problem with using command line parameters for any application is that a new instance of the application is started. For some apps it would be preferable if there was only one instance of the app and the command line could be forwarded to the previous instance. This can be done by looking for a previous instance of the app, sending a WM_COPYDATA message to that previous instance and exiting (if it is found). If it is not found then the application is started normally. This is the technique used by MS Word and other applications.
A VB .Net version is documented in this tip. Source code for the VB .Net, C# and VB6 versions is available for download. See below.
Michael created the VB .Net demo program:
Run another copy of this app - Clicking this button causes the 'Command Line' to be sent to an instance of this application. If running the application returns the command to the list box.
Use code similar to the following in your application:
Include Class AppMessenger in your application:
Using the Routines
Add the following module to your application if you want to use these routines:
Module Description AppMessenger.vb The routines described in this document.
Used in the demo, not needed in your application:
Module - Click the link for details. Description BGWebConnect.vb Connect to the web using a URL and the System.Diagnostics.Process class. BGAbout.vb Provide an About message. BGInfo.vb Connect to the web and display this page. BGHelpMenu.vb Dynamically create the help menu.
Download VB Code
Click here to download the VB.Net Source Code [14k]. Click here to download the C# Source Code [11k]. Click here to download the VB6 Source Code [16k]. The C# and VB6 code is unmodified as provided by Michael Culley.
VB6 Note from Michael Culley - To keep things simple I created a hidden window to receive the WM_COPYDATA message. The alternative was to create some extra code to go through the processes and enum though Windows. I have included the extra code in a module in case anyone wants to look at it. I noticed this was similar to your December 2000 tip but I guess it is different enough.
I found the 'Run Another Copy of this App' button doesn't always work in the IDE. If the user runs VB6 first and then opens the project then the current directory is the path to VB6 and not the path to the current project. If the user opens the project by double clicking on fcl.vbp or running fcl.exe then it works fine. This will not affect the code when running on an end user's machine.
The VB6 version uses my subclassing control Click here to download a copy of the control. Be sure to unzip and register it.
Michael Culley develops software in Australia. He can be reached at: email@example.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