application to a
single instance is very
problematic. There are dozens of solutions available; most
of them work most of the time. The problem is that solutions
that use ProcessIDs or Window Handles fail occasionally due
to Windows sloppy internal handling.
Here is a quick and
dirty solution for keeping a single
instance of a window.
It uses shared memory to maintain a linked list of open
windows. An application
checks the list on startup, and if the application is on the
list, it is exited.
Here is the header:
void RegisterSiblingWindowHandle(LPCTSTR lpLabel, HWND hWnd);
HWND GetSiblingWindowHandle(LPCTSTR lpLabel);
void RemoveSiblingWindowHandle(LPCTSTR lpLabel);
void RemoveSiblingWindowHandle(HWND hWnd);
To use the code, you
need to activate the link to the shared list. The easiest
way to do that is to insert the function
into the constructor of
CWinApp() if you are
using MFC, or right after the RegisterClass() in your
WinMain() in a standard Windows application.
You need to release the
list at the end of the application as well with
This works from the
CWinApp() destructor in
MFC or after the message loop in a
When an application
starts, before the main window is created (either via
HWND hWndOther = GetSiblingWindowHandle(_T("My Unique ID"));
if( hWndOther != NULL )
::SetWindowPos(hWndOther, HWND_TOP, 0, 0, 0, 0,
When a new window is
opened (like in CMainFrame or after
RegisterSiblingWindowHandle(_T("My Unique ID"), m_hWnd);
To unregister a window
(in ExitInstance() or just before the
RemoveSiblingWindowHandle(_T("My Unique ID"));
Of course, replace all
the _T("My Unique ID") tags with a unique ID for your app.
About the Author
I have worked as a Software Engineer for 22 years. I work
with Windows, Unix/Linux, DSP, and Microcontrollers.