I want to kill an exe before I uninstall.
I am using uninstall script code which worked fine on windows server 2003, but is not working consistently on windows server 2008 R2. ( I works sometimes but not always)

I've noticed that when it does not work, that it returning the number of processes running as 128( which seems a suspiciously round computer number)

Anyone got any ideas?

Thank you in advance

// Process information functions.

prototype NUMBER Kernel32.K32EnumProcesses(POINTER, NUMBER, BYREF NUMBER);
prototype NUMBER Kernel32.K32EnumProcessModules(NUMBER, BYREF NUMBER, NUMBER,
prototype NUMBER Kernel32.K32GetModuleFileNameEx(NUMBER, NUMBER, BYREF STRING,

// Function: _Process_Running
// Description: Determines if the specified process is running in memory.
// Parameters: szAppName - Name of the application to check.
// Returns: 1 - The process is running.
// 0 - The process is not running.
// -1 - Error querying task list

function NUMBER ProcessRunning(hMSI,szAppName)
NUMBER nvRunning; // Process is running
NUMBER nvProcessIDs(512); // Array of process IDs
NUMBER nvBytesReturned; // Number of bytes returned in process ID array
NUMBER nvProcesses; // Number of processes running
NUMBER nvIndex; // Loop index
NUMBER nvProcessHandle; // Handle to a process
NUMBER nvModuleHandle; // Handle to a process module
NUMBER nvBytesRequired; // Number of bytes required to store values
POINTER pvProcessIDs; // Pointer to process ID array
STRING svModuleName; // Module name
STRING svFileName; // Module filename
STRING szMessage; // Message String
// The psapi.dll reads the Windows NT performance database. The DLL
// is part of the Win32 SDK.

// Could not load psapi.dll.

WriteToLogFile(hMSI,"ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE +
"]. Rolling back uninstall");

return -1;

// Get the PIDs of all currently running processes.

nvRunning = 0;

pvProcessIDs = ArrayToPointer(nvProcessIDs);

K32EnumProcesses(pvProcessIDs, 512, nvBytesReturned);

// Determine the number of process IDs retrieved. Each process ID

nvProcesses = nvBytesReturned / PROCESSID_LENGTH;

Sprintf(szMessage, "INFO: ProcessRunning: nvRunning = %d, nvProcesses = %d", nvRunning,nvProcesses);
WriteToLogFile(hMSI, szMessage);

// Get the executable associated with each process, and check if
// its filename matches the one passed to the function.

for nvIndex = 1 to nvProcesses
// Get a handle to the process.

nvProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, 0, nvProcessIDs(nvIndex));
Sprintf(szMessage, "INFO: ProcessRunning: nvIndex = %d, nvProcessHandle = %d", nvIndex,nvProcessHandle);
WriteToLogFile(hMSI, szMessage);
if nvProcessHandle != 0 then
// Get a handle to the first module in the process, which
// should be the executable.

if K32EnumProcessModules(nvProcessHandle, nvModuleHandle,
PROCESSID_LENGTH, nvBytesRequired) != 0 then
// Get the path of the module.

if K32GetModuleFileNameEx(nvProcessHandle, nvModuleHandle,
svModuleName, SizeOf(svModuleName)) != 0 then
// Extract the filename (without an extension) from
// the path.

ParsePath(svFileName, svModuleName, FILENAME_ONLY);
Sprintf(szMessage, "INFO: ProcessRunning: nvRunning = %d, nvProcesses = %d, svFileName = %s, svModuleName = %s, szAppName = %s ", nvRunning,nvProcesses,svFileName,svModuleName,szAppName);
WriteToLogFile(hMSI, szMessage);
if StrCompare(svFileName, szAppName) = 0 then
// The process module matches the application
// name passed to the function.

nvRunning = 1;

goto ProcessRunningEnd;


if UnUseDLL(PSAPI_FILE) < 0 then
// Log this failure but ignore it since the task has been completed.
WriteToLogFile(hMSI,"ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE +
"]. Will ignore.");

return nvRunning;
0 Comments   [ - ] Hide Comments


Please log in to comment

There are no answers at this time
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
Answer this question or Comment on this question for clarity