VC小技巧(1)---禁止同一应用程序同时运行

来源:互联网 发布:赌场套利软件 编辑:程序博客网 时间:2024/06/06 14:19
有时候为了避免不必要的错误,应防止同一应用程序被打开两个实例

以下一个函数可以达到此项目的,挺有用的!

BOOL C××App::AlreadyRunning()
{
    BOOL bFound = FALSE;

    // Try to create a mutex with the app's name
    HANDLE hMutexOneInstance = ::CreateMutex(NULL,TRUE,_T(AfxGetAppName()));

    // Already there...means that we are already running an instance
    if(::GetLastError() == ERROR_ALREADY_EXISTS)
        bFound = TRUE;

    // Release the mutex
    if(hMutexOneInstance)
        ::ReleaseMutex(hMutexOneInstance);

    return(bFound);
}

只要在
BOOL C**App::InitInstance()
{
    // Is it already running?
    if(AlreadyRunning())
    {
        // Yep...get out now
        AfxMessageBox(IDS_ALREADY_RUNNING,MB_ICONWARNING);
        return(FALSE);
    }
    。。。。。

}
原创粉丝点击