C++ (MFC) 单程序运行(防止多开程序)

C++ (MFC) 单程序运行(防止多开程序)

项目文件名:MFCAppTest

在 C*****App.cpp 文件中

CMFCAppTestApp::InitInstance 函数中
添加以下代码

	//避免程序的多开  xxxx为信号量的名字 可随意
	CreateMutex(NULL, TRUE, TEXT("MFCAppTest")); 
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
   
		//windowname为你的主窗体的标题,当然你也可以通过进程来找到主窗体。
		CWnd* cwnd = CWnd::FindWindow(NULL, TEXT("MFCAppTest"));
		if (cwnd)//显示原先的主界面
		{
   
			cwnd->ShowWindow(SW_SHOWNORMAL);
			cwnd->SetForegroundWindow();
		}
		return FALSE;
	}

完整代码


// MFCAppTest.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MFCAppTest.h"
#include "MFCAppTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCAppTestApp

BEGIN_MESSAGE_MAP(CMFCAppTestApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CMFCAppTestApp construction

CMFCAppTestApp::CMFCAppTestApp()
{
   
	// support Restart Manager
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CMFCAppTestApp object

CMFCAppTestApp theApp;


// CMFCAppTestApp initialization

BOOL CMFCAppTestApp::InitInstance()
{
   
	//避免程序的多开  xxxx为信号量的名字 可随意
	CreateMutex(NULL, TRUE, TEXT("MFCAppTest")); 
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
   
		//windowname为你的主窗体的标题,当然你也可以通过进程来找到主窗体。
		CWnd* cwnd = CWnd::FindWindow(NULL, TEXT("MFCAppTest"));
		if (cwnd)//显示原先的主界面
		{
   
			cwnd->ShowWindow(SW_SHOWNORMAL);
			cwnd->SetForegroundWindow();
		}
		return FALSE;
	}

	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// Create the shell manager, in case the dialog contains
	// any shell tree view or shell list view controls.
	CShellManager *pShellManager = new CShellManager;

	// Activate "Windows Native" visual manager for enabling themes in MFC controls
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// 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
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	CMFCAppTestDlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR 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
	}
	else if (nResponse == -1)
	{
   
		TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
		TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
	}

	// Delete the shell manager created above.
	if (pShellManager != NULL)
	{
   
		delete pShellManager;
	}

#ifndef _AFXDLL
	ControlBarCleanUp();
#endif

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


注意:

如果出现 CreateMutex 未定义
使用:CreateMutexW(Unicode)或CreateMutexA替换

相关推荐

  1. C++ (MFC) 程序运行防止程序)

    2024-01-13 21:34:03       69 阅读
  2. 如何在python程序内连续运行个代码

    2024-01-13 21:34:03       61 阅读
  3. rust - 使用文件锁防止应用

    2024-01-13 21:34:03       48 阅读
  4. Python 进程和线程在加速程序运行上的差别

    2024-01-13 21:34:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 21:34:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 21:34:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 21:34:03       87 阅读
  4. Python语言-面向对象

    2024-01-13 21:34:03       96 阅读

热门阅读

  1. go如何终止多个for select循环嵌套

    2024-01-13 21:34:03       53 阅读
  2. Redis面试题12

    2024-01-13 21:34:03       54 阅读
  3. 达梦数据实时同步工具DMHS常见故障处理

    2024-01-13 21:34:03       55 阅读
  4. Union-Find

    2024-01-13 21:34:03       62 阅读
  5. 【数模百科】2004-2023年美赛获奖论文下载

    2024-01-13 21:34:03       63 阅读
  6. 麒麟系统编写桌面点击可执行文件

    2024-01-13 21:34:03       94 阅读
  7. 作业-去重复统计(2)

    2024-01-13 21:34:03       52 阅读