让程序只启动一个实例(from: http://topic.csdn.net/t/20010427/08/106676.html)
2008-07-16 16:09
651 查看
下面的两种方法都是可以的
HANDLE hMap=CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,128,
"MutexRuning");
if(hMap==NULL)
{
AfxMessageBox("创建内存映射文件失败",MB_OK|MB_ICONSTOP);
return FALSE;
}
else if(GetLasError()==ERROR_ALREADY_EXISTS)
{
LPVOID lpMem=MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
CString str=(char *)lpMem;
UnmapViewOfile(lpMem);
CloseHandle(hMap);
AfxMessageBox(str,MB_OK|MB_ICONSTOP);
return FALSE;
}
else
{
//是第一次运行
LPVOID lpMem=MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
//程序运行描述信息
strcpy((char *) lpMem,"xxxx正在运行!");
UnmapViewOfFile(lpMem);
}
// HWND hOldInstance;
// if ((hOldInstance =::FindWindow(NULL, _T("LANMonitorClient"))) != NULL)
// {
// ShowWindow(hOldInstance,SW_RESTORE|SW_SHOWNORMAL);
// SetForegroundWindow(hOldInstance);
// return FALSE;
// }
HANDLE hMap=CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,128,
"MutexRuning");
if(hMap==NULL)
{
AfxMessageBox("创建内存映射文件失败",MB_OK|MB_ICONSTOP);
return FALSE;
}
else if(GetLasError()==ERROR_ALREADY_EXISTS)
{
LPVOID lpMem=MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
CString str=(char *)lpMem;
UnmapViewOfile(lpMem);
CloseHandle(hMap);
AfxMessageBox(str,MB_OK|MB_ICONSTOP);
return FALSE;
}
else
{
//是第一次运行
LPVOID lpMem=MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
//程序运行描述信息
strcpy((char *) lpMem,"xxxx正在运行!");
UnmapViewOfFile(lpMem);
}
// HWND hOldInstance;
// if ((hOldInstance =::FindWindow(NULL, _T("LANMonitorClient"))) != NULL)
// {
// ShowWindow(hOldInstance,SW_RESTORE|SW_SHOWNORMAL);
// SetForegroundWindow(hOldInstance);
// return FALSE;
// }
相关文章推荐
- 清问一个Windows的程序的LDT是谁创建的?http://topic.csdn.net/t/20061228/17/5262577.html
- 程序只启动一个实例的几种方法(转自http://blog.csdn.net/starlee)
- 转帖 一个很有用但不常用到的传值方法! http://topic.csdn.net/u/20070124/22/09d43606-4119-4407-bfa6-8b2890ffbac7.html
- 9003错误,sqlserver无法启动,HELP!!!十万火急~~~!!http://topic.csdn.net/t/20060708/09/4867769.html
- [推荐] 哈佛大学凌晨4点半的景象(zz from :http://topic.csdn.net/u/20100806/09/3D19DC5F-ACFF-49A1-993A-018F47FCDB77.html)
- 怎么在获得一个库里面所有的表名?http://topic.csdn.net/t/20020801/09/915234.html
- Get Response from Http (Referenced to http://topic.csdn.net/t/20050115/23/3729009.html)
- 函数的简单应用(原帖地址:http://topic.csdn.net/u/20090317/16/c7f5c2e3-293d-4d67-85be-0a4a17d52855.html)
- 聚焦索引与非聚焦索引及其查询效率(http://topic.csdn.net/u/20080303/11/F3AA3990-20EA-479C-9B15-A5318507291C.html)
- C#.NET禁止一个程序启动多个实例
- 试着解决http://topic.csdn.net/u/20090610/20/92c3d2eb-4ee5-4df1-bc09-2fd32254fa06_3.html
- C#.NET禁止一个程序启动多个实例
- 如何用Java 实现 Excel 表达式的解析(摘自:http://topic.csdn.net/t/20030408/17/1634982.html#)
- 当窗口大小变化时,始终让底图居中--转载http://topic.csdn.net/t/20060824/16/4973302.html
- http://topic.csdn.net/u/20091028/23/b5a8884a-778f-4535-8779-ae6453ada88e.html
- temp_http://topic.csdn.net/t/20040130/02/2687082.html
- 如何把标准输出重定向为指定缓冲区 http://topic.csdn.net/t/20020111/16/472320.html
- http://topic.csdn.net/u/20100424/21/c5545aac-231d-42f0-8bc4-8e058b189073.html
- C#.NET禁止一个程序启动多个实例
- http://topic.csdn.net/t/20061023/19/5103384.html