sysos.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetLastError';
  153. { time and date functions }
  154. function GetTickCount : longint;
  155. {$ifdef wince}cdecl{$else}stdcall{$endif};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. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetModuleFileName' + ApiSuffix;
  176. function GetModuleHandle(p : pointer) : longint;
  177. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetModuleHandle' + ApiSuffix;
  178. { file functions }
  179. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  180. overlap:pointer):longint;
  181. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WriteFile';
  182. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  183. overlap:pointer):longint;
  184. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ReadFile';
  185. function CloseHandle(h : thandle) : longint;
  186. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CloseHandle';
  187. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  188. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetFilePointer';
  189. function GetFileSize(h:thandle;p:pointer) : longint;
  190. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
  191. function SetEndOfFile(h : thandle) : longbool;
  192. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
  193. function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
  194. {$ifndef WINCE}
  195. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  196. function GetFileType(Handle:thandle):DWord;
  197. stdcall;external KernelDLL name 'GetFileType';
  198. function GetFileAttributes(p : pchar) : dword;
  199. stdcall;external KernelDLL name 'GetFileAttributesA';
  200. function DeleteFile(p : pchar) : longint;
  201. stdcall;external KernelDLL name 'DeleteFileA';
  202. function MoveFile(old,_new : pchar) : longint;
  203. stdcall;external KernelDLL name 'MoveFileA';
  204. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  205. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  206. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  207. stdcall;external KernelDLL name 'CreateFileA';
  208. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  209. { Directory }
  210. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  211. stdcall;external KernelDLL name 'CreateDirectoryA';
  212. function RemoveDirectory(name:pointer):longbool;
  213. stdcall;external KernelDLL name 'RemoveDirectoryA';
  214. function SetCurrentDirectory(name : pointer) : longbool;
  215. stdcall;external KernelDLL name 'SetCurrentDirectoryA';
  216. function GetCurrentDirectory(bufsize : longint;name : pchar) : longbool;
  217. stdcall;external KernelDLL name 'GetCurrentDirectoryA';
  218. var
  219. SetFilePointerEx : function(hFile : THandle;
  220. liDistanceToMove : int64;lpNewFilePointer : pint64;
  221. dwMoveMethod : DWord) : ByteBool;stdcall;
  222. procedure SetupProcVars;
  223. var
  224. hinstLib : THandle;
  225. begin
  226. SetFilePointerEx:=nil;
  227. hinstLib:=LoadLibrary(KernelDLL);
  228. if hinstLib<>0 then
  229. begin
  230. pointer(SetFilePointerEx):=GetProcAddress(hinstLib,'SetFilePointerEx');
  231. FreeLibrary(hinstLib);
  232. end;
  233. end;
  234. {$endif WINCE}
  235. Procedure Errno2InOutRes;
  236. var
  237. res : Word;
  238. pErrno : ^longint;
  239. Begin
  240. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  241. pErrno:=@Errno;
  242. case pErrno^ of
  243. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  244. begin
  245. { This is the offset to the Win32 to add to directly map }
  246. { to the DOS/TP compatible error codes when in this range }
  247. res := word(pErrno^)+131;
  248. end;
  249. ERROR_DIR_NOT_EMPTY,
  250. ERROR_ALREADY_EXISTS,
  251. ERROR_SHARING_VIOLATION :
  252. begin
  253. res :=5;
  254. end;
  255. else
  256. begin
  257. { other error codes can directly be mapped }
  258. res := Word(pErrno^);
  259. end;
  260. end;
  261. pErrno^:=0;
  262. InOutRes:=res;
  263. end;
  264. function OleStrToString(source: PWideChar) : ansistring;inline;
  265. begin
  266. OleStrToStrVar(source,result);
  267. end;
  268. procedure OleStrToStrVar(source : PWideChar;var dest : ansistring);inline;
  269. begin
  270. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  271. end;
  272. function StringToOleStr(const source : ansistring) : PWideChar;inline;
  273. begin
  274. result:=nil;
  275. widestringmanager.Ansi2WideMoveProc(pchar(pointer(source)),widestring(pointer(result)),length(source));
  276. end;