Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
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.
C++
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;
}


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.
Posted
Updated 22-Sep-14 19:48pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-14 2:06am    
You need to use the debugger on the second instance of the process, it will quickly show you the flaw...
—SA

1 solution

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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-15 12:08pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900