Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Receive Command Line parameter in already running app 2

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I need an app that I am writing to be able to accept command line parameters (which I know how to do). But I also need it to be able to accept them when the app is running without creating a new instance of itself.

Basically the app will be shelled to from another program (not a VB or my creation, so I can't alter it). I need to make sure that the command gets executed by my application without creating another instance of itself.

The propritary(sp?) software would call "c:\myapp /save", this would fire up my app. My app would retrieve the command line parameter and check to see if there is a copy already running, then it would either pass the parameter to the already running program or it would execute the command.


Anyone have any ideas? Is this a good scenerio, given that the propritary(sp?) can only communicate with my app in this mannor?

Thanks,
Troy Williams B.Eng.
fenris@hotmail.com

 
Sounds like you need to create and ActiveX exe instead of a standard exe
 
see on article about command line parameters. Great explanation! I used this same code example to pass command line parameters to already running instances of my vb exe, it requires subclassing but once implemented you will want to implement it in all your exe's!
 
My God!The vbaccelerator way is horrendous! It's much easier than that, doesn't involve any subclassing, and the feature can be added an any already existing application with just a few lines of code. And bjd4jc is on the right lines - you do need to create an ActiveX exe, but a tiny little helper library rather than a conversion of your project. Here's the example code. First we'll create the helper classes.

Create a new ActiveX Exe project. Name it iLinker
Add one module and two classes.
Call one class clsCommand and the other clsConnector
Set the instancing of clsCommand to PublicNotCreateable, and the instancing of clsConnect to MultiUse

The drop in the following code:

Module
[tt]
Option Explicit

Public PrivateCommand As clsCommand
Public lCount As Long
[/tt]

clsCommand
[tt]
Option Explicit

Public Event CommandArrived(strCommand As String)

Friend Sub RaiseCommandArrived(strCommand As String)
RaiseEvent CommandArrived(strCommand)
End Sub
[/tt]

clsConnector
[tt]
Option Explicit

Private WithEvents CommandCopy As clsCommand
Public Event CommandArrived(strCommand As String)

Private Sub Class_Initialize()
If PrivateCommand Is Nothing Then
Set PrivateCommand = New clsCommand
End If
Set CommandCopy = PrivateCommand
lCount = lCount + 1
End Sub

Private Sub Class_Terminate()
lCount = lCount - 1
If lCount = 0 Then
Set PrivateCommand = Nothing
End If
End Sub

' Bubble the event
Private Sub CommandCopy_CommandArrived(strCommand As String)
RaiseEvent CommandArrived(strCommand)
End Sub

Public Property Let NewCommand(strCommand As String)
PrivateCommand.RaiseCommandArrived strCommand
End Property
[/tt]
Ok, that's all the hard work done. You now have a tiny class library that will provide you with ability to send messages between applications, and respond to them as they arrive.

Here's a skeleton program (it just needs a Form) that uses the class library to address the issue you were asking about:
[tt]
Option Explicit

Dim WithEvents AppMessage As clsConnector
Public Startup As Boolean

Private Sub Form_Initialize()
Set AppMessage = New clsConnector
If App.PrevInstance Then
AppMessage.NewCommand = Command$
Unload frmDisplay
End If
End Sub

Private Sub Form_Terminate()
Set AppMessage = Nothing
End Sub

Private Sub AppMessage_CommandArrived(strCommand As String)
If Not App.PrevInstance Then MsgBox strCommand
End Sub
[/tt]
To use it, you just need to compile and register the iLink ActiveX exe, then add a reference to it in the skeleton program.
 
Thanks all for the input. Thanks strongm for the code, I can't wait to try it!

I was also wondering would there be a better way to approach this problem? Troy Williams B.Eng.
fenris@hotmail.com

 
Seems to me that the vbaccellerator article was on the right track, but a little overkill.

If I had to do this, I would dust off some code, or old articles I have seen that use WM_COPYDATA in VB to send messages between two VB applications.

I would use that code as the transport for sending the command-line args.

Then, just code your single application to do the following:

1. Upon startup, look for a copy of itself (there is code out there that will return the hwnd or window handle of an application given its windows titlebar title).

2. If a previous instance was found, use the hwnd to send the command line parameters to that running instance via the WM_COPYDATA approach. Once the message is sent/received, terminate the instance of the app via normal, programmatic VB termination (main form unload, etc).

3. Code the application to process incoming WM_COPYDATA messages and process them, taking the command line parameters bundled within, and passing them off to whatever handler is used to process the normal command line params.

No subclassing. Not much code...

Take a look at the following MS Q Article to get you started... it might have just about everything you need.


Tom
TJRTech, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top