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 initial function to be called in MFC and what it will do

11 Answers   Infosys,


what is the difference between compiling and building?

1 Answers  


What is the base class for MFC Framework ?

4 Answers  


Why Array Index starts from Zero

30 Answers   HCL,


How to handle command line arguements from simple MFC application ?

2 Answers   TCS,






if both base and derived class have the constructors if i create an object for derive class which class constructor is executed first

10 Answers   Siemens, Symphony,


Tell me the different controls in MFC ?

5 Answers  


What interface must be supported by an ActiveX control?

2 Answers  


How to update all the views whenver document got updated ?

1 Answers  


In SDI how many view's are attached to document object and in MDI how many view's are attached to Document object?

2 Answers   Wipro,


In VC++ How to transfer between one exe to another exe while running..

5 Answers   Wipro,


Do you have an idea about MFC?

0 Answers   C DAC, CDAC,


Categories