sysos.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. { DllEntryPoint }
  54. DLL_PROCESS_ATTACH = 1;
  55. DLL_THREAD_ATTACH = 2;
  56. DLL_PROCESS_DETACH = 0;
  57. DLL_THREAD_DETACH = 3;
  58. { These constants are used for conversion of error codes }
  59. { from win32 i/o errors to tp i/o errors }
  60. { errors 1 to 18 are the same as in Turbo Pascal }
  61. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  62. { The media is write protected. }
  63. ERROR_WRITE_PROTECT = 19;
  64. { The system cannot find the device specified. }
  65. ERROR_BAD_UNIT = 20;
  66. { The device is not ready. }
  67. ERROR_NOT_READY = 21;
  68. { The device does not recognize the command. }
  69. ERROR_BAD_COMMAND = 22;
  70. { Data error (cyclic redundancy check) }
  71. ERROR_CRC = 23;
  72. { The program issued a command but the }
  73. { command length is incorrect. }
  74. ERROR_BAD_LENGTH = 24;
  75. { The drive cannot locate a specific }
  76. { area or track on the disk. }
  77. ERROR_SEEK = 25;
  78. { The specified disk or diskette cannot be accessed. }
  79. ERROR_NOT_DOS_DISK = 26;
  80. { The drive cannot find the sector requested. }
  81. ERROR_SECTOR_NOT_FOUND = 27;
  82. { The printer is out of paper. }
  83. ERROR_OUT_OF_PAPER = 28;
  84. { The system cannot write to the specified device. }
  85. ERROR_WRITE_FAULT = 29;
  86. { The system cannot read from the specified device. }
  87. ERROR_READ_FAULT = 30;
  88. { A device attached to the system is not functioning.}
  89. ERROR_GEN_FAILURE = 31;
  90. { The process cannot access the file because }
  91. { it is being used by another process. }
  92. ERROR_SHARING_VIOLATION = 32;
  93. { A pipe has been closed on the other end }
  94. { Removing that error allows eof to works as on other OSes }
  95. ERROR_BROKEN_PIPE = 109;
  96. ERROR_DISK_FULL = 112;
  97. ERROR_DIR_NOT_EMPTY = 145;
  98. ERROR_ALREADY_EXISTS = 183;
  99. ERROR_FILENAME_EXCED_RANGE = 206;
  100. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  101. type
  102. {UINT = longint;
  103. BOOL = longint; obsolete }
  104. UINT = cardinal;
  105. BOOL = longbool;
  106. // WCHAR = word;
  107. {$ifdef FPC_OS_UNICODE}
  108. LPTCH = ^word;
  109. LPTSTR = ^word;
  110. LPCTSTR = ^word;
  111. {$else FPC_OS_UNICODE}
  112. LPTCH = ^char;
  113. LPTSTR = ^char;
  114. LPCTSTR = ^char;
  115. {$endif FPC_OS_UNICODE}
  116. LPWSTR = ^wchar;
  117. PVOID = pointer;
  118. LPVOID = pointer;
  119. LPCVOID = pointer;
  120. LPDWORD = ^DWORD;
  121. HLocal = THandle;
  122. PStr = pchar;
  123. LPStr = pchar;
  124. PLPSTR = ^LPSTR;
  125. PLPWSTR = ^LPWSTR;
  126. PSecurityAttributes = ^TSecurityAttributes;
  127. TSecurityAttributes = record
  128. nLength : DWORD;
  129. lpSecurityDescriptor : Pointer;
  130. bInheritHandle : BOOL;
  131. end;
  132. PProcessInformation = ^TProcessInformation;
  133. TProcessInformation = record
  134. hProcess: THandle;
  135. hThread: THandle;
  136. dwProcessId: DWORD;
  137. dwThreadId: DWORD;
  138. end;
  139. PFileTime = ^TFileTime;
  140. TFileTime = record
  141. dwLowDateTime,
  142. dwHighDateTime : DWORD;
  143. end;
  144. LPSystemTime= ^PSystemTime;
  145. PSystemTime = ^TSystemTime;
  146. TSystemTime = record
  147. wYear,
  148. wMonth,
  149. wDayOfWeek,
  150. wDay,
  151. wHour,
  152. wMinute,
  153. wSecond,
  154. wMilliseconds: Word;
  155. end;
  156. TTlsDirectory=packed record
  157. data_start, data_end : pointer;
  158. index_pointer, callbacks_pointer : pointer;
  159. zero_fill_size : dword;
  160. flags : dword;
  161. end;
  162. PExceptionRecord = ^TExceptionRecord;
  163. TExceptionRecord = record
  164. ExceptionCode : cardinal;
  165. ExceptionFlags : cardinal;
  166. ExceptionRecord : PExceptionRecord;
  167. ExceptionAddress : Pointer;
  168. NumberParameters : Longint;
  169. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  170. end;
  171. TSystemInfo = record
  172. case LongInt of
  173. 0 : ( dwOemId : DWord;
  174. dwPageSize : DWord;
  175. lpMinimumApplicationAddress : Pointer;
  176. lpMaximumApplicationAddress : Pointer;
  177. dwActiveProcessorMask : PDWord;
  178. dwNumberOfProcessors : DWord;
  179. dwProcessorType : DWord;
  180. dwAllocationGranularity : DWord;
  181. wProcessorLevel : Word;
  182. wProcessorRevision : Word;
  183. );
  184. 1 : (
  185. wProcessorArchitecture : Word;
  186. wReserved : Word;
  187. );
  188. end;
  189. PSystemInfo = ^TSystemInfo;
  190. { misc. functions }
  191. function GetLastError : DWORD;
  192. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetLastError';
  193. procedure SetLastError(dwErrCode : DWORD);
  194. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetLastError';
  195. { time and date functions }
  196. function GetTickCount : DWORD;
  197. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetTickCount';
  198. {$ifndef WINCE}
  199. { process functions }
  200. procedure ExitProcess(uExitCode : UINT);
  201. stdcall;external KernelDLL name 'ExitProcess';
  202. { Startup }
  203. procedure GetStartupInfo(p : pointer);
  204. stdcall;external KernelDLL name 'GetStartupInfoA';
  205. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  206. stdcall;external KernelDLL name 'GetStdHandle';
  207. {$ifdef FPC_UNICODE_RTLx}
  208. { command line/environment functions }
  209. function GetCommandLine : pwidechar;
  210. stdcall;external KernelDLL name 'GetCommandLineW';
  211. {$else}
  212. function GetCommandLine : pchar;
  213. stdcall;external KernelDLL name 'GetCommandLineA';
  214. {$endif}
  215. function GetCurrentProcessId:DWORD;
  216. stdcall; external KernelDLL name 'GetCurrentProcessId';
  217. function Win32GetCurrentThreadId:DWORD;
  218. stdcall; external KernelDLL name 'GetCurrentThreadId';
  219. function GetCurrentProcess : THandle;
  220. stdcall;external 'kernel32' name 'GetCurrentProcess';
  221. function ReadProcessMemory(process : THandle;address : pointer;dest : pointer;size : ptruint;bytesread : pptruint) : longbool;
  222. stdcall;external 'kernel32' name 'ReadProcessMemory';
  223. { module functions }
  224. function GetModuleFileName(l1:THandle;p:PChar;l2:longint):longint;
  225. stdcall;external KernelDLL name 'GetModuleFileNameA';
  226. function GetModuleHandle(p : PChar) : THandle;
  227. stdcall;external KernelDLL name 'GetModuleHandleA';
  228. {$else WINCE}
  229. { module functions }
  230. function GetModuleFileName(l1:THandle;p:PWideChar;l2:longint):longint;
  231. cdecl;external KernelDLL name 'GetModuleFileNameW';
  232. function GetModuleHandle(p : PWideChar) : THandle;
  233. cdecl;external KernelDLL name 'GetModuleHandleW';
  234. {$endif WINCE}
  235. { file functions }
  236. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  237. overlap:pointer):longint;
  238. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WriteFile';
  239. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  240. overlap:pointer):longint;
  241. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ReadFile';
  242. function CloseHandle(h : thandle) : longint;
  243. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CloseHandle';
  244. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  245. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetFilePointer';
  246. function GetFileSize(h:thandle;p:pointer) : longint;
  247. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
  248. function SetEndOfFile(h : thandle) : longbool;
  249. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
  250. function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
  251. procedure GetSystemInfo(lpSystemInfo: PSystemInfo); {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetSystemInfo';
  252. {$ifndef WINCE}
  253. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  254. function GetFileType(Handle:thandle):DWord;
  255. stdcall;external KernelDLL name 'GetFileType';
  256. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  257. { File }
  258. function DeleteFileW(p : punicodechar) : longint;
  259. stdcall;external KernelDLL name 'DeleteFileW';
  260. function MoveFileW(old,_new : punicodechar) : longint;
  261. stdcall;external KernelDLL name 'MoveFileW';
  262. function CreateFileW(lpFileName:punicodechar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  263. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  264. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  265. stdcall;external KernelDLL name 'CreateFileW';
  266. function GetFileAttributesW(p : punicodechar) : dword;
  267. stdcall;external KernelDLL name 'GetFileAttributesW';
  268. { Directory }
  269. function CreateDirectoryW(name : pointer;sec : pointer) : longbool;
  270. stdcall;external KernelDLL name 'CreateDirectoryW';
  271. function RemoveDirectoryW(name:pointer):longbool;
  272. stdcall;external KernelDLL name 'RemoveDirectoryW';
  273. function SetCurrentDirectoryW(name : pointer) : longbool;
  274. stdcall;external KernelDLL name 'SetCurrentDirectoryW';
  275. function GetCurrentDirectoryW(bufsize : longint;name : punicodechar) : Dword;
  276. stdcall;external KernelDLL name 'GetCurrentDirectoryW';
  277. function GetFullPathNameW (lpFileName: PUnicodeChar; nBufferLength: DWord;
  278. lpBuffer: PUnicodeChar; var lpFilePart: PUnicodeChar): DWord;
  279. stdcall; external KernelDLL name 'GetFullPathNameW';
  280. function SetEnvironmentVariableW (lpName: PUnicodeChar; lpValue: PUnicodeChar): BOOL;
  281. stdcall; external KernelDLL name 'SetEnvironmentVariableW';
  282. { Console functions needed for WriteFile fix for bug 17550 }
  283. function GetConsoleMode(hConsoleHandle:thandle; lpMode:LPDWORD):BOOL;
  284. stdcall;external KernelDLL name 'GetConsoleMode';
  285. function GetConsoleOutputCP : UINT;
  286. stdcall; external KernelDLL name 'GetConsoleOutputCP';
  287. function GetOEMCP: UINT;
  288. stdcall; external KernelDLL name 'GetOEMCP';
  289. function SysAllocStringLen(psz:pointer;len:dword):pointer;
  290. stdcall; external 'oleaut32.dll' name 'SysAllocStringLen';
  291. procedure SysFreeString(bstr:pointer);
  292. stdcall; external 'oleaut32.dll' name 'SysFreeString';
  293. function SysReAllocStringLen(var bstr:pointer;psz: pointer;
  294. len:dword): Integer; stdcall;external 'oleaut32.dll' name 'SysReAllocStringLen';
  295. {$endif WINCE}
  296. Procedure Errno2InOutRes(oserror: longword);
  297. var
  298. res : Word;
  299. Begin
  300. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  301. case oserror of
  302. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  303. begin
  304. { This is the offset to the Win32 to add to directly map }
  305. { to the DOS/TP compatible error codes when in this range }
  306. res := word(oserror)+131;
  307. end;
  308. ERROR_DISK_FULL :
  309. res := 101;
  310. ERROR_DIR_NOT_EMPTY,
  311. ERROR_ALREADY_EXISTS,
  312. ERROR_SHARING_VIOLATION :
  313. begin
  314. res :=5;
  315. end;
  316. ERROR_FILENAME_EXCED_RANGE : Res := 3;
  317. else
  318. begin
  319. { other error codes can directly be mapped }
  320. res := Word(oserror);
  321. end;
  322. end;
  323. InOutRes:=res;
  324. end;
  325. function OleStrToString(source: PWideChar) : UnicodeString;inline;
  326. begin
  327. OleStrToStrVar(source,result);
  328. end;
  329. procedure OleStrToStrVar(source : PWideChar;var dest : UnicodeString);inline;
  330. begin
  331. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  332. end;
  333. procedure OleStrToStrVar(source : PWideChar;var dest : AnsiString);inline;
  334. begin
  335. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  336. end;
  337. function StringToOleStr(const source : ansistring) : PWideChar;inline;
  338. begin
  339. result:=nil;
  340. widestringmanager.Ansi2WideMoveProc(pchar(pointer(source)),StringCodePage(source),widestring(pointer(result)),length(source));
  341. end;
  342. function StringToOleStr(const source : UnicodeString) : PWideChar;inline;
  343. begin
  344. result:=nil;
  345. { will call UnicodeString overload if FPC_WIDESTRING_EQUAL_UNICODESTRING is set }
  346. SetString(widestring(pointer(result)),PWideChar(source),length(source));
  347. end;
  348. {$ifndef WINCE}
  349. {$define FPC_HAS_TRANSLATEPLACEHOLDERCP}
  350. function TranslatePlaceholderCP(cp: TSystemCodePage): TSystemCodePage; {$ifdef SYSTEMINLINE}inline;{$endif}
  351. begin
  352. TranslatePlaceholderCP:=cp;
  353. case cp of
  354. CP_OEMCP:
  355. TranslatePlaceholderCP:=GetOEMCP;
  356. CP_ACP:
  357. TranslatePlaceholderCP:=DefaultSystemCodePage;
  358. end;
  359. end;
  360. {$endif not WINCE}
  361. {$define HAS_GETCPUCOUNT}
  362. function GetCPUCount: LongWord;
  363. var
  364. info: TSystemInfo;
  365. begin
  366. FillChar(info, SizeOf(info), 0);
  367. GetSystemInfo(@info);
  368. Result := info.dwNumberOfProcessors;
  369. end;