Hello!
My service (not windowed application) for winXP (32) creates a list of processes (like task manager) and setts every process to run in just one CPU in multiproccessor system.
1) when I try to get the handle of the process I get error: "The handle is invalid". If I remove "CloseHandle" for "OpenProcess" and run the service again, I get error "The parameter is incorrect". I tried with "PROCESS_ALL_ACCESS" and "true" combinations in "OpenProcess", but the problem still the same.
2) THe procedure "ProcessList" runs once a minute and reports a different (!?) handle for the same process every time, e. g. for "explorer" reports handle 455 first time and next time reports handle 856 for "explorer", and soon on.
Any idea?
Type
PrcRec = packed record // the object
Prcs: String;
CPUnr: Cardinal;
end;
var
ProcList: packed array of PrcRec; // The global variable
procedure TMyService.ProcessList;
var CPUcnt, i: Cardinal;
Info: SYSTEM_INFO;
Flag: BOOL;
SSHandle, ProcHandle: THandle;
Snapshop: TProcessEntry32;
vMaskProcess, vMaskSystem: Cardinal;
begin
SetLength(ProcList, 0);
GetSystemInfo(Info);
CPUcnt:=Info.dwNumberOfProcessors;
SSHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SSHandle<>THandle(0) then begin
Snapshop.dwSize:=SizeOf(Snapshop);
Flag:=Process32First(SSHandle, Snapshop);
i:=-1;
While Integer(Flag)<>0 do begin
Inc(i);
// The problematic function
ProcHandle:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, Snapshop.th32ProcessID);
if CPUcnt>1 then SetProcessAffinityMask(ProcHandle, CPUcnt -1);
SetLength(ProcList, i +1);
ProcList[i].Prcs:=Snapshop.szExeFile;
ProcList[i].CPUnr:=CPUcnt -1; // :=GetProcessAffinityMask(ProcHandle, vMaskProcess, vMaskSystem);
CloseHandle(ProcHandle);
Flag:=Process32Next(SSHandle, Snapshop);
end;
CloseHandle(SSHandle);
end;
end;
Thanks in advance, Adaltino.
.