vc程序只运行一次的判断——20100723
2010-07-23 16:11
211 查看
在工程下对应的app类中的InitInstance函数加入下列代码即可(红色部分为加入代码):
BOOL CAutoUpdateApp::InitInstance()
{
HANDLE hMap=CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, 128, AfxGetAppName());
if(hMap == NULL) // 如果创建失败
{
AfxMessageBox("创建用于互斥运行的内存映射文件对象失败!",
MB_OK|MB_ICONSTOP);
return FALSE;
}
else if(GetLastError() == ERROR_ALREADY_EXISTS)
{
LPVOID lpMem = MapViewOfFile(hMap, FILE_MAP_WRITE, 0,0,0);
CString str = (char)lpMem;
UnmapViewOfFile(lpMem);
CloseHandle(hMap);
str="程序已在运行!";
AfxMessageBox(str, MB_OK|MB_ICONSTOP);
return FALSE;
}
else
{
LPVOID lpMem = MapViewOfFile(hMap, FILE_MAP_WRITE, 0,0,0);
//strcpy((char)lpMem, "程序正在运行!");
UnmapViewOfFile(lpMem);
CString str = (char)lpMem;
str.Format("%s/n程序开始运行!",AfxGetAppName());
AfxMessageBox(str, MB_OK|MB_ICONSTOP);
}
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
CAutoUpdateDlg 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
}
CloseHandle(hMap);
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
vc6.0下测试通过,可以放心使用。
BOOL CAutoUpdateApp::InitInstance()
{
HANDLE hMap=CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, 128, AfxGetAppName());
if(hMap == NULL) // 如果创建失败
{
AfxMessageBox("创建用于互斥运行的内存映射文件对象失败!",
MB_OK|MB_ICONSTOP);
return FALSE;
}
else if(GetLastError() == ERROR_ALREADY_EXISTS)
{
LPVOID lpMem = MapViewOfFile(hMap, FILE_MAP_WRITE, 0,0,0);
CString str = (char)lpMem;
UnmapViewOfFile(lpMem);
CloseHandle(hMap);
str="程序已在运行!";
AfxMessageBox(str, MB_OK|MB_ICONSTOP);
return FALSE;
}
else
{
LPVOID lpMem = MapViewOfFile(hMap, FILE_MAP_WRITE, 0,0,0);
//strcpy((char)lpMem, "程序正在运行!");
UnmapViewOfFile(lpMem);
CString str = (char)lpMem;
str.Format("%s/n程序开始运行!",AfxGetAppName());
AfxMessageBox(str, MB_OK|MB_ICONSTOP);
}
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
CAutoUpdateDlg 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
}
CloseHandle(hMap);
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
vc6.0下测试通过,可以放心使用。
相关文章推荐
- vc程序只运行一次的判断
- VC程序只运行一次实例
- VC++ 一次只能运行一个程序设置
- 使程序只能运行一次,用互斥量来判断程序是否已运行,并设置窗体背景色
- VC中判断程序是否已运行
- VC程序只运行一次实例
- c++ 或者 vc++中判断程序实例是否运行
- VC程序只允许运行一次
- VC判断指定程序是否运行,杀掉指定进程
- vc程序只运行一次实例
- VC程序只运行一次实例
- 在VC中实现一次执行一个进程,如果同时打开两个,则报该程序已经运行
- VC控制程序只运行一次效果
- 自己的程序只运行一次
- Win7下运行VC程序UAC权限问题
- NSIS:判断程序是否运行并进行卸载
- C#:判断当前程序是否通过管理员运行
- VC 如何使程序运行后自己删除自己
- VC 判断进程是否是以管理员权限运行,并且判断是否是用户进程而非服务进程
- VC程序编译成exe后需msvcr100.dll、msvcp100.dll才能运行的解决方法