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
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 |
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 |
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 |
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 |
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 |
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,
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 |
What is the initial function to be called in MFC and what it will do
what is the difference between compiling and building?
What is the base class for MFC Framework ?
Why Array Index starts from Zero
How to handle command line arguements from simple MFC application ?
if both base and derived class have the constructors if i create an object for derive class which class constructor is executed first
Tell me the different controls in MFC ?
What interface must be supported by an ActiveX control?
How to update all the views whenver document got updated ?
In SDI how many view's are attached to document object and in MDI how many view's are attached to Document object?
In VC++ How to transfer between one exe to another exe while running..
Do you have an idea about MFC?