tags:

views:

81

answers:

3

I have a program written in foxpro (~shudder~) that I've inherited.

The issue is, it has some COM controls in it. When you start 2 copies, the second copy will throw errors. So I thought, this could be cleaned up to bring the active copy to the front allowing only a single instance.

That would be fine, but the program takes a command line parameter allowing it to be run in a different mode. You can only have one copy running in each mode e.g.

1)

"program /bos"   
"program /pos"  = OK

2)

"program /pos"
"program /pos"  = NOT OK

What I'd like is this

"program /pos"
"program /pos" - Bring currently running pos instance to the front.

"program /bos" - start a BOS instance.
"program /bos" - bring the bos instance to the front.

I hope that makes sense. What is an elegant way to determine whether the running instance of program is in pos or bos mode so I can determine which process to bring to the front or whether to start a fresh copy in the requested mode.

I don't necessarily need foxpro code here - although that would be nice. The concept is what I'm after and some hints in regards to the windows API functions to achieve it would also be of help. e.g. can you determine the command line options of a running process?

A: 

I don't think you can get the command-line of a process other than yours.

A common way to do this is to use a named mutex. When a program starts it grabs the mutex and if it can't then another instance is already running.

There's no reason you couldn't use two named mutexes - one for /pos mode and one for /bos mode.

Aaron
I think you're onto something with the named mutexes and you're right about the command line. I guess I can write the pos or bos process id to a text file.
Matt H
@Matt - Or you could use a named shared memory block to share the HWND or whatever other info you need...
Aaron
A: 

GetCommandLine will return the command line arguments; then, I'd use a named mutex as a unique identifier to determine whether the process is already running. If you include the command line args (or the specific part you're looking for) as part of the mutex name, then you can get the behavior you're looking for.

Eric Brown
GetCommandLine returns command line arguments of the current process only. The named mutex approach is helpful however.
Matt H
right, but you don't care about the command line of the other process. All you care about is the name of the mutex; since both processes are running the same code, the mutex will have the same name if the command line args are the same.
Eric Brown
A: 

A named mutex will do exactly what you need. If you did want to see the command line for all visible processes, though, and you were targeting Windows XP or later you could use WMI: WIN32_Process has a CommandLine property which was introduced in XP / Server 2003.

Stuart Dunkeld