Well... time for necro-posting
Last week I was struggling with TlHelp32 (replaced by JwaTlHelp32, jwawinbase, jwawinnt) to reanimate an ancient Delphi project written not by me (CHRUnLoad was an utility/hack to fake import characters to Wizardry 8 game). I've ran into exact problem described in this topic.
sizeof(PROCESSENTRY32) gave me 296 bytes and #24 error (ERROR_BAD_LENGTH)
I've made a stupid fix to increase PROCESSENTRY32 size by adding tmp1,tmp2,tmp3,tmp4,tmp5:DWORD; to it (i.e.additional 20 bytes)
Actually I've just added the "tmp" variables until I got sizeof(PROCESSENTRY32)=316 which gave error #18 (ERROR_NO_MORE_FILES) which looks ok
Eventually the program works as expected and provides all necessary data.
The complete PROCESSENTRY32 in my case is:
type PROCESSENTRY32 = record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD; // this process
th32DefaultHeapID: ULONG_PTR;
th32ModuleID: DWORD; // associated exe
cntThreads: DWORD;
th32ParentProcessID: DWORD; // this process's parent process
pcPriClassBase: LONG; // Base priority of process's threads
dwFlags: DWORD;
szExeFile: array [0..MAX_PATH-1] of Char; // Path
tmp1,tmp2,tmp3,tmp4,tmp5:DWORD; //WHAT THE HELL IS THAT????? 20 bytes missing????
end;
instead of the same record in "JwaTlHelp32.pas,v 1.11 2007/09/05 11:58:52 dezipaitor" found in Lazarus 1.6 / FPC 3.0.0
type tagPROCESSENTRY32 = record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD; // this process
th32DefaultHeapID: ULONG_PTR;
th32ModuleID: DWORD; // associated exe
cntThreads: DWORD;
th32ParentProcessID: DWORD; // this process's parent process
pcPriClassBase: LONG; // Base priority of process's threads
dwFlags: DWORD;
szExeFile: array [0..MAX_PATH - 1] of Char; // Path
end;
Moreover I had to copy-paste all the relevant structures and procedures into my code, because for some reason JwaTlHelp32 ignored any changes I've made and always gave up sizeof(tagPROCESSENTRY32)=296 no matter what I've changed. But I might have been doing something wrong, e.g. I don't remember if I added any sort of additional variables to the record. P.S. Increasing the length of "szExeFile: array [0..MAX_PATH-1] of Char;" array works only once? E.g. I get 296->300 bytes size if I write "array [0..MAX_PATH-0] of Char" but if I write "array [0..MAX_PATH+3] of Char" the size of the whole record still remains 300 bytes.
The function giving the error #24 is
interface
function Process32First(hSnapshot: HANDLE; var lppe: PROCESSENTRY32): BOOL; stdcall;//always ANSI!
...
implementation
function Process32First; external 'kernel32.dll' name 'Process32First'; //ANSI
Relevant to Win7x64
Seems like Kernel32.dll version is 6.1.7601.17514 (818kb) modified 21.11.2010 5:24
I'm using {$MODE Delphi} as it was a project imported from Delphi source.
P.S. The problem looks solved for my task by that stupid hack I've made, so it's rather a note than any request for help. Moreover, I don't really think anyone would use anything like that now except for reviving ancient software. So there is absolutely nothing urgent in the problem. But having JwaTlHelp32 running smoothly is a good idea.
(UPD) Looks like dwSize = sizeof(PROCESSENTRY32); must be set before sending lppe to kernel32.