Single instance app & command line usage

harpo
10-10-2009, 07:59 AM
Hi,

I have a VB6 app for which I want to ensure that only one instance is running. For this purpose, in Form_Initialize() of the startup form I have

If App.PrevInstance Then End

But this app is also integrated with WindowsExplorer, that is you may right click a file and select 'Open in myapp', which sends a command like
myapp.exe -sh<filepath> to it - in this case, if there is an opened instance, nothing happens as the newly started one dies immediately.

Is it possible to combine both? To have only one instance, which somehow catches commands sent to the .exe ?

What comes to mind is to write the command to a file or registry before End, and to regularly check for this file/registry, read and delete it when it appears. But there should be a more elegant way than regular checks.

Thanks for any suggestions

Iceplug
10-10-2009, 09:43 AM
Use the Command$ function in your program to look at the command line arguments.

harpo
10-10-2009, 10:38 AM
I do that and the app can be used in command line mode.
The problem is that I do not want more than one instance to be opened, so at startup I check if another instance is already running, and if yes - the new one terminates itself.

If the app is already started (instance #1), and then something sends a command to it, like app.exe -blah , this will start the program again - instance #2. But I do not two instances - instead, I want instance #1 somehow to become aware of -blah command.

Hope I explained it better this time.

dilettante
10-10-2009, 10:51 AM
I suspect what you'd have to do is either use DDE (and DDE Shell Integration) or else use some form of IPC to let instance #2 pass its command line to instance #1.

DDE is an old Windows technology, and while VB6 has pretty good support for it built in most programmers don't have much experience with it and little gets written about it anymore. However the Shell (i.e. Explorer) can use DDE to directly inform instance #1 of a new request without ever starting a #2.

An alternative might be to have instance #1 be some other sort of IPC server that lets #2 start up and try to greet #1 with the new command line (silently terminating if it succeeds, proceeding as #1 itself if this fails).

This other IPC might be UDP, Mailslots, a window message, etc.

vb5prgrmr
10-10-2009, 07:11 PM
3 ways discussed in this thread...

http://www.tek-tips.com/viewthread.cfm?qid=1567400&page=1



Good Luck

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum