Recent

Author Topic: sizeof(PROCESSENTRY32) on x64 wrong size?  (Read 2547 times)

lyanidle

  • Newbie
  • Posts: 1
sizeof(PROCESSENTRY32) on x64 wrong size?
« on: June 22, 2018, 01:18:00 pm »
Hi,

i was trying to port some C-Code to pascal and encountered with some weird error.
The C-Code is very simple:

Code: Pascal  [Select][+][-]
  1.         PROCESSENTRY32 entry;
  2.         entry.dwSize = sizeof(PROCESSENTRY32);
  3.         HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  4.         if (Process32First(snapshot, &entry) == TRUE) {
  5. ....
  6.  

In pascal, the ported code returned an error at Process32First with GetLastError() 24 (BAD LENGTH).

I compared sizeof(PROCESSENTRY32) in visual studio with sizeof(PROCESSENTRY32) in pascal and noticed that in visual studio the size is 304 while on pascal it is 300.

This comes because of the struct:

Code: Pascal  [Select][+][-]
  1.   tagPROCESSENTRY32 = record
  2.     dwSize: DWORD;
  3.     cntUsage: DWORD;
  4.     th32ProcessID: DWORD;          // this process
  5.     th32DefaultHeapID: ULONG_PTR;
  6.     th32ModuleID: DWORD;           // associated exe
  7.     cntThreads: DWORD;
  8.     th32ParentProcessID: DWORD;    // this process's parent process
  9.     pcPriClassBase: LONG;          // Base priority of process's threads
  10.     dwFlags: DWORD;
  11.     szExeFile: array [0..MAX_PATH - 1] of Char;    // Path
  12.   end;  
  13.  

this:
th32DefaultHeapID: ULONG_PTR;

is at offset 0xC and not aligned to 8 bytes, which it has to be on x64. That's why it should be 8 byte aligned (+4).

This is either a bug, or I just don't know what I am doing.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: sizeof(PROCESSENTRY32) on x64 wrong size?
« Reply #1 on: June 22, 2018, 02:25:08 pm »
I compared sizeof(PROCESSENTRY32) in visual studio with sizeof(PROCESSENTRY32) in pascal and noticed that in visual studio the size is 304 while on pascal it is 300.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses JwaTlHelp32;
  5.  
  6. begin
  7.   Writeln('Target=', {$INCLUDE %FPCTARGETCPU%});
  8.   Writeln('FPC=', {$INCLUDE %FPCVERSION%});
  9.   Writeln('SizeOf(PROCESSENTRY32)=', SizeOf(PROCESSENTRY32));
  10.   Readln;
  11. end.
Result is:
Code: [Select]
Target=x86_64
FPC=3.0.4
SizeOf(PROCESSENTRY32)=304

 

TinyPortal © 2005-2018