Click to See Complete Forum and Search --> : How to detect concurrent user
garage
July 3rd, 2007, 01:11 AM
Hi All,
I have following scenario.
Application name : test.exe
user1 has logged into the system and executed the application.
Now, without closing the application he simply locks the system (CTRL+ALT+DEL).
Now, user2 logs into the same system and tries to execute the test.exe application. At this point, I want to detect whether that application is already running in the system.
Can anybody suggest me the way out to detect this scenario using Win API ?
Thanking you all in advance.....
Thanks,
Garage
Ejaz
July 3rd, 2007, 02:11 AM
user1 has logged into the system and executed the application.
Now, without closing the application he simply locks the system (CTRL+ALT+DEL).
Now, user2 logs into the same system and tries to execute the test.exe application. At this point, I want to detect whether that application is already running in the system.
Do you want to detect the user who log in, or want to detect if the application is already running or not? For later case, you can use a named mutex in your application to check across processes. Take a look at How to limit 32-bit applications to one instance in Visual C++ (http://support.microsoft.com/kb/243953) to see the technique.
And, if you want to get the user name, take a look at Igor Vartanov's post in this (http://www.codeguru.com/forum/showthread.php?t=347275) thread.
garage
July 3rd, 2007, 02:56 AM
Hi Ejaz,
Thanks a lot for your prompt response and also for providing me some direction.
But, I do not have much idea about mutex. So, just wanted to know, in case I use semaphore, then will it take care of the single instance issue across user boundaries ?
Thanks,
garage
garage
July 3rd, 2007, 03:11 AM
Here my concern is, as I understand mutex can be used across processes and semaphore is within process. So, when we talk about across users, then do we need to go for mutex or semaphore will do the trick ? May be I am wrong about the concept of semaphore and mutex !
I am trying to clear the Test case 8 for Vista certification of the product I am currently working on.
Thanks,
garage
Ejaz
July 3rd, 2007, 03:21 AM
Take a look at Mutex vs. Semaphore, what is the difference? (http://koti.mbnet.fi/niclasw/MutexSemaphore.html) :)
Well, I never done that through Semaphore and by using Mutex is more natural to limit the access to the single instance.
Here is a sample (beware, I only typed it, it might have typos).
// .h file
class CMutex
{
public:
bool TryLock();
void Lock();
void Unlock();
HANDLE GetMutex() { return m_cMutex; }
CMutex( const string& rName );
~CMutex() { CloseHandle( m_cMutex ); }
bool IsExist() { return ( ERROR_ALREADY_EXISTS == m_dwLastError); }
private:
void Init(const string& rName);
private:
HANDLE m_cMutex;
DWORD m_dwLastError;
};
// .cpp file
//-----------------------------------------------------------------------
CMutex::CMutex( const string& rName )
{
Init( rName );
}
//-----------------------------------------------------------------------
void CMutex::Init( const string& rName )
{
m_cMutex = CreateMutex( 0, false, rName.c_str() );
m_dwLastError = GetLastError();
assert( m_cMutex );
}
//-----------------------------------------------------------------------
bool CMutex::TryLock()
{
return WaitForSingleObject( m_cMutex, 0 ) == WAIT_TIMEOUT ? true : false;
}
//-----------------------------------------------------------------------
void CMutex::Lock()
{
while( WaitForSingleObject( m_cMutex, INFINITE ) != WAIT_OBJECT_0 );
}
//-----------------------------------------------------------------------
void CMutex::Unlock()
{
ReleaseMutex( m_cMutex );
}
//-----------------------------------------------------------------------
and in the main application, you can declare a global variable, like
CMutex g_mutex("GARAGE_TEST_APP");
You can see if its already running or not in the initlization of your application, as
if ( g_mutex.IsExist() )
return FALSE; // or exit however you prefer
garage
July 3rd, 2007, 04:29 AM
Let me check the behavior with user switch. I am using semaphore. Under same user, it's working properly.
Thanks,
garage
j0nas
July 3rd, 2007, 04:40 AM
NOTE: remember to prepend the named object (mutex or semaphore) with "Global\"... see kb310153 (http://support.microsoft.com/kb/310153)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.