Detecting Multiple Instances of an Application in VC++ | CodeGuru

Detecting Multiple Instances of an Application in VC++

Environment: VC6 This is a very simple way to detect the running instance of an application. It can be achieved by declaring a data variable in the compiler’s own data section called shared. #pragma data_seg(“Shared”) LONG g_Counter = -1; #pragma data_seg() After this, the Linker will be noticed to make the Shared section readable, writable, […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 26, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6

This is a very simple way to detect the running instance of an application. It can be achieved by declaring a data variable in the compiler’s own data section called shared.

#pragma data_seg(“Shared”)
LONG g_Counter = -1;
#pragma data_seg()
After this, the Linker will be noticed to make the Shared section readable, writable, and shared.
#pragma comment(linker, "/section:Shared, rws")
Now, whenever the application runs, the data variable declared in the compiler’s data section will be incremented by 1 and until or unless the application gets closed, the data variable’s value will not be decremented. Now, at this point, if we start the application again, it will check for the value of the variable declared in the data section of the compiler. If it’s greater than 1, splash message of duplicate instance; else, start the application.
The value in the data variable of the compiler’s data section is incremented or decremented by the functions declared in winbase.h; these are InterLockedIncrement and InterLockedDecrement. Just pass the address of the variable and the rest will be performed by the function.
Downloads

Download demo project – 112 Kb
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.