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 are the special requirements for dialog box resources used in a form view?
What is the use of UpdateData funciton ?
What is serialization ?which function is responsible for serializing data ?
How WM_PAINT message gets called in MFC,please explain it . a)Who calls the WM_PAINT message? b)When it gets called? c)how it comes to message queue? Please Explain it
if both base and derived class have the constructors if i create an object for derive class which class constructor is executed first
Will there be any difference in the image buffer size if it is loaded in from CString to LPTSTR using GetBuffer()? lptstr = string.GetBuffer(0);
What is the use of CCmdTarget ?
Tell me about different kinds of synchranization objects ?
How to handle dynamic menus in MFC?
What is the base class for most MFC classes?
How we can findout Memoryleaks, In what way it can be avoided
Tell me the different controls in MFC ?