在MFC中运行单个实例? [英] Running single Instance in MFC?

查看:111
本文介绍了在MFC中运行单个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个MFC应用程序,我将其命名为setup.exe,如何运行我的应用程序的单个实例

(即setup.exe)。我做的是如果我的setup.exe已经存在,那么如果我再次尝试运行setup.exe,那么我将把已经打开的setup.exe带到前面。 />
这里我遇到了一个问题,即我正在安装microsoft的setup.exe来安装microsoft office,现在我尝试启动setup.exe,

而不是启动我的setup.exe,我得到了microsoft的setup.exe被带到了前面。我的setup.exe没有启动。这实际上是我为在应用程序类中运行单个实例而实现的。

Hi,

I am having an MFC application which I named it is as setup.exe,How can I run single Instance of my application
(i.e, setup.exe).What I had done is if my setup.exe already exists then if I try running setup.exe again then I am bringing the setup.exe which is already opened to front.
Here exactly I got struck with a problem i.e, I was installing microsoft's setup.exe for installing microsoft office and now during this time I tried launching my setup.exe,
Instead of launching my setup.exe ,I got microsoft's setup.exe was brought to front .My setup.exe is not launched.This is actually I implemented for running a single instance in the application class.

BOOL InitInstance()
{

  AppIsAllreadyRunning();
}

BOOL AppIsAllreadyRunning(BOOL bShow/*=TRUE*/)
{
	

    BOOL bRunning = FALSE;

	WCHAR szAppName[MAX_PATH] = {0};
	::wcscpy_s(szAppName, MAX_PATH, theApp.m_pszExeName);
	::wcscat_s(szAppName, MAX_PATH, L".exe");

	DWORD dwOwnPID = ::GetProcessId(::GetCurrentProcess());
	HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32* processInfo = new PROCESSENTRY32;
    processInfo->dwSize = sizeof(PROCESSENTRY32);
    int index = 0;
	while(::Process32Next(hSnapShot, processInfo) != FALSE)
    {
		if(!::wcscmp(processInfo->szExeFile, szAppName))
        {
            if(processInfo->th32ProcessID != dwOwnPID)
            {
                if(bShow)
		   ::EnumWindows(ShowAppEnum, processInfo->th32ProcessID);

                bRunning = TRUE;
                break;
            }
        }
    }

    ::CloseHandle(hSnapShot);
    delete processInfo;
    return bRunning;
}

BOOL CALLBACK ShowAppEnum(HWND hwnd, LPARAM lParam)
{
    DWORD dwID = 0;
	::GetWindowThreadProcessId(hwnd, &dwID) ;
	if(dwID == (DWORD)lParam)
	{
		if (!::IsWindowVisible(hwnd))
			::ShowWindow(hwnd,SW_SHOW); 
		::SetForegroundWindow(hwnd);
	}
	return TRUE;
}





最初我启动了setup.exe,现在我将setup.exe复制到另一个驱动器,我试过runnig那个location.Acutal结果exe应该启动但是又带来了已经从不同位置启动的setup.exe。

任何人都可以让我知道如何摆脱这个问题。

预计如果我在不同的驱动器中有相同的setup.exe,并且如果我尝试从所有位置运行它,则应该启动与所有驱动器对应的setup.exe。如果我运行相同的设置。来自同一位置的exe两次然后它必须显示现有的setup.exe。

在C#中我看到它是在GUID基础上完成的。但我不确定在MFC中。任何人都可以帮助。



提前谢谢。



Initially I launched my setup.exe and now I copied setup.exe to another drive and I tried runnig from that location.Acutal result the exe should get launched but again it is bringing the setup.exe which is already launched from different location.
Can anyone please let me know how can I get rid off this problem.
Expected is if I had same setup.exe in different drives and if I try runnig it from all the locations setup.exe's corresponding to all the drives should get launched.If I am running the same setup.exe from the same location twice then It has to show the existing setup.exe to front.
In C# I had seen it is done on GUID basis.But I am not sure in MFC.Can anyone kindly help .

Thanks in advance.

推荐答案

有几种方法可以做到这一点。您已实现的方法(通过当前正在运行的进程查找具有给定名称的应用程序并在找到它时将其置于前面)并不是最佳方法。它只比FindWindow方法稍好一点,我甚至都不会链接到它。



我个人喜欢共享内存段(或内存映射)的方法文件)是通过命名的互斥锁创建和访问的。它是共享内存,因此当实例运行并且另一个实例启动时,实例可以使用此区域来共享和交换信息。

这不仅允许您将应用程序限制为单个实例(如果以这种方式调用可执行文件,还允许您将命令行参数传递给当前运行的实例。



它有或2或3或任何你想要的东西)自从我做了这些事以来已经这么多年了,所以我真实地忘记了我在那里获得了我当时使用过的代码,但是请看看下面的CodeProject文章,看看它们是否适合你的需要。

CSingleInstance - 单一实例应用 [ ^ ]

单个实例A应用 [ ^ ]

将应用程序限制为单实例 - MFC方式 [ ^ ]



我有这种挥之不去的记忆,我可能已经使用了 CodeProject上的终极工具箱 [ ^ ], Ultimate Toolbox主页 [ ^ ],但我不记得班级的名字。



Soren Madsen
There are several ways of doing this. The method you have implemented (going through the currently running processes looking for an application with the given name and bringing it to the front if you find it) is not the best approach. It is only slightly better than the FindWindow approach, which I am not even going to link to.

Personally I like the approach where a shared memory segment (or memory mapped file) is created and accessed through a named mutex. It is shared memory so when an instance is running and another instance starts up, the instances can use this area to share and exchange information.
This not only allows you to limit your application to a single instance (or 2, or 3 or whatever you want), but also lets you pass command line parameters to the currently running instance if you invoke the executable that way.

It has been so many years since I did these things, so I have honestly forgotten where I got the code I used back then, but take a look at the following CodeProject articles to see if any of them fits your need.
CSingleInstance - Single Instance Apps[^]
Single Instance Application[^]
Limiting an application to a single Instance - the MFC way[^]

I have this lingering memory, that I might have used the class from The Ultimate Toolbox on CodeProject[^], The Ultimate Toolbox Home Page[^], but I don't remember the name of the class.

Soren Madsen


这篇关于在MFC中运行单个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆