Avoiding duplicate Exe running simultaneously
-
Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks
-
You can use Mutex to do single instance check. An article here: How to limit 32-bit applications to one instance in Visual C++[^] Thanks, Suman :)
-- "Programming is an art that fights back!"
-
Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks
rp_suman's link is good, but there's a better article here on CP http://www.codeproject.com/KB/cpp/avoidmultinstance.aspx[^] This is particularly good to read if you have a GUI for your app.
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
You can use Mutex to do single instance check. An article here: How to limit 32-bit applications to one instance in Visual C++[^] Thanks, Suman :)
-- "Programming is an art that fights back!"
-
rp_suman's link is good, but there's a better article here on CP http://www.codeproject.com/KB/cpp/avoidmultinstance.aspx[^] This is particularly good to read if you have a GUI for your app.
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks
Here's my solution:
/* MultipleInstance.h */
class MultipleInstance {
public:
MultipleInstance();
~MultipleInstance();
static bool Active();
private:
static bool _Active;
static bool _Initialized;
static HANDLE _Handle;
static MultipleInstance *_Instance;
};
/* MultipleInstance.cpp */
static MultipleInstance _MultipleInstance;
bool MultipleInstance::_Active = false;
bool MultipleInstance::_Initialized = false;
HANDLE MultipleInstance::_Handle = NULL;
MultipleInstance *MultipleInstance::_Instance = &_MultipleInstance;
MultipleInstance::MultipleInstance()
{
if ((!_Initialized) && (this == _Instance)) {
// create security descriptor to let all users access the semaphore
SECURITY_DESCRIPTOR security_descriptor = { 0 };
InitializeSecurityDescriptor(&security_descriptor,
SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&security_descriptor,TRUE,NULL,FALSE);
// create semaphore
SECURITY_ATTRIBUTES security_attributes = { 0 };
security_attributes.nLength = sizeof(security_attributes);
security_attributes.lpSecurityDescriptor = &security_descriptor;
security_attributes.bInheritHandle = FALSE;
SetLastError(0);
_Handle = ::CreateSemaphore(&security_attributes,0,1,
_T("Global\\InstanceSemaphore"));
/* change "InstanceSemaphore" to your desired name */
if (GetLastError() == ERROR_ALREADY_EXISTS) {
_Active = true;
}
_Initialized = true;
}
}
MultipleInstance::~MultipleInstance()
{
if (_Initialized && (this == _Instance)) {
if (_Handle != INVALID_HANDLE_VALUE) &&
(_Handle != NULL)) {
CloseHandle(_Handle);
_Handle = NULL;
}
}
}
bool MultipleInstance::Active()
{
return _Active;
}Software Zen:
delete this;
-
Thanks, but it seems a bit complex for me.. :doh: Is there an alternative without having to use an object? I'm relatively new to c++ and I haven't used objects in my program so far.
Well in that case you've got a lot of reading ahead of you! Yes, you can do this without using classes, *but* the core technique outlined in the article really is the only good way to do this, as it properly deals with concurrency issues that many of the other approaches don't deal with, at least for an app that has a GUI. If you don't have a GUI then you may not need something as sophisticated.
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
Here's my solution:
/* MultipleInstance.h */
class MultipleInstance {
public:
MultipleInstance();
~MultipleInstance();
static bool Active();
private:
static bool _Active;
static bool _Initialized;
static HANDLE _Handle;
static MultipleInstance *_Instance;
};
/* MultipleInstance.cpp */
static MultipleInstance _MultipleInstance;
bool MultipleInstance::_Active = false;
bool MultipleInstance::_Initialized = false;
HANDLE MultipleInstance::_Handle = NULL;
MultipleInstance *MultipleInstance::_Instance = &_MultipleInstance;
MultipleInstance::MultipleInstance()
{
if ((!_Initialized) && (this == _Instance)) {
// create security descriptor to let all users access the semaphore
SECURITY_DESCRIPTOR security_descriptor = { 0 };
InitializeSecurityDescriptor(&security_descriptor,
SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&security_descriptor,TRUE,NULL,FALSE);
// create semaphore
SECURITY_ATTRIBUTES security_attributes = { 0 };
security_attributes.nLength = sizeof(security_attributes);
security_attributes.lpSecurityDescriptor = &security_descriptor;
security_attributes.bInheritHandle = FALSE;
SetLastError(0);
_Handle = ::CreateSemaphore(&security_attributes,0,1,
_T("Global\\InstanceSemaphore"));
/* change "InstanceSemaphore" to your desired name */
if (GetLastError() == ERROR_ALREADY_EXISTS) {
_Active = true;
}
_Initialized = true;
}
}
MultipleInstance::~MultipleInstance()
{
if (_Initialized && (this == _Instance)) {
if (_Handle != INVALID_HANDLE_VALUE) &&
(_Handle != NULL)) {
CloseHandle(_Handle);
_Handle = NULL;
}
}
}
bool MultipleInstance::Active()
{
return _Active;
}Software Zen:
delete this;
Hi, many many thanks for the advice but not being familiar to oject programming I am missing some crucial bits to make it work. In MultipleInstance.h what name should I give to: #ifndef #define Is there anything else I need to specify? In MultipleInstance.cpp I have specified: #include <iostream> #include "MultipleInstance.h" Is anything else needed? In the main cpp file: What is the correct syntax I need to use to call the object? Should I insert it immediately after int _tmain(int argc, _TCHAR* argv[]){