From 3317a0088d5409c2905d86bcf74ba52f1558124e Mon Sep 17 00:00:00 2001 From: Freeman Date: Sat, 16 Jan 2021 06:26:56 +0300 Subject: [PATCH] // Put all WinAPI definitions to single include file Be independent from Delphi versions 7 and below, merge all imports (Delphi linker workaround). --- Lib/CRT.pas | 4 +- Lib/KoW/CRT.inc | 59 +++++---------- Lib/KoW/KolibriOS.inc | 53 +++++--------- Lib/KoW/SysAPI.inc | 37 +--------- Lib/KoW/System.inc | 5 -- Lib/KoW/WinAPI.inc | 164 ++++++++++++++++++++++++++++++++++++++++++ Lib/KolibriOS.pas | 5 -- Lib/System.pas | 1 + 8 files changed, 202 insertions(+), 126 deletions(-) create mode 100644 Lib/KoW/WinAPI.inc diff --git a/Lib/CRT.pas b/Lib/CRT.pas index 71b58e1..6f27420 100644 --- a/Lib/CRT.pas +++ b/Lib/CRT.pas @@ -73,11 +73,9 @@ procedure Delay(Milliseconds: LongWord); implementation -uses {$IFDEF KolibriOS} +uses KolibriOS; -{$ELSE} - Windows; {$ENDIF} var diff --git a/Lib/KoW/CRT.inc b/Lib/KoW/CRT.inc index a087cdc..6447a7c 100644 --- a/Lib/KoW/CRT.inc +++ b/Lib/KoW/CRT.inc @@ -4,12 +4,6 @@ Copyright (c) 2021 Delphi SDK for KolibriOS team *) -type - TConsoleFontInfo = packed record - Number: LongWord; - Size: TCoord; - end; - var // Console.obj defaults ScrSize: TCoord = (X: 80; Y: 300); @@ -18,27 +12,6 @@ var SaveInputCP, SaveOutputCP: Word; -const - msvcrt = 'msvcrt.dll'; - -function GetCurrentConsoleFont(hConsole: THandle; MaximumWindow: LongBool; var Info: TConsoleFontInfo): LongBool; stdcall; - external kernel32 name 'GetCurrentConsoleFont'; -function GetConsoleWindow: THandle; stdcall; - external kernel32 name 'GetConsoleWindow'; - -function _cgets_s(Buffer: PKolibriChar; Count: Cardinal; var Read: Cardinal): PKolibriChar; cdecl; - external msvcrt name '_cgets_s'; -function _cputs(Str: PKolibriChar): Integer; cdecl; - external msvcrt name '_cputs'; -function _getch: KolibriChar; cdecl; - external msvcrt name '_getch'; -function gets: PKolibriChar; cdecl; - external msvcrt name 'gets'; -function _kbhit: Integer; cdecl; - external msvcrt name '_kbhit'; -function _cprintf(Fmt: PKolibriChar): Integer; cdecl varargs; - external msvcrt name '_cprintf'; - procedure con_set_cursor_pos(X, Y: Integer); stdcall; forward; // call local proc from con_cls procedure con_cls; stdcall; @@ -62,7 +35,7 @@ begin if not CloseWindow then begin CursorOff; - _getch; + getch; end; SetConsoleCP(SaveInputCP); @@ -84,8 +57,8 @@ begin AllocConsole; {$ENDIF} - TTextRec(Input).Handle := System.GetStdHandle(STD_INPUT_HANDLE); - TTextRec(Output).Handle := System.GetStdHandle(STD_OUTPUT_HANDLE); + TTextRec(Input).Handle := GetStdHandle(STD_INPUT_HANDLE); + TTextRec(Output).Handle := GetStdHandle(STD_OUTPUT_HANDLE); {$IFDEF Debug} SaveInputCP := GetConsoleCP; @@ -93,7 +66,7 @@ begin SaveOutputCP := GetConsoleOutputCP; SetConsoleOutputCP(CP_KOLIBRIOS); - SetConsoleTitleA(Title); + SetConsoleTitle(Title); if WndWidth <> LongWord(-1) then WndSize.X := WndWidth; @@ -102,8 +75,8 @@ begin MainWindow := GetConsoleWindow; GetCurrentConsoleFont(TTextRec(Output).Handle, False, Font); - SetWindowPos(MainWindow, 0, WndPos.X, WndPos.Y, - Font.Size.X * WndSize.X + GetSystemMetrics(SM_CXVSCROLL), Font.Size.Y * WndSize.Y + GetSystemMetrics(SM_CYHSCROLL), 0); +{ SetWindowPos(MainWindow, 0, WndPos.X, WndPos.Y, + Font.Size.X * WndSize.X + GetSystemMetrics(SM_CXVSCROLL), Font.Size.Y * WndSize.Y + GetSystemMetrics(SM_CYHSCROLL), 0);} if ScrWidth <> LongWord(-1) then ScrSize.X := ScrWidth; @@ -155,12 +128,12 @@ var Font: TConsoleFontInfo; begin GetCurrentConsoleFont(TTextRec(Output).Handle, False, Font); - Result := Font.Size.Y; + Result := Font.dwFontSize.Y; end; function con_getch: KolibriChar; stdcall; begin - Result := _getch; + Result := getch; end; function con_gets(Str: PKolibriChar; Length: Integer): PKolibriChar; stdcall; @@ -168,8 +141,8 @@ var LF: KolibriChar; Read: Cardinal; begin - _cgets_s(Str, Length, Read); - _cgets_s(@LF, SizeOf(LF), Read); + cgets_s(Str, Length, Read); + cgets_s(@LF, SizeOf(LF), Read); Result := Str; end; @@ -180,7 +153,7 @@ end; function con_kbhit: Boolean; stdcall; begin - Result := _kbhit <> 0; + Result := kbhit <> 0; end; function con_set_cursor_height(Height: Integer): Integer; stdcall; @@ -212,14 +185,14 @@ end; procedure con_write_string(Str: PKolibriChar; Length: LongWord); stdcall; var - Written: Cardinal; + Written: LongWord; begin - System.WriteFile(TTextRec(Output).Handle, Str^, Length, Written, nil); + WriteFile(TTextRec(Output).Handle, Str^, Length, Written, nil); end; procedure con_write_asciiz(Str: PKolibriChar); stdcall; begin - _cputs(Str); + cputs(Str); end; procedure Delay(Milliseconds: LongWord); @@ -240,11 +213,11 @@ begin Pointer(@System.con_gets) := @con_gets; Pointer(@System.con_gets2) := @con_gets2; Pointer(@System.con_kbhit) := @con_kbhit; - Pointer(@System.con_printf) := @_cprintf; + Pointer(@System.con_printf) := @cprintf; Pointer(@System.con_set_cursor_height) := @con_set_cursor_height; Pointer(@System.con_set_cursor_pos) := @con_set_cursor_pos; Pointer(@System.con_set_flags) := @con_set_flags; - Pointer(@System.con_set_title) := @Windows.SetConsoleTitleA; + Pointer(@System.con_set_title) := @SetConsoleTitle; Pointer(@System.con_write_asciiz) := @con_write_asciiz; Pointer(@System.con_write_string) := @con_write_string; end; diff --git a/Lib/KoW/KolibriOS.inc b/Lib/KoW/KolibriOS.inc index 17e07b1..b658169 100644 --- a/Lib/KoW/KolibriOS.inc +++ b/Lib/KoW/KolibriOS.inc @@ -4,16 +4,6 @@ Copyright (c) 2021 Delphi SDK for KolibriOS team *) -type - TOSVersionInfoA = packed record - dwOSVersionInfoSize: LongWord; - dwMajorVersion: LongWord; - dwMinorVersion: LongWord; - dwBuildNumber: LongWord; - dwPlatformId: LongWord; - szCSDVersion: array[0..127] of KolibriChar; - end; - const BoardLog = 'BOARDLOG.TXT'; @@ -21,18 +11,11 @@ var hBoard: THandle; DebugReadPos: UInt64; -function GetFileSizeEx(hFile: THandle; var FileSize: UInt64): LongBool; stdcall; - external kernel32 name 'GetFileSizeEx'; -function GetVersionExA(var Info: TOSVersionInfoA): LongBool; stdcall; - external kernel32 name 'GetVersionExA'; -function SetFilePointerEx(hFile: THandle; Distance: Int64; NewFilePtr: PUInt64; Method: LongWord): LongBool; stdcall; - external kernel32 name 'SetFilePointerEx'; - function InitBoard: Boolean; begin if hBoard = 0 then begin - hBoard := CreateFileA(BoardLog, GENERIC_READ or GENERIC_WRITE, + hBoard := CreateFile(BoardLog, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_ALWAYS, 0, 0); Result := hBoard <> INVALID_HANDLE_VALUE; end @@ -54,28 +37,28 @@ var BytesWritten: LongWord; begin if not InitBoard or not SetFilePointerEx(hBoard, 0, nil, FILE_END) or - not System.WriteFile(hBoard, Data, SizeOf(Data), BytesWritten, nil) or (BytesWritten = 0) + not WriteFile(hBoard, Data, SizeOf(Data), BytesWritten, nil) or (BytesWritten = 0) then RunError(ERROR_ACCESS_DENIED); end; procedure ExitThread; stdcall; begin - Windows.ExitProcess(0); + ExitProcess(0); end; function GetCurrentDirectory(Buffer: PKolibriChar; Count: LongWord): LongWord; begin - Result := GetCurrentDirectoryA(Count, Buffer); + Result := System.GetCurrentDirectory(Count, Buffer); end; procedure GetKernelVersion(var Buffer: TKernelVersion); var - Info: TOSVersionInfoA; + Info: TOSVersionInfo; begin FillChar(Buffer, SizeOf(Buffer), 0); Info.dwOSVersionInfoSize := SizeOf(Info); - if GetVersionExA(Info) then + if GetVersionEx(Info) then with Buffer, Info do begin A := dwMajorVersion; @@ -84,9 +67,9 @@ begin end; end; -function GetSystemDate: KolibriOS.TSystemDate; +function GetSystemDate: TSystemDate; var - Date: Windows.TSystemTime; + Date: System.TSystemTime; begin GetLocalTime(Date); with Result, Date do @@ -98,9 +81,9 @@ begin end; end; -function GetSystemTime: KolibriOS.TSystemTime; +function GetSystemTime: TSystemTime; var - Time: Windows.TSystemTime; + Time: System.TSystemTime; begin GetLocalTime(Time); with Result, Time do @@ -113,12 +96,12 @@ end; function GetTickCount: LongWord; stdcall; asm - JMP Windows.GetTickCount + JMP System.GetTickCount end; function GetTickCount64: UInt64; stdcall; asm - CALL Windows.GetTickCount + CALL System.GetTickCount XOR EDX, EDX end; @@ -128,12 +111,12 @@ var QSize: UInt64; Unpacked: Pointer; begin - hFile := CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); + hFile := CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); if (hFile <> INVALID_HANDLE_VALUE) and GetFileSizeEx(hFile, QSize) then begin Size := QSize; GetMem(Result, Size); - Windows.ReadFile(hFile, Result^, Size, Size, nil); + System.ReadFile(hFile, Result^, Size, Size, nil); if PPackedFileHeader(Result).Signature = KPCK then begin Size := PPackedFileHeader(Result).UnpackedSize; @@ -149,11 +132,11 @@ begin end; procedure SetCurrentDirectory(Path: PKolibriChar); -begin - SetCurrentDirectoryA(Path); +asm + JMP System.SetCurrentDirectory end; -procedure Sleep(Time: LongWord); +procedure Sleep(Time: LongWord); begin - Windows.Sleep(Time * 10); + System.Sleep(Time * 10); end; diff --git a/Lib/KoW/SysAPI.inc b/Lib/KoW/SysAPI.inc index 322df39..43ccc97 100644 --- a/Lib/KoW/SysAPI.inc +++ b/Lib/KoW/SysAPI.inc @@ -4,43 +4,10 @@ Copyright (c) 2021 Delphi SDK for KolibriOS team *) -type - HRESULT = type LongInt; - - HINST = THandle; - HMODULE = THandle; - HRSRC = THandle; - var ExitCode: Integer; - MainWindow: THandle; + MainWindow: HWnd; -const - kernel32 = 'kernel32.dll'; - user32 = 'user32.dll'; - -procedure _Halt(ExitCode: Integer); - -procedure ExitProcess(ExitCode: Cardinal); stdcall; - external kernel32 name 'ExitProcess'; -function GetCommandLine: PKolibriChar; stdcall; - external kernel32 name 'GetCommandLineA'; -function GetModuleFileName(hModule: THandle; Buffer: PKolibriChar; Count: Cardinal): Cardinal; stdcall; - external kernel32 name 'GetModuleFileNameA'; -function GetProcessHeap: THandle; stdcall; - external kernel32 name 'GetProcessHeap'; -function GetStdHandle(Code: LongWord): THandle; stdcall; - external kernel32 name 'GetStdHandle'; -function HeapAlloc(hHeap: THandle; Flags, Bytes: Cardinal): Pointer; stdcall; - external kernel32 name 'HeapAlloc'; -function HeapReAlloc(hHeap: THandle; Flags: Cardinal; Mem: Pointer; Bytes: Cardinal): Pointer; stdcall; - external kernel32 name 'HeapReAlloc'; -function HeapFree(hHeap: THandle; Flags: Cardinal; Mem: Pointer): LongBool; stdcall; - external kernel32 name 'HeapFree'; -function WriteFile(hFile: THandle; const Buffer; Count: Cardinal; var BytesWritten: Cardinal; Overlapped: Pointer): LongBool; stdcall; - external kernel32 name 'WriteFile'; - -procedure MessageBox(Wnd: THandle; Text, Caption: PKolibriChar; Flags: Cardinal); stdcall; - external user32 name 'MessageBoxA'; +procedure _Halt(ExitCode: Integer); procedure __lldiv; diff --git a/Lib/KoW/System.inc b/Lib/KoW/System.inc index 1b1d8f3..a161090 100644 --- a/Lib/KoW/System.inc +++ b/Lib/KoW/System.inc @@ -4,9 +4,6 @@ Copyright (c) 2021 Delphi SDK for KolibriOS team *) -const - HEAP_NO_SERIALIZE = $00001; - var ModuleFileName: array[0..1023] of KolibriChar; HeapHandle: THandle; @@ -39,8 +36,6 @@ end; procedure ErrorMessage(Msg: PKolibriChar; Count: Byte); const - MB_ICONERROR = $0010; - MB_TASKMODAL = $2000; EOL: array[0..1] of KolibriChar = #13#10; var Buf: array[Low(Byte)..High(Byte) + 1] of KolibriChar; diff --git a/Lib/KoW/WinAPI.inc b/Lib/KoW/WinAPI.inc new file mode 100644 index 0000000..7ea2e47 --- /dev/null +++ b/Lib/KoW/WinAPI.inc @@ -0,0 +1,164 @@ +type + HModule = type THandle; + HWnd = type THandle; + + PCoord = ^TCoord; + TCoord = packed record + X, Y: SmallInt; + end; + + PPoint = ^TPoint; + TPoint = packed record + X, Y: LongInt; + end; + + PSmallRect = ^TSmallRect; + TSmallRect = packed record + Left, Top, Right, Bottom: SmallInt; + end; + + PConsoleCursorInfo = ^TConsoleCursorInfo; + TConsoleCursorInfo = packed record + dwSize: LongWord; + bVisible: LongBool; + end; + + PConsoleFontInfo = ^TConsoleFontInfo; + TConsoleFontInfo = packed record + nFont: LongWord; + dwFontSize: TCoord; + end; + + PConsoleScreenBufferInfo = ^TConsoleScreenBufferInfo; + TConsoleScreenBufferInfo = packed record + dwSize: TCoord; + dwCursorPosition: TCoord; + wAttributes: Word; + srWindow: TSmallRect; + dwMaximumWindowSize: TCoord; + end; + + PSystemTime = ^TSystemTime; + TSystemTime = record + wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute, wSecond, wMilliseconds: Word; + end; + + TOSVersionInfo = packed record + dwOSVersionInfoSize, dwMajorVersion, dwMinorVersion, dwBuildNumber, dwPlatformId: LongWord; + szCSDVersion: array[0..127] of AnsiChar; + end; + +const + kernel32 = 'kernel32.dll'; + user32 = 'user32.dll'; + msvcrt = 'msvcrt.dll'; + + INVALID_HANDLE_VALUE = THandle(-1); + + STD_INPUT_HANDLE = LongWord(-10); + STD_OUTPUT_HANDLE = LongWord(-11); + + GENERIC_READ = LongWord($80000000); + GENERIC_WRITE = LongWord($40000000); + + FILE_SHARE_READ = LongWord($1); + FILE_SHARE_WRITE = LongWord($2); + + OPEN_EXISTING = 3; + OPEN_ALWAYS = 4; + + FILE_BEGIN = 0; + FILE_CURRENT = 1; + FILE_END = 2; + + HEAP_NO_SERIALIZE = LongWord($00001); + + MB_ICONERROR = LongWord($0010); + MB_TASKMODAL = LongWord($2000); + +function AllocConsole: LongBool; stdcall; + external kernel32 name 'AllocConsole'; +function CreateFile(FileName: PAnsiChar; Access, ShareMode: LongWord; SecurityAttributes: Pointer; Disposition, FlagsAndAttributes: LongWord; hTemplateFile: THandle): THandle; stdcall; + external kernel32 name 'CreateFileA'; +procedure ExitProcess(ExitCode: Cardinal); stdcall; + external kernel32 name 'ExitProcess'; +function FillConsoleOutputCharacter(hConsoleOutput: THandle; Ch: AnsiChar; Count: LongWord; Coord: TCoord; var CharsWritten: LongWord): LongBool; stdcall; + external kernel32 name 'FillConsoleOutputCharacterA'; +function GetCommandLine: PAnsiChar; stdcall; + external kernel32 name 'GetCommandLineA'; +function GetConsoleCP: Cardinal; stdcall; + external kernel32 name 'GetConsoleCP'; +function GetConsoleCursorInfo(hConsoleOutput: THandle; var Info: TConsoleCursorInfo): LongBool; stdcall; + external kernel32 name 'GetConsoleCursorInfo'; +function GetConsoleOutputCP: Cardinal; stdcall; + external kernel32 name 'GetConsoleOutputCP'; +function GetConsoleScreenBufferInfo(hConsoleOutput: THandle; var Info: TConsoleScreenBufferInfo): LongBool; stdcall; + external kernel32 name 'GetConsoleScreenBufferInfo'; +function GetConsoleWindow: HWnd; stdcall; + external kernel32 name 'GetConsoleWindow'; +function GetCurrentConsoleFont(hConsoleOutput: THandle; MaximumWindow: LongBool; var Info: TConsoleFontInfo): LongBool; stdcall; + external kernel32 name 'GetCurrentConsoleFont'; +function GetCurrentDirectory(Count: LongWord; Buffer: PAnsiChar): LongWord; stdcall; + external kernel32 name 'GetCurrentDirectoryA'; +function GetFileSizeEx(hFile: THandle; var FileSize: UInt64): LongBool; stdcall; + external kernel32 name 'GetFileSizeEx'; +procedure GetLocalTime(var Time: TSystemTime); stdcall; + external kernel32 name 'GetLocalTime'; +function GetModuleFileName(hModule: HModule; Buffer: PAnsiChar; Count: Cardinal): Cardinal; stdcall; + external kernel32 name 'GetModuleFileNameA'; +function GetProcessHeap: THandle; stdcall; + external kernel32 name 'GetProcessHeap'; +function GetStdHandle(Code: LongWord): THandle; stdcall; + external kernel32 name 'GetStdHandle'; +function GetTickCount: LongWord; stdcall; + external kernel32 name 'GetStdHandle'; +function GetVersionEx(var Info: TOSVersionInfo): LongBool; stdcall; + external kernel32 name 'GetVersionExA'; +function HeapAlloc(hHeap: THandle; Flags, Bytes: Cardinal): Pointer; stdcall; + external kernel32 name 'HeapAlloc'; +function HeapReAlloc(hHeap: THandle; Flags: Cardinal; Mem: Pointer; Bytes: Cardinal): Pointer; stdcall; + external kernel32 name 'HeapReAlloc'; +function HeapFree(hHeap: THandle; Flags: Cardinal; Mem: Pointer): LongBool; stdcall; + external kernel32 name 'HeapFree'; +function ReadFile(hFile: THandle; var Buffer; Count: LongWord; var BytesRead: LongWord; lpOverlapped: Pointer): LongBool; stdcall; + external kernel32 name 'ReadFile'; +function SetConsoleCP(CodePage: Cardinal): LongBool; stdcall; + external kernel32 name 'SetConsoleCP'; +function SetConsoleCursorInfo(hConsoleOutput: THandle; const Info: TConsoleCursorInfo): LongBool; stdcall; + external kernel32 name 'SetConsoleCursorInfo'; +function SetConsoleCursorPosition(hConsoleOutput: THandle; Pos: TCoord): LongBool; stdcall; + external kernel32 name 'SetConsoleCursorPosition'; +function SetConsoleOutputCP(CodePage: Cardinal): LongBool; stdcall; + external kernel32 name 'SetConsoleOutputCP'; +function SetConsoleScreenBufferSize(hConsoleOutput: THandle; Size: TCoord): LongBool; stdcall; + external kernel32 name 'SetConsoleScreenBufferSize'; +function SetConsoleTextAttribute(hConsoleOutput: THandle; Attr: Word): LongBool; stdcall; + external kernel32 name 'SetConsoleTextAttribute'; +function SetConsoleTitle(Title: PAnsiChar): LongBool; stdcall; + external kernel32 name 'SetConsoleTitleA'; +function SetConsoleWindowInfo(hConsoleOutput: THandle; Abs: LongBool; const Window: TSmallRect): LongBool; stdcall; + external kernel32 name 'SetConsoleWindowInfo'; +function SetCurrentDirectory(Path: PAnsiChar): LongBool; stdcall; + external kernel32 name 'SetCurrentDirectoryA'; +function SetFilePointerEx(hFile: THandle; Distance: Int64; NewFilePtr: PUInt64; Method: LongWord): LongBool; stdcall; + external kernel32 name 'SetFilePointerEx'; +procedure Sleep(Milliseconds: LongWord); stdcall; + external kernel32 name 'Sleep'; +function WriteFile(hFile: THandle; const Buffer; Count: LongWord; var BytesWritten: LongWord; Overlapped: Pointer): LongBool; stdcall; + external kernel32 name 'WriteFile'; + +procedure MessageBox(Wnd: HWnd; Text, Caption: PAnsiChar; Flags: Cardinal); stdcall; + external user32 name 'MessageBoxA'; + +function cgets_s(Buffer: PAnsiChar; Count: Cardinal; var Read: Cardinal): PAnsiChar; cdecl; + external msvcrt name '_cgets_s'; +function cputs(Str: PAnsiChar): Integer; cdecl; + external msvcrt name '_cputs'; +function getch: AnsiChar; cdecl; + external msvcrt name '_getch'; +function gets: PAnsiChar; cdecl; + external msvcrt name 'gets'; +function kbhit: Integer; cdecl; + external msvcrt name '_kbhit'; +function cprintf(Fmt: PAnsiChar): Integer; cdecl varargs; + external msvcrt name '_cprintf'; diff --git a/Lib/KolibriOS.pas b/Lib/KolibriOS.pas index 8c64711..11d68bb 100644 --- a/Lib/KolibriOS.pas +++ b/Lib/KolibriOS.pas @@ -679,11 +679,6 @@ const implementation -{$IFNDEF KolibriOS} -uses - Windows; -{$ENDIF} - {$IFDEF KolibriOS} procedure ExitThread; stdcall; diff --git a/Lib/System.pas b/Lib/System.pas index 0b38fe1..4245d87 100644 --- a/Lib/System.pas +++ b/Lib/System.pas @@ -327,6 +327,7 @@ const con_write_string: procedure(Str: PKolibriChar; Length: LongWord); stdcall = nil; {$IFNDEF KolibriOS} + {$I KoW\WinAPI.inc} {$I KoW\SysAPI.inc} {$ENDIF}