sysos.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  100. type
  101. {UINT = longint;
  102. BOOL = longint; obsolete }
  103. UINT = cardinal;
  104. BOOL = longbool;
  105. // WCHAR = word;
  106. {$ifdef FPC_OS_UNICODE}
  107. LPTCH = ^word;
  108. LPTSTR = ^word;
  109. LPCTSTR = ^word;
  110. {$else FPC_OS_UNICODE}
  111. LPTCH = ^char;
  112. LPTSTR = ^char;
  113. LPCTSTR = ^char;
  114. {$endif FPC_OS_UNICODE}
  115. LPWSTR = ^wchar;
  116. PVOID = pointer;
  117. LPVOID = pointer;
  118. LPCVOID = pointer;
  119. LPDWORD = ^DWORD;
  120. HLocal = THandle;
  121. PStr = pchar;
  122. LPStr = pchar;
  123. PLPSTR = ^LPSTR;
  124. PLPWSTR = ^LPWSTR;
  125. PSecurityAttributes = ^TSecurityAttributes;
  126. TSecurityAttributes = record
  127. nLength : DWORD;
  128. lpSecurityDescriptor : Pointer;
  129. bInheritHandle : BOOL;
  130. end;
  131. PProcessInformation = ^TProcessInformation;
  132. TProcessInformation = record
  133. hProcess: THandle;
  134. hThread: THandle;
  135. dwProcessId: DWORD;
  136. dwThreadId: DWORD;
  137. end;
  138. PFileTime = ^TFileTime;
  139. TFileTime = record
  140. dwLowDateTime,
  141. dwHighDateTime : DWORD;
  142. end;
  143. LPSystemTime= ^PSystemTime;
  144. PSystemTime = ^TSystemTime;
  145. TSystemTime = record
  146. wYear,
  147. wMonth,
  148. wDayOfWeek,
  149. wDay,
  150. wHour,
  151. wMinute,
  152. wSecond,
  153. wMilliseconds: Word;
  154. end;
  155. TTlsDirectory=packed record
  156. data_start, data_end : pointer;
  157. index_pointer, callbacks_pointer : pointer;
  158. zero_fill_size : dword;
  159. flags : dword;
  160. end;
  161. PExceptionRecord = ^TExceptionRecord;
  162. TExceptionRecord = record
  163. ExceptionCode : cardinal;
  164. ExceptionFlags : cardinal;
  165. ExceptionRecord : PExceptionRecord;
  166. ExceptionAddress : Pointer;
  167. NumberParameters : Longint;
  168. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  169. end;
  170. TSystemInfo = record
  171. case LongInt of
  172. 0 : ( dwOemId : DWord;
  173. dwPageSize : DWord;
  174. lpMinimumApplicationAddress : Pointer;
  175. lpMaximumApplicationAddress : Pointer;
  176. dwActiveProcessorMask : PDWord;
  177. dwNumberOfProcessors : DWord;
  178. dwProcessorType : DWord;
  179. dwAllocationGranularity : DWord;
  180. wProcessorLevel : Word;
  181. wProcessorRevision : Word;
  182. );
  183. 1 : (
  184. wProcessorArchitecture : Word;
  185. wReserved : Word;
  186. );
  187. end;
  188. PSystemInfo = ^TSystemInfo;
  189. { misc. functions }
  190. function GetLastError : DWORD;
  191. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetLastError';
  192. procedure SetLastError(dwErrCode : DWORD);
  193. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetLastError';
  194. { time and date functions }
  195. function GetTickCount : DWORD;
  196. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetTickCount';
  197. {$ifndef WINCE}
  198. { process functions }
  199. procedure ExitProcess(uExitCode : UINT);
  200. stdcall;external KernelDLL name 'ExitProcess';
  201. { Startup }
  202. procedure GetStartupInfo(p : pointer);
  203. stdcall;external KernelDLL name 'GetStartupInfoA';
  204. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  205. stdcall;external KernelDLL name 'GetStdHandle';
  206. {$ifdef FPC_UNICODE_RTLx}
  207. { command line/environment functions }
  208. function GetCommandLine : pwidechar;
  209. stdcall;external KernelDLL name 'GetCommandLineW';
  210. {$else}
  211. function GetCommandLine : pchar;
  212. stdcall;external KernelDLL name 'GetCommandLineA';
  213. {$endif}
  214. function GetCurrentProcessId:DWORD;
  215. stdcall; external KernelDLL name 'GetCurrentProcessId';
  216. function Win32GetCurrentThreadId:DWORD;
  217. stdcall; external KernelDLL name 'GetCurrentThreadId';
  218. function GetCurrentProcess : THandle;
  219. stdcall;external 'kernel32' name 'GetCurrentProcess';
  220. function ReadProcessMemory(process : THandle;address : pointer;dest : pointer;size : ptruint;bytesread : pptruint) : longbool;
  221. stdcall;external 'kernel32' name 'ReadProcessMemory';
  222. { module functions }
  223. function GetModuleFileName(l1:THandle;p:PChar;l2:longint):longint;
  224. stdcall;external KernelDLL name 'GetModuleFileNameA';
  225. function GetModuleHandle(p : PChar) : THandle;
  226. stdcall;external KernelDLL name 'GetModuleHandleA';
  227. {$else WINCE}
  228. { module functions }
  229. function GetModuleFileName(l1:THandle;p:PWideChar;l2:longint):longint;
  230. cdecl;external KernelDLL name 'GetModuleFileNameW';
  231. function GetModuleHandle(p : PWideChar) : THandle;
  232. cdecl;external KernelDLL name 'GetModuleHandleW';
  233. {$endif WINCE}
  234. { file functions }
  235. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  236. overlap:pointer):longint;
  237. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WriteFile';
  238. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  239. overlap:pointer):longint;
  240. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ReadFile';
  241. function CloseHandle(h : thandle) : longint;
  242. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CloseHandle';
  243. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  244. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetFilePointer';
  245. function GetFileSize(h:thandle;p:pointer) : longint;
  246. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
  247. function SetEndOfFile(h : thandle) : longbool;
  248. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
  249. function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
  250. procedure GetSystemInfo(lpSystemInfo: PSystemInfo); {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetSystemInfo';
  251. {$ifndef WINCE}
  252. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  253. function GetFileType(Handle:thandle):DWord;
  254. stdcall;external KernelDLL name 'GetFileType';
  255. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  256. { File }
  257. function DeleteFileW(p : punicodechar) : longint;
  258. stdcall;external KernelDLL name 'DeleteFileW';
  259. function MoveFileW(old,_new : punicodechar) : longint;
  260. stdcall;external KernelDLL name 'MoveFileW';
  261. function CreateFileW(lpFileName:punicodechar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  262. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  263. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  264. stdcall;external KernelDLL name 'CreateFileW';
  265. function GetFileAttributesW(p : punicodechar) : dword;
  266. stdcall;external KernelDLL name 'GetFileAttributesW';
  267. { Directory }
  268. function CreateDirectoryW(name : pointer;sec : pointer) : longbool;
  269. stdcall;external KernelDLL name 'CreateDirectoryW';
  270. function RemoveDirectoryW(name:pointer):longbool;
  271. stdcall;external KernelDLL name 'RemoveDirectoryW';
  272. function SetCurrentDirectoryW(name : pointer) : longbool;
  273. stdcall;external KernelDLL name 'SetCurrentDirectoryW';
  274. function GetCurrentDirectoryW(bufsize : longint;name : punicodechar) : Dword;
  275. stdcall;external KernelDLL name 'GetCurrentDirectoryW';
  276. function GetFullPathNameW (lpFileName: PUnicodeChar; nBufferLength: DWord;
  277. lpBuffer: PUnicodeChar; var lpFilePart: PUnicodeChar): DWord;
  278. stdcall; external KernelDLL name 'GetFullPathNameW';
  279. function SetEnvironmentVariableW (lpName: PUnicodeChar; lpValue: PUnicodeChar): BOOL;
  280. stdcall; external KernelDLL name 'SetEnvironmentVariableW';
  281. { Console functions needed for WriteFile fix for bug 17550 }
  282. function GetConsoleMode(hConsoleHandle:thandle; lpMode:LPDWORD):BOOL;
  283. stdcall;external KernelDLL name 'GetConsoleMode';
  284. function GetConsoleOutputCP : UINT;
  285. stdcall; external KernelDLL name 'GetConsoleOutputCP';
  286. function GetOEMCP: UINT;
  287. stdcall; external KernelDLL name 'GetOEMCP';
  288. function SysAllocStringLen(psz:pointer;len:dword):pointer;
  289. stdcall; external 'oleaut32.dll' name 'SysAllocStringLen';
  290. procedure SysFreeString(bstr:pointer);
  291. stdcall; external 'oleaut32.dll' name 'SysFreeString';
  292. function SysReAllocStringLen(var bstr:pointer;psz: pointer;
  293. len:dword): Integer; stdcall;external 'oleaut32.dll' name 'SysReAllocStringLen';
  294. {$endif WINCE}
  295. Procedure Errno2InOutRes(oserror: longword);
  296. var
  297. res : Word;
  298. Begin
  299. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  300. case oserror of
  301. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  302. begin
  303. { This is the offset to the Win32 to add to directly map }
  304. { to the DOS/TP compatible error codes when in this range }
  305. res := word(oserror)+131;
  306. end;
  307. ERROR_DISK_FULL :
  308. res := 101;
  309. ERROR_DIR_NOT_EMPTY,
  310. ERROR_ALREADY_EXISTS,
  311. ERROR_SHARING_VIOLATION :
  312. begin
  313. res :=5;
  314. end;
  315. else
  316. begin
  317. { other error codes can directly be mapped }
  318. res := Word(oserror);
  319. end;
  320. end;
  321. InOutRes:=res;
  322. end;
  323. function OleStrToString(source: PWideChar) : UnicodeString;inline;
  324. begin
  325. OleStrToStrVar(source,result);
  326. end;
  327. procedure OleStrToStrVar(source : PWideChar;var dest : UnicodeString);inline;
  328. begin
  329. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  330. end;
  331. procedure OleStrToStrVar(source : PWideChar;var dest : AnsiString);inline;
  332. begin
  333. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  334. end;
  335. function StringToOleStr(const source : ansistring) : PWideChar;inline;
  336. begin
  337. result:=nil;
  338. widestringmanager.Ansi2WideMoveProc(pchar(pointer(source)),StringCodePage(source),widestring(pointer(result)),length(source));
  339. end;
  340. function StringToOleStr(const source : UnicodeString) : PWideChar;inline;
  341. begin
  342. result:=nil;
  343. { will call UnicodeString overload if FPC_WIDESTRING_EQUAL_UNICODESTRING is set }
  344. SetString(widestring(pointer(result)),PWideChar(source),length(source));
  345. end;
  346. {$ifndef WINCE}
  347. {$define FPC_HAS_TRANSLATEPLACEHOLDERCP}
  348. function TranslatePlaceholderCP(cp: TSystemCodePage): TSystemCodePage; {$ifdef SYSTEMINLINE}inline;{$endif}
  349. begin
  350. TranslatePlaceholderCP:=cp;
  351. case cp of
  352. CP_OEMCP:
  353. TranslatePlaceholderCP:=GetOEMCP;
  354. CP_ACP:
  355. TranslatePlaceholderCP:=DefaultSystemCodePage;
  356. end;
  357. end;
  358. {$endif not WINCE}
  359. {$define HAS_GETCPUCOUNT}
  360. function GetCPUCount: LongWord;
  361. var
  362. info: TSystemInfo;
  363. begin
  364. FillChar(info, SizeOf(info), 0);
  365. GetSystemInfo(@info);
  366. Result := info.dwNumberOfProcessors;
  367. end;