Recent

Author Topic: Questionable definition of INPUT_RECORD in struct.inc  (Read 488 times)

440bx

  • Hero Member
  • *****
  • Posts: 943
Questionable definition of INPUT_RECORD in struct.inc
« on: April 10, 2019, 05:09:10 am »
Hello,

in struct.inc, INPUT_RECORD is defined as follows:
Code: Pascal  [Select]
  1.      INPUT_RECORD = record
  2.           EventType: Word;
  3.           Reserved: Word;
  4.           Event : record case longint of
  5.                  0 : ( KeyEvent : KEY_EVENT_RECORD );
  6.                  1 : ( MouseEvent : MOUSE_EVENT_RECORD );
  7.                  2 : ( WindowBufferSizeEvent : WINDOW_BUFFER_SIZE_RECORD );
  8.                  3 : ( MenuEvent : MENU_EVENT_RECORD );
  9.                  4 : ( FocusEvent : FOCUS_EVENT_RECORD );
  10.        end;
  11.       end;                                            
Note the presence of "Reserved : Word" after EventType.

The MSDN defines INPUT_RECORD as follows:
Code: Pascal  [Select]
  1. typedef struct _INPUT_RECORD {
  2.   WORD  EventType;
  3.   union {
  4.     KEY_EVENT_RECORD          KeyEvent;
  5.     MOUSE_EVENT_RECORD        MouseEvent;
  6.     WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
  7.     MENU_EVENT_RECORD         MenuEvent;
  8.     FOCUS_EVENT_RECORD        FocusEvent;
  9.   } Event;
  10. } INPUT_RECORD;
  11.  
Note the absence of a "Reserved" field after EventType.

The FPC definition ends up working because of alignment but, there is no "reserved" field in INPUT_RECORD.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 1300
Re: Questionable definition of INPUT_RECORD in struct.inc
« Reply #1 on: April 10, 2019, 06:52:45 am »
The FPC definition ends up working because of alignment but, there is no "reserved" field in INPUT_RECORD.
Compatible with Delphi?
And it is interesting that in recent versions in Delphi is clearly removed alignment to justify this field:
Code: Pascal  [Select]
  1. {$ALIGN 1}
  2.   PInputRecord = ^TInputRecord;
  3.   {$EXTERNALSYM _INPUT_RECORD}
  4.   _INPUT_RECORD = record
  5.     EventType: Word;
  6.     Reserved: Word;
  7. ...
  8.   INPUT_RECORD = _INPUT_RECORD;
  9. {$ALIGN ON}

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Questionable definition of INPUT_RECORD in struct.inc
« Reply #2 on: April 10, 2019, 06:59:10 am »
Unit JwaWinCon:
Code: Pascal  [Select]
  1. //fpc\3.0.4\source\packages\winunits-jedi\src\jwawincon.pas:
  2.   _INPUT_RECORD = record
  3.     EventType: WORD;
  4.     case Integer of
  5.       0: (KeyEvent: KEY_EVENT_RECORD);
  6.       1: (MouseEvent: MOUSE_EVENT_RECORD);
  7.       2: (WindowBufferSizeEvent: WINDOW_BUFFER_SIZE_RECORD);
  8.       3: (MenuEvent: MENU_EVENT_RECORD);
  9.       4: (FocusEvent: FOCUS_EVENT_RECORD);
  10.   end;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7216
Re: Questionable definition of INPUT_RECORD in struct.inc
« Reply #3 on: April 10, 2019, 10:18:37 am »
Afaik the windows headers started out as packed records. IOW only manual alignment

440bx

  • Hero Member
  • *****
  • Posts: 943
Re: Questionable definition of INPUT_RECORD in struct.inc
« Reply #4 on: April 10, 2019, 01:21:31 pm »
Afaik the windows headers started out as packed records. IOW only manual alignment

It's a bit disconcerting to see it defined one way in MSDN and a different way in FPC.    I figured I'd point it out because you've made it clear that _in general_ you follow MSDN whenever possible.  In this case it's definitely possible to follow MSDN and unless a programmer is accessing that alignment field, there shouldn't be any compatibility issues in removing it.

Someone might think that field is actually used for something (like the reserved field passed to a dll entry point) when it really isn't, just alignment/filler.

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.