sysos.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. {
  2. Basic stuff for windows rtls
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2001 by Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. const
  12. { constants for GetStdHandle }
  13. STD_INPUT_HANDLE = dword(-10);
  14. STD_OUTPUT_HANDLE = dword(-11);
  15. STD_ERROR_HANDLE = dword(-12);
  16. INVALID_HANDLE_VALUE = THandle(-1);
  17. IGNORE = 0; { Ignore signal }
  18. INFINITE = longint($FFFFFFFF); { Infinite timeout }
  19. { flags for CreateFile }
  20. GENERIC_READ=$80000000;
  21. GENERIC_WRITE=$40000000;
  22. CREATE_NEW = 1;
  23. CREATE_ALWAYS = 2;
  24. OPEN_EXISTING = 3;
  25. OPEN_ALWAYS = 4;
  26. TRUNCATE_EXISTING = 5;
  27. FILE_ATTRIBUTE_ARCHIVE = 32;
  28. FILE_ATTRIBUTE_COMPRESSED = 2048;
  29. FILE_ATTRIBUTE_NORMAL = 128;
  30. FILE_ATTRIBUTE_DIRECTORY = 16;
  31. FILE_ATTRIBUTE_HIDDEN = 2;
  32. FILE_ATTRIBUTE_READONLY = 1;
  33. FILE_ATTRIBUTE_SYSTEM = 4;
  34. FILE_ATTRIBUTE_TEMPORARY = 256;
  35. { Share mode open }
  36. fmShareCompat = $00000000;
  37. fmShareExclusive = $10;
  38. fmShareDenyWrite = $20;
  39. fmShareDenyRead = $30;
  40. fmShareDenyNone = $40;
  41. { flags for SetFilePos }
  42. FILE_BEGIN = 0;
  43. FILE_CURRENT = 1;
  44. FILE_END = 2;
  45. { GetFileType }
  46. FILE_TYPE_UNKNOWN = 0;
  47. FILE_TYPE_DISK = 1;
  48. FILE_TYPE_CHAR = 2;
  49. FILE_TYPE_PIPE = 3;
  50. VER_PLATFORM_WIN32s = 0;
  51. VER_PLATFORM_WIN32_WINDOWS = 1;
  52. VER_PLATFORM_WIN32_NT = 2;
  53. { These constants are used for conversion of error codes }
  54. { from win32 i/o errors to tp i/o errors }
  55. { errors 1 to 18 are the same as in Turbo Pascal }
  56. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  57. { The media is write protected. }
  58. ERROR_WRITE_PROTECT = 19;
  59. { The system cannot find the device specified. }
  60. ERROR_BAD_UNIT = 20;
  61. { The device is not ready. }
  62. ERROR_NOT_READY = 21;
  63. { The device does not recognize the command. }
  64. ERROR_BAD_COMMAND = 22;
  65. { Data error (cyclic redundancy check) }
  66. ERROR_CRC = 23;
  67. { The program issued a command but the }
  68. { command length is incorrect. }
  69. ERROR_BAD_LENGTH = 24;
  70. { The drive cannot locate a specific }
  71. { area or track on the disk. }
  72. ERROR_SEEK = 25;
  73. { The specified disk or diskette cannot be accessed. }
  74. ERROR_NOT_DOS_DISK = 26;
  75. { The drive cannot find the sector requested. }
  76. ERROR_SECTOR_NOT_FOUND = 27;
  77. { The printer is out of paper. }
  78. ERROR_OUT_OF_PAPER = 28;
  79. { The system cannot write to the specified device. }
  80. ERROR_WRITE_FAULT = 29;
  81. { The system cannot read from the specified device. }
  82. ERROR_READ_FAULT = 30;
  83. { A device attached to the system is not functioning.}
  84. ERROR_GEN_FAILURE = 31;
  85. { The process cannot access the file because }
  86. { it is being used by another process. }
  87. ERROR_SHARING_VIOLATION = 32;
  88. { A pipe has been closed on the other end }
  89. { Removing that error allows eof to works as on other OSes }
  90. ERROR_BROKEN_PIPE = 109;
  91. ERROR_DIR_NOT_EMPTY = 145;
  92. ERROR_ALREADY_EXISTS = 183;
  93. type
  94. {UINT = longint;
  95. BOOL = longint; obsolete }
  96. UINT = cardinal;
  97. BOOL = longbool;
  98. // WCHAR = word;
  99. {$ifdef UNICODE}
  100. LPTCH = ^word;
  101. LPTSTR = ^word;
  102. LPCTSTR = ^word;
  103. {$else UNICODE}
  104. LPTCH = ^char;
  105. LPTSTR = ^char;
  106. LPCTSTR = ^char;
  107. {$endif UNICODE}
  108. LPWSTR = ^wchar;
  109. PVOID = pointer;
  110. LPVOID = pointer;
  111. LPCVOID = pointer;
  112. LPDWORD = ^DWORD;
  113. HLocal = THandle;
  114. PStr = pchar;
  115. LPStr = pchar;
  116. PLPSTR = ^LPSTR;
  117. PLPWSTR = ^LPWSTR;
  118. PSecurityAttributes = ^TSecurityAttributes;
  119. TSecurityAttributes = record
  120. nLength : DWORD;
  121. lpSecurityDescriptor : Pointer;
  122. bInheritHandle : BOOL;
  123. end;
  124. PProcessInformation = ^TProcessInformation;
  125. TProcessInformation = record
  126. hProcess: THandle;
  127. hThread: THandle;
  128. dwProcessId: DWORD;
  129. dwThreadId: DWORD;
  130. end;
  131. PFileTime = ^TFileTime;
  132. TFileTime = record
  133. dwLowDateTime,
  134. dwHighDateTime : DWORD;
  135. end;
  136. LPSystemTime= ^PSystemTime;
  137. PSystemTime = ^TSystemTime;
  138. TSystemTime = record
  139. wYear,
  140. wMonth,
  141. wDayOfWeek,
  142. wDay,
  143. wHour,
  144. wMinute,
  145. wSecond,
  146. wMilliseconds: Word;
  147. end;
  148. threadvar
  149. errno : longint;
  150. { misc. functions }
  151. function GetLastError : DWORD;
  152. stdcall;external KernelDLL name 'GetLastError';
  153. { time and date functions }
  154. function GetTickCount : longint;
  155. stdcall;external KernelDLL name 'GetTickCount';
  156. {$ifndef WINCE}
  157. { process functions }
  158. procedure ExitProcess(uExitCode : UINT);
  159. stdcall;external KernelDLL name 'ExitProcess';
  160. { Startup }
  161. procedure GetStartupInfo(p : pointer);
  162. stdcall;external KernelDLL name 'GetStartupInfoA';
  163. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  164. stdcall;external KernelDLL name 'GetStdHandle';
  165. { command line/enviroment functions }
  166. function GetCommandLine : pchar;
  167. stdcall;external KernelDLL name 'GetCommandLineA';
  168. function GetCurrentProcessId:DWORD;
  169. stdcall; external KernelDLL name 'GetCurrentProcessId';
  170. function Win32GetCurrentThreadId:DWORD;
  171. stdcall; external KernelDLL name 'GetCurrentThreadId';
  172. {$endif WINCE}
  173. { module functions }
  174. function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
  175. stdcall;external KernelDLL name 'GetModuleFileName' + ApiSuffix;
  176. function GetModuleHandle(p : pointer) : longint;
  177. stdcall;external KernelDLL name 'GetModuleHandle' + ApiSuffix;
  178. function GetCommandFile:pchar;forward;
  179. { file functions }
  180. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  181. overlap:pointer):longint;
  182. stdcall;external KernelDLL name 'WriteFile';
  183. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  184. overlap:pointer):longint;
  185. stdcall;external KernelDLL name 'ReadFile';
  186. function CloseHandle(h : thandle) : longint;
  187. stdcall;external KernelDLL name 'CloseHandle';
  188. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  189. stdcall;external KernelDLL name 'SetFilePointer';
  190. function GetFileSize(h:thandle;p:pointer) : longint;
  191. stdcall;external KernelDLL name 'GetFileSize';
  192. function SetEndOfFile(h : thandle) : longbool;
  193. stdcall;external KernelDLL name 'SetEndOfFile';
  194. function FreeLibrary(hLibModule:THandle):ByteBool; stdcall; external KernelDLL name 'FreeLibrary';
  195. {$ifndef WINCE}
  196. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  197. function GetFileType(Handle:thandle):DWord;
  198. stdcall;external KernelDLL name 'GetFileType';
  199. function GetFileAttributes(p : pchar) : dword;
  200. stdcall;external KernelDLL name 'GetFileAttributesA';
  201. function DeleteFile(p : pchar) : longint;
  202. stdcall;external KernelDLL name 'DeleteFileA';
  203. function MoveFile(old,_new : pchar) : longint;
  204. stdcall;external KernelDLL name 'MoveFileA';
  205. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  206. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  207. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  208. stdcall;external KernelDLL name 'CreateFileA';
  209. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  210. { Directory }
  211. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  212. stdcall;external KernelDLL name 'CreateDirectoryA';
  213. function RemoveDirectory(name:pointer):longbool;
  214. stdcall;external KernelDLL name 'RemoveDirectoryA';
  215. function SetCurrentDirectory(name : pointer) : longbool;
  216. stdcall;external KernelDLL name 'SetCurrentDirectoryA';
  217. function GetCurrentDirectory(bufsize : longint;name : pchar) : longbool;
  218. stdcall;external KernelDLL name 'GetCurrentDirectoryA';
  219. {$else WINCE}
  220. function LoadLibraryW(lpLibFileName:PWideChar):THandle; stdcall; external KernelDLL name 'LoadLibraryW';
  221. function LoadLibrary(lpLibFileName:PChar):THandle;
  222. var buf: array[0..MaxPathLen] of WideChar;
  223. begin
  224. AnsiToWideBuf(lpLibFileName, -1, buf, SizeOf(buf));
  225. LoadLibrary:=LoadLibraryW(PWideChar(lpLibFileName));
  226. end;
  227. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddressA';
  228. {$endif WINCE}
  229. var
  230. SetFilePointerEx : function(hFile : THandle;
  231. liDistanceToMove : int64;lpNewFilePointer : pint64;
  232. dwMoveMethod : DWord) : ByteBool;stdcall;
  233. procedure SetupProcVars;
  234. var
  235. hinstLib : THandle;
  236. begin
  237. SetFilePointerEx:=nil;
  238. hinstLib:=LoadLibrary(KernelDLL);
  239. if hinstLib<>0 then
  240. begin
  241. pointer(SetFilePointerEx):=GetProcAddress(hinstLib,'SetFilePointerEx');
  242. FreeLibrary(hinstLib);
  243. end;
  244. end;
  245. Procedure Errno2InOutRes;
  246. Begin
  247. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  248. case Errno of
  249. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  250. begin
  251. { This is the offset to the Win32 to add to directly map }
  252. { to the DOS/TP compatible error codes when in this range }
  253. InOutRes := word(errno)+131;
  254. end;
  255. ERROR_DIR_NOT_EMPTY,
  256. ERROR_ALREADY_EXISTS,
  257. ERROR_SHARING_VIOLATION :
  258. begin
  259. InOutRes :=5;
  260. end;
  261. else
  262. begin
  263. { other error codes can directly be mapped }
  264. InOutRes := Word(errno);
  265. end;
  266. end;
  267. errno:=0;
  268. end;