Restrict application to one instance per shell session on Windows

There are a lot of solutions for restricting an application from running twice. Searching by process name, using a named mutex etc. But I all of these methods don't work if I want to restrict my application to the shell session.

A user may have more than login session and shell on windows (right?)? If this is true I want to be able to run one instance of my application in every shell session but allow only one.

Is there a way to get a shell identifier which could then be put into the mutex name?


You can create local (session only) or global (whole system) mutexes. See http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx for more info. Look for global and local.


The shell identifier you want to use is the user name. This is available as Environment::UserName or GetUserName()


You can go from process id of the current process to a WTS session ID, I think that will do what you need. ProcessIdToSessionId

You should be aware that a terminal services session can be disconnected from one desktop and then connected to by another, so the 'shell' can actually change, but the session ID should remain the same.

链接地址: http://www.djcxy.com/p/51164.html

上一篇: 每个网络一个应用程序实例

下一篇: 在Windows上将应用程序限制为每个shell会话的一个实例