function GetProcessID(ProcessName : string ) : DWORD ;var Handle:tHandle; Process:tProcessEntry32A; GotProcess:Boolean;begin Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL,0); Showmessage(IntToHex(Handle,8)); Process.dwSize:=SizeOf(Process); Showmessage(IntToHex(Process.dwSize,8)); GotProcess := Process32First(Handle,Process); if GotProcess and (Process.szExeFile<>ProcessName) then repeat Showmessage(Process.szExeFile); GotProcess := Process32Next(Handle,Process); until (not GotProcess) or (Process.szExeFile=ProcessName); if GotProcess then Result := Process.th32ProcessID else Result := 0; CloseHandle(Handle);end;
Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0); Showmessage(IntToHex(Handle,8)); Process.dwSize:=SizeOf(Process); Showmessage(IntToHex(Process.dwSize,8)); GotProcess := Process32First(Handle,Process); <- Fails here.
The CreateToolhelp32Snapshot function can fail with an error code of ERROR_BAD_LENGTH for a variety of reasons. For example, this error can occur if the loader data table in the target process is corrupted or not initialized, or if an internal synchronization error has occurred between the calling process and the target process. Ensure that the target process was not started suspended, and try calling the function again.
Not really sure why, but maybe the struct for the tProcessEntry32A maybe diffrent.*EDIT* That does not seem to be the case. That error normally raises when you forget to tet the dwSize, but i've done that.
*EDIT* again, found the error. Embarcadero's TLHelp32 declaresth32DefaultHeapID: DWORD;where as it should be: th32DefaultHeapID: ULONG_PTR;
the fpc interprets it then not to be a pounter but a number. Which case, when declaring your TProcessEntry32, make sure it's a ULONG_PTR
ULONG_PTR works, is there any diffrence?
The Cause for this bug is the dwFlags of TProcessEntry32 in jwaTLHelp32 - its defined as a DWORD , it should be a ULONG_PTR. the Process32First and Process32Next Functions are expecting 304 bytes not 300. mantis Bug id - 16361