WinAPI Import by Ordinal V13

Syntax: DECLARE FUNCTION name LIB "dll" ALIAS "#ordinal" (params) AS type
Linker: MGC_IMPORT_THUNK_BY_ORDINAL (import_table.c)

Some Windows DLLs export functions by ordinal number rather than by name. V13 adds support for importing these via the # prefix in the ALIAS clause.

Parameters

ParameterDescription
namePB name used to call the function in your code
"dll"DLL filename (e.g., "kernel32.dll")
"#ordinal"Ordinal number prefixed with # (e.g., "#42")
paramsParameter list (BYVAL/BYREF name AS type)
typeReturn type (LONG, QUAD, etc.)

Example

#COMPILE EXE
#DIM ALL

' Import by ordinal from kernel32.dll
DECLARE FUNCTION GetProcessHeapOrd LIB "kernel32.dll" _
    ALIAS "#123" () AS QUAD

FUNCTION PBMAIN() AS LONG
    LOCAL hHeap AS QUAD
    hHeap = GetProcessHeapOrd()
    PRINT "Heap handle: "; hHeap
    FUNCTION = 0
END FUNCTION

Generated Import Table

; In .idata section:
; The PB declare name becomes the IAT symbol
; Linker registers it as MGC_IMPORT_THUNK_BY_ORDINAL
__imp_GetProcessHeapOrd:
    dq    IAT_GetProcessHeapOrd        ; Import Address Table entry

; At link time, the PE import directory records:
;   DLL: kernel32.dll
;   Import by ordinal: 123
;   Thunk: __imp_GetProcessHeapOrd

How It Works

  1. Parser detects ALIAS "#123" and marks the declaration as ordinal import
  2. Codegen emits call [__imp_<PBname>] as usual
  3. Linker creates a PE import directory entry with the IMAGE_ORDINAL_FLAG set
  4. Windows loader resolves the ordinal to the actual function address at load time