Currently I am working on a mfc application and it should be named as setup.exe and done as well.Before getting into where I got struck ,I will let you what I had done. First I must prevent multiple Instances of my application.In the primary stage I created an event and checking ::getlasterror() whether the setup.exe already exists or not and if the .exe exists I am showing a messagebox.So,how many time I run the exe that many times the message boxes will get repeated. So,Instead of displaying that many messageboxes.I got a thought like,If my setup.exe is already existing then bring it to the front of the screen.So,I tried like this and it is working good enough.But here arised my actual problem i.e,Once I was installing some windows setup.exe and it's installation is going and in the meantime I tried running my setup.exe then I am getting the windows setup.exe to the front but not my application "setup.exe". This is actually I implemented in InitInstance as follows,
BOOL CMyApp::InitInstance()
{
CWinApp::InitInstance();
AppIsAllreadyRunning();
return TRUE;
}
BOOL CMyApp::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;
}
This is how I tried.Can anyone please let me know how can make my setup.exe different from other setup.exe,When I run my setup.exe it should only come to the front,as I explained above like first if I run a different setup.exe and some installing is going on and in the sametime if I run my setup.exe it has to get launched or if it already exists it has to come on to the front.But a different setup.exe which is already running is popping up to the front(this should not happen).