检查是否进程已经运行。
hImHere = CreateMutex(NULL, TRUE, "DialersIveBeenStartedMutex");
//
// Is there another one of us already here?
if ( ERROR_ALREADY_EXISTS == GetLastError() )
{
HWND hDialerWnd;
hDialerWnd = FindWindow(gszDialerClassName,
NULL);
SetForegroundWindow(hDialerWnd);
CloseHandle( hImHere );
return 0;
}
hImHere = CreateMutex(NULL, TRUE, "DialersIveBeenStartedMutex");
//
// Is there another one of us already here?
if ( ERROR_ALREADY_EXISTS == GetLastError() )
{
HWND hDialerWnd;
hDialerWnd = FindWindow(gszDialerClassName,
NULL);
SetForegroundWindow(hDialerWnd);
CloseHandle( hImHere );
return 0;
}
解决方案 »
- 这个客户端(UDP)写的对吗?感觉少了点什么
- Window消息机制
- ***今晚我想做回男人哈哈***
- 新人没钱没技术,只能冷天雪地裸体跪求C++超菜鸟问题
- SDK中关于 list box 的问题?
- console程序中如何测试main的返回值?在先等待!
- 怎样实现SDI中视图窗中文字的刷新?在线等待。。。
- CTreeCtrl树形控件动态添加节点不能保存
- 紧急求助:MDI程序, 主工程用另一个的工程编译好的mfc扩展dll,怎么在主工程里总是找不到dll导出的类呢??
- 我想把自己的函数制成一个文件,使用时用include 包含进来,应怎样做?
- chinaren 有什么漏洞可以把照片删掉
- 什么是回调函数?主要用在哪些方面,时候,要注意什么呢?糊涂中
const CString CLASS_NAME="MyProgram";
HANDLE hMutex=CreateMutex(NULL,FALSE,CLASS_NAME); if(hMutex==NULL)return FALSE; if(GetLastError() == ERROR_ALREADY_EXISTS )
{
hMutex=NULL;
CString strWindow;
strWindow.LoadString(IDR_MAINFRAME);
strWindow=strWindow.Left(strWindow.Find("\n",0));
HWND hWnd = FindWindowEx( NULL, NULL,NULL,strWindow); if ( hWnd != NULL ) {
ShowWindow(hWnd,SW_SHOW);
ShowWindow(hWnd,SW_RESTORE);
BringWindowToTop(hWnd);
SetForegroundWindow(hWnd);
}
return FALSE;
}
if(hWnd != NULL)
{
::ShowWindow(hWnd, SW_SHOW);
return FALSE;
}
在CYourApp::InitInstance()中:HWND hWnd = ::FindWindow(NULL,"你那程序窗口的标题");
if(hWnd != NULL)
{
::SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSZIE | SWP_SHOWWINDOW);
return FALSE;
}
#define LimitSingleInstance_H#include <windows.h> class CLimitSingleInstance
{
protected:
DWORD m_dwLastError;
HANDLE m_hMutex;public:
CLimitSingleInstance(TCHAR *strMutexName)
{
//be sure to use a name that is unique for this application otherwise
//two apps may think they are the same if they are using same name for
//3rd parm to CreateMutex
m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
m_dwLastError = GetLastError(); //save for use later...
}
~CLimitSingleInstance()
{
if (m_hMutex) //don't forget to close handles...
{
CloseHandle(m_hMutex); //do as late as possible
m_hMutex = NULL; //good habit to be in
}
} BOOL IsAnotherInstanceRunning()
{
return (ERROR_ALREADY_EXISTS == m_dwLastError);
}
};
#endif 添加一全局变量:
CLimitSingleInstance g_singleInstane;if (g_singleInstane.IsAnotherInstanceRunning()) {
// ref : AloneWolf or xstnt
...
}
CLimitSingleInstance g_singleInstane("{{A6B13EE4-A974-11d2-8DB7-00C04FB6E8F6}"}");
BOOL CCsdsApp::InitInstance()
///////////////////////////设置只能运行一个实例
{
SystemCsdsMutex = ::CreateMutex(NULL,TRUE,"LYCHPCSDSMutex");
if(GetLastError() == ERROR_ALREADY_EXISTS) {
CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd) {
if(::GetProp(pPrevWnd->GetSafeHwnd(),"LYCHPCSDSMutex")) {
if(pPrevWnd->IsIconic()) pPrevWnd->ShowWindow(SW_RESTORE);
pPrevWnd->SetForegroundWindow();
pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
return FALSE;
}
pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT);
}
::CloseHandle(SystemCsdsMutex);
return FALSE;
}
int WINAPI WinMain(.......)
{
........
........
Atom atom;
//确保程序只被运行一次
if(GlobalFindAtom("PROGRAM_RUNNING") == 0)
{
//说明程序还没运行
atom = GlobalAddAto("PROGRAM_RUNNING");
}
else
{
//说明程序已经运行,则这一次的实例就应该立即退出。
return 0;
}
..................
.................
return 0;
}