首先是方法:

// IsAlreadyRunning - 是否已经运行
BOOL IsAlreadyRunning()
{
    BOOL bRet = FALSE;
    HANDLE hMutex = ::CreateMutex(NULL, TRUE, _szAppName);

    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        bRet = TRUE;
        HWND hWnd = FindWindow(_szAppName, _szTitle);
        SetForegroundWindow(hWnd);
    }
    if (hMutex)
    {
        ::ReleaseMutex(hMutex);
    }
    return bRet;
}

然后在程序开始处调用:

// WinMain - 程序进入点
int CALLBACK WinMain(HINSTANCE hInstance,    //应用程序实例句柄
                    HINSTANCE hPrevInstance,    //16位系统中,该值指向当前程序的前一个实例,Win32 系统中,hPrevInstance总是为NULL
                    LPSTR lpCmdLine,    //指向应用程序命令行的字符串的指针,不包括执行文件名
                    int nCmdShow)    //指明窗口如何显示
{
    if (IsAlreadyRunning()) return 0;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-09-16
  • 2022-12-23
  • 2021-05-30
猜你喜欢
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2021-08-14
  • 2021-06-08
  • 2021-08-07
  • 2021-06-26
相关资源
相似解决方案