sysos.inc 17 KB

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