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)
{
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.