News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bug in WINDOWS.INC - INPUT_RECORD

Started by BATSoftware, September 10, 2007, 01:59:50 AM

Previous topic - Next topic

BATSoftware

Hello,
After 5 hrs of wondering why ReadConsoleInput wasnt returning the correct information according to WIN32.HLP and M$ online documentation, I discovered an error in the structure definition for INPUT_RECORD. The bug is not immediately noticable unless you release that structures in WINDOWS.H are for C programs. M$ flavors of C compliers will default to structure alignment on DWORD boundries. Hence the union in INPUT_RECORD needs to be DWORD aligned.

WIN32.HLP definition of INPUT_RECORD:

typedef struct _INPUT_RECORD { // ir 
    WORD EventType; 
    union {
        KEY_EVENT_RECORD KeyEvent;
        MOUSE_EVENT_RECORD MouseEvent;
        WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
        MENU_EVENT_RECORD MenuEvent;
        FOCUS_EVENT_RECORD FocusEvent;
    } Event;
} INPUT_RECORD;

Current WINDOWS.INC definition:
INPUT_RECORD STRUCT
  EventType WORD ?
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
INPUT_RECORD ENDS

Correct WINDOWS.INC definition should be:
INPUT_RECORD STRUCT
  EventType WORD ?
ALIGN 4 
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
INPUT_RECORD ENDS

After making the alignment adjustment, the returned data from ReadConsoleInput aligned properly.

William A Troy, Jr J.D.
BATSoftware







hutch--

William,

Thanks for the error report, do you have a short piece of code that I can test ReadConsoleInput() with ? Here is a quick test piec to verify your report which appears to be correct.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    INPUT_RECORDx STRUCT
      EventType WORD ?
      dummy WORD ?              ; aligned here
      UNION
        KeyEvent                KEY_EVENT_RECORD            <>
        MouseEvent              MOUSE_EVENT_RECORD          <>
        WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
        MenuEvent               MENU_EVENT_RECORD           <>
        FocusEvent              FOCUS_EVENT_RECORD          <>
      ENDS
    INPUT_RECORDx ENDS

    ; ReadConsoleInput
;     HANDLE hConsoleInput, // handle of a console input buffer
;     PINPUT_RECORD lpBuffer, // address of the buffer for read data
;     DWORD nLength, // number of records to read
;     LPDWORD lpNumberOfEventsRead // address of number of records read
;    );

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL hCI   :DWORD
    LOCAL nEV   :DWORD
    LOCAL pIR   :INPUT_RECORDx

    mov hCI, rv(GetStdHandle,STD_INPUT_HANDLE)

    invoke ReadConsoleInput,hCI,addr pIR,1,ADDR nEV

    movzx eax, WORD PTR (pIR.KeyEvent.wVirtualKeyCode)
    print str$(eax)," VirtualKeyCode",13,10

    movzx eax, WORD PTR (pIR.KeyEvent.wVirtualScanCode)
    print str$(eax)," VirtualScanCode",13,10

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

hutch--

In view of not getting an answer and being satisfied with the testing that the report was correct, the next release version of WINDOWS.INC will have this modified form of the structure.


INPUT_RECORD STRUCT
  EventType             WORD ?
  two_byte_alignment    WORD ?
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
INPUT_RECORD ENDS


I have added the alignment padding manually so that the file remains compatible with Pelle's Macro Assembler.

Again thanks for the report.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php