Jump to content


This is a ready-only archive of the InstallSite Forum. You cannot post any new content here. / Dies ist ein Archiv des InstallSite Forums. Hier können keine neuen Beiträge veröffentlicht werden.
Photo

Detect running process


1 reply to this topic

BennyTordrup

BennyTordrup
  • Full Members
  • 3 posts

Posted 01 August 2007 - 09:45

Hi

I have an installation that installs some files that a potential running process reads at startup. I would like for my installation to detect if this process is running and either kill the process or ask the user to kill the process.

The process in question is NOT installed by my installation - it is an external process.

Can this be done?

Best regards

Benny

m.nouryan

m.nouryan
  • Full Members
  • 23 posts

Posted 10 August 2007 - 16:47

Hi,

This is what I have done and it works:

Create a DLL file and implement a function that would receive the process name as parameter and would shut-down that process.

Call that function in your setup script (or you may directly call the DLL function in a custom action though I have not tried this myself)

Here is a function sample:
Please Note:
1) The code is developed in VC++ and might use some functions from MFC though you should be able to do it with pure windows API if you don’t want to use MFC.
2) I have taken some product specific parts of the code so it might not give you compile errors,


BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
DWORD dwProcessId;
if(GetWindowThreadProcessId(hwnd,&dwProcessId))
if(dwProcessId==lParam)
{
THIS IS THE WINDOW, SEND THE CLOSE/QUIT MESSAGE
}
return TRUE;
}

__declspec(dllexport) LONG ExitApplication(LPSTR ApplicationExeFileName)
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};


CString strExeFileName=ApplicationExeFileName;//<The Target Process EXE File Name>
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return 1;

// Fill in the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);

// Walk the snapshot of the processes, and for each process,
// display information.
if (Process32First(hProcessSnap, &pe32))
{
do
{
if(strExeFileName.CompareNoCase(pe32.szExeFile)==0)
{
//Method 1:
//Post WM_CLOSE to the thread's queue
if(::PostThreadMessage(pe32.th32ProcessID,WM_CLOSE,0,0)==0)
{
Message.Format(_T("WM_CLOSE - Last Error=%d"),::GetLastError());
AfxMessageBox(Message);
}

//Method 2:
//if it did not work, try to find the process window and
// send WM_CLOSE or WM_QUIT to the window
// do this in EnumWindowsProc call back function
EnumWindows(EnumWindowsProc,pe32.th32ProcessID);


//Method 3:
//if Nothing works, Force it to shut down:
// NOT RECOMMENDED, use as last resort
HANDLE hProcess=::OpenProcess(PROCESS_TERMINATE,TRUE,pe32.th32ProcessID);
if(hProcess!=NULL)
{
if(::TerminateProcess(hProcess, 0)==0)
{
Message.Format(_T("TerminateProcess - Last Error=%d"),::GetLastError());
AfxMessageBox(Message);
}
}
::CloseHandle(hProcess);
}
}while (Process32Next(hProcessSnap, &pe32));
}

CloseHandle (hProcessSnap);
return 0;
}