English 中文(简体)
Bringing active program to the front - with a twist
原标题:

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 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.

问题回答

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.

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.





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签