Solved

Allowing a single instance of an application

Posted on 1997-12-11
2
205 Views
Last Modified: 2013-11-20
I'm trying to ensure that only one instance of my application can be run at a time. I've been trying to use the following code:

// Check to see if or app is already running and revert
//  to it. Only one can run at a time. If we find another
//  version of ourself, defer to it.
HWND hwnd = FindWindow( "My App", NULL );
if( hwnd )
{
  if( IsIconic(hwnd) )
    ShowWindow( hwnd, SW_RESTORE );  // If an icon, restore
  SetForegroundWindow( hwnd );          
  return false;// Force a call to ExitInstance and shut down
}

  // Register our class with Windows
  // Declare a WNDCLASS stucture and fill it
WNDCLASS wc;
wc.style          = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;  
wc.lpfnWndProc    = DefWindowProc;
wc.cbClsExtra     = 0;
wc.cbWndExtra     = 0;
wc.hInstance      = AfxGetInstanceHandle();
wc.hIcon          = LoadIcon(IDR_MAINFRAME);
wc.hCursor        = LoadCursor(IDC_ARROW);
wc.hbrBackground  = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName   = NULL;
wc.lpszClassName  = "My App";
AfxRegisterClass( &wc );

With another instance of this app already running, FindWindow never returns a window handle. The line AfxRegisterClass() returns true indicating registration was successful, but other instances still launch.

The funny thing is this code seems to work in antoher App of mine and it was basically cut and pasted into the new App.

Is there an easier way to perform this check? Perhaps through the framework itself? How can I allow only one instance of an application to run?
0
Comment
Question by:JagerM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1312175
Make sure your mainframe is also using this class for its window. Otherwise, the registering of the wndclass will work, but since there are no top-level windows of this class, FindWindow will return NULL.

0
 

Author Comment

by:JagerM
ID: 1312176
That did the trick. Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 215
Include multiple hostnames in this command? 3 56
Fibonacci challenge 11 141
Adjust the codes 3 63
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question