Home / Tips / VC++ Tips / VC++: How to ensure that at a time only one instance of an application runs in the memory?

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

VC++: How to ensure that at a time only one instance of an application runs in the memory?


How to ensure that at a time only one instance of an application runs in the memory?


Some applications like MS-Power Point allow only one instance of the application to run in the memory. It means that if PowerPoint application is already loaded and if we try to open another '.ppt' file the file gets opened in the same instance of PowerPoint. We can also achieve this using the following code.
In the frame window class write a function as shown below:

int CMainFrame::oncheck( )
{
return 0xA1B2C3D4 ;
}

Nothing special about the number 0xA1B2C3D4. We can give any unique number.
Make the following entry in message map of frame window class.

ON_MESSAGE ( WM_USER, oncheck )
BOOL CMyApp::InitInstance( )
{
CWnd *check = CWnd::FindWindow ( 0, "Hello World" ) ;
if ( check && check -> SendMessage ( WM_USER ) == 0xA1B2C3D4 )
{
check -> SetForegroundWindow( ) ;
check -> ShowWindow ( SW_SHOWNORMAL ) ;
}
else
{
// Code added by AppWizard
m_pMainWnd -> ShowWindow ( SW_SHOW ) ;
m_pMainWnd -> UpdateWindow( ) ;
return TRUE ;
}
return 0 ;
}

In the above code "Hello World" must be the title of the window.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts