Closed KnockKnockP closed 2 months ago
Thinking about it, I could probably just use mutex to force single instance. I don't know why I overcomplicated it.
Yeah, you can do that. Here's an example:
// I use a class here to perform cleanup after winmain
class InstanceMutex
{
HANDLE m_handle = NULL;
public:
HRESULT Init()
{
#ifdef _DEBUG
return 0;
#else
SetLastError(NO_ERROR);
m_handle = CreateMutex(NULL, TRUE, WINDOW_CLASS);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
if (m_handle)
{
CloseHandle(m_handle);
m_handle = NULL;
}
return ERROR_ALREADY_EXISTS;
}
return GetLastError();
#endif
}
~InstanceMutex()
{
#ifdef _DEBUG
if (m_handle)
CloseHandle(m_handle);
#endif
}
};
InstanceMutex g_instanceMutex;
bool DetectDuplicateInstances()
{
HRESULT hres = g_instanceMutex.Init();
if (hres != ERROR_ALREADY_EXISTS) return false;
HWND hWnd = FindWindow(WINDOW_CLASS_NAME, NULL);
if (hWnd) {
SendMessage (hWnd, WM_REQUESTACTIVATE, 0, 0);
return true;
}
return false;
}
Thanks! I'll take a look.
On Wed, Jun 26, 2024, 22:26 KnockKnockP @.***> wrote: