Must admit that I have never dealt with multiple users before, but maybe you could try a different approach?
WinXP allows the use of Psapi.dll to give you lots of power over running processes and threads...maybe that will be more successfull??...have a try and say if it worked
Code:
#include <windows.h>
#include <iostream>
#include <cstring>
const int MAX_PROCESSES = 300,//arbitary number...maybe more needed
MAX_MODULES = 50;//again - maybe more needed
const char* szPathToChange = "calc.exe";//name of app to kill
/*Sigh...I have old headers at the moment....so I must load the
PSAPI funcs manually - you might be able to skip this - should you wish to*/
typedef BOOL(WINAPI *ENUMPROCESSMODULES)(HANDLE,HMODULE *, DWORD,LPDWORD);
typedef BOOL(WINAPI *ENUMPROCESSES)(DWORD *,DWORD,DWORD *);
typedef DWORD(WINAPI *GETMODULEBASENAME)(HANDLE,HMODULE,LPTSTR,DWORD);
ENUMPROCESSMODULES lpfEnumProcessModules;
ENUMPROCESSES lpfEnumProcesses;
GETMODULEBASENAME lpfGetModuleBaseName;
void CheckProcess(DWORD dwPID)
{
HMODULE hModArray[MAX_MODULES];//array of handles to loaded modules
DWORD dwNoModules;//number of modules in process
char szPath[MAX_PATH+1];//name of module
//Get process handle
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwPID);
if(!dwPID)//if unable to open handle
{
std::cout << "Unable to open handle to PID - ";
std::cout << dwPID << std::endl;
return;
}
__try//Structured Exception Handling....ensure close Proc Handle
{
if(!lpfEnumProcessModules(hProc,hModArray, sizeof(hModArray),
&dwNoModules))//find modules in process
{
std::cout << "Unable to enumerate modules of PID - ";
std::cout << dwPID << std::endl;
return;
}
for(int i = 0;i < dwNoModules;++i)
{
if(lpfGetModuleBaseName(hProc,hModArray[i], szPath,
MAX_PATH))//Get name of each process
{
//is it the one we are interested in?
if(strcmp(szPath,szPathToChange) == 0)
{
std::cout << "Found an instance under PID ";
std::cout << dwPID << " - Terminating" << std::endl;
TerminateProcess(hProc,0);//kill it!
}
}
}
}
__finally
{
CloseHandle(hProc);//close handle to opened process
}
}
int main(void)
{
DWORD dwPIDArray[MAX_PROCESSES];//array of Process IDs
DWORD dwNoProcesses;//Number of PIDs
//Load the PSAPI library (WinNT,2k,XP only)
HMODULE hLib = LoadLibrary("C:\\WINDOWS\\system32\\PSAPI.dll");
if(!hLib)
{
std::cout << "Unable to load PSAPI" << std::endl;
return 1;
}
__try
{
//find the 3 functions we need for this code
lpfEnumProcesses = reinterpret_cast<ENUMPROCESSES>
(GetProcAddress(hLib,"EnumProcesses"));
if(!lpfEnumProcesses)
{
std::cout << "Unable to find EnumProcesses Func!" << std::endl;
return 1;
}
lpfEnumProcessModules = reinterpret_cast<ENUMPROCESSMODULES>
(GetProcAddress(hLib,"EnumProcessModules"));
if(!lpfEnumProcessModules)
{
std::cout << "Unable to find EnumProcessModules Func!" << std::endl;
return 1;
}
lpfGetModuleBaseName = reinterpret_cast<GETMODULEBASENAME>
(GetProcAddress(hLib,"GetModuleBaseNameA"));
if(!lpfGetModuleBaseName )
{
std::cout << "Unable to find GetModuleBaseName Func!" << std::endl;
return 1;
}
//Now enumerate all processes on system
if(!lpfEnumProcesses(dwPIDArray, sizeof(dwPIDArray), &dwNoProcesses))
{
std::cout << "Unable to enumerate Processes!" << std::endl;
return 1;
}
for(int i = 0;i < dwNoProcesses/sizeof(DWORD);++i)
{
CheckProcess(dwPIDArray[i]);//examine process
}
}
__finally
{
FreeLibrary(hLib);//release the library
}
return 0;
}