Simple Single Instance Windows

Limiting an 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.

Downloads

Comments

killed processes ?

How does this code deal with processes killed in the task manager ?
Although your code looks interesting, you fail to mention what the problems with the other solutions are.
Thanks

Re: Killed Processes

Posted by egawtry
on 11/09/2005 12:32pm

The code deals with underegistered closed windows and processes by checking with the IsWindow() and automatically cleaning up.
The primary problems with the two other common methods:
Window Caption Text Lookup
==========================
1. What if the window is hidden? It doesn't appear in window lists.
2. Maintaining the label as a constant is not attractive.
3. Windows sometimes ignores windows - Windows has done this since Windows 3.0 (funny - 2.0 didn't have the problem). This seems completely random.
Searching for process ID
========================
This is a bit more reliable that the caption text search, it fact it works 99.9% of the time. This is what is the basis for many of the other routines here on CodeGuru. It still falls prey to #3 mentioned under caption text searches.
DDE
===
This is reliable unless your DDE engine in Windows has crashed. DDE is stable under Win9x, but seems to randomly crash under WinNT and Win2000. I haven't tested it under WinXP (I gave up back with W2K).

Top White Papers and Webcasts

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.