Hi All,
I have created one MFC Dialog Based application.now if i am
running the application its working fine,instead of closing
he application i minimized the application,if i run the
application again,i am getting the Dialog.

I want to prevent the calling of application multiple times.
please give me the code and let me know in which method i
need to make changes.

Praveer

Answers were Sorted based on User's Feedback



Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / revanth

BOOL CSampleApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP
if an application
// manifest specifies use of ComCtl32.dll version 6
or later to enable
// visual styles. Otherwise, any window creation
will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control
classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();

AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to
reduce the size
// of your final executable, you should remove from
the following
// the specific initialization routines you do not
need
// Change the registry key under which our settings
are stored
// TODO: You should modify this string to be
something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated
Applications"));

if(NULL != ::CreateMutex(NULL, TRUE, _T
("CSingleInstanceApp")))
{
long dwError = ::GetLastError();
if(dwError == ERROR_ALREADY_EXISTS)
return false;
}

CSampleDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the
dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the
dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE
so that we exit the
// application, rather than start the
application's message pump.
return FALSE;
}

Is This Answer Correct ?    6 Yes 2 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / rajaram b

if(NULL != ::CreateMutex(NULL, TRUE,
_T("CSingleInstanceApp")))
{
long dwError = ::GetLastError();
if(dwError == ERROR_ALREADY_EXISTS)
EndDialog(IDOK);
}

Paste the above code snippet inside the "InitInstance"
function.

Is This Answer Correct ?    6 Yes 3 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / phaniram

if(NULL != ::CreateMutex(NULL, FALSE,_T
("CSingleInstanceApp")))
{
long dwError = ::GetLastError();
if(dwError == ERROR_ALREADY_EXISTS)
EndDialog(NULL,IDOK);
else
{
CDlgMutexDlg dlg;
m_pMainWnd = &dlg;
nResponse = dlg.DoModal();
}
}

Is This Answer Correct ?    6 Yes 4 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / sreekanth

A: You need to create a named mutex semaphore when you
start your application. When the second one starts it tries
to get access to the mutex but will fail...


Code:
// app.h
class CYourApp : public CWinApp
{
...

private:
HANDLE hMutex;
};

// app.cpp
BOOL CYourApp::InitInstance()
{
// Create mutex
hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");

switch(::GetLastError())
{
case ERROR_SUCCESS:
// Mutex created successfully. There is no instance
running
break;

case ERROR_ALREADY_EXISTS:
// Mutex already exists so there is a running
instance of our app.
return FALSE;

default:
// Failed to create mutex by unknown reason
return FALSE;
}
}

Is This Answer Correct ?    2 Yes 0 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / harini

Write the constructor of the dialog in private of the class by making the class as single instance class.

Is This Answer Correct ?    1 Yes 1 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / krishna

Try this....

//...
BOOL CWinAppEx::EnableTokenPrivilege( LPCTSTR
lpszSystemName,
BOOL
bEnable /*TRUE*/ )
{
ASSERT( lpszSystemName != NULL );
BOOL bRetVal = FALSE;

if( ::GetVersion() < 0x80000000 ) // NT40/2K/XP //
afxData ???
{
HANDLE hToken = NULL;
if( ::OpenProcessToken( ::GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken ) )
{
TOKEN_PRIVILEGES tp = { 0 };
if( ::LookupPrivilegeValue( NULL,
lpszSystemName, &tp.Privileges[0].Luid ) )
{
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = ( bEnable ?
SE_PRIVILEGE_ENABLED : 0 );


// To determine whether the function
adjusted all of the
// specified privileges, call GetLastError:

if( ::AdjustTokenPrivileges( hToken, FALSE,
&tp,
sizeof( TOKEN_PRIVILEGES ),
(PTOKEN_PRIVILEGES)NULL, NULL ) )
{
bRetVal = ( ::GetLastError() ==
ERROR_SUCCESS );
}
}
::CloseHandle( hToken );
}
}

return bRetVal;
}
//...

Is This Answer Correct ?    0 Yes 1 No

Hi All, I have created one MFC Dialog Based application.now if i am running the application its w..

Answer / praveer

Hi,
i wrote the code as you told still its not
working,here is my code,please have a look
/////////////////////////////////////////////////////////////
BOOL CDlgApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce
the size
// of your final executable, you should remove from the
following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a
shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC
statically
#endif


if(NULL != ::CreateMutex(NULL, TRUE,_T("CSingleInstanceApp")))
{
long dwError = ::GetLastError();
if(dwError == ERROR_ALREADY_EXISTS)
EndDialog(NULL,IDOK);
}

CDlgDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();




if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that
we exit the
// application, rather than start the application's
message pump.

return FALSE;
}
////////////////////////////////

Is This Answer Correct ?    3 Yes 5 No

Post New Answer

More MFC Interview Questions

what is the difference between SDI and MDI

15 Answers   CMC, HCL, Siemens,


what is the component of CLR ?

8 Answers   AZTEC, Symphony,


what is the size of a process

2 Answers   E Logic, IBM,


What is the size of class

18 Answers   Aircom,


How you create a button dynamically?

2 Answers  






I want recent paper pattern for HP company?

0 Answers   HP,


What is model and modeless dialog box ? Give some examples?

2 Answers   Mphasis,


General purpose classes in MFC

1 Answers   TCS,


How can update edit control data of an executing application from other application?

3 Answers   HP,


Which CPrintInfo member variable must be set for multiple page printouts?

1 Answers  


What is a critical section and how is it implemented?

2 Answers  


How many types of dialog box are their

10 Answers   Honeywell,


Categories