sysos.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. threadvar
  171. errno : longint;
  172. { misc. functions }
  173. function GetLastError : DWORD;
  174. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetLastError';
  175. procedure SetLastError(dwErrCode : DWORD);
  176. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetLastError';
  177. { time and date functions }
  178. function GetTickCount : longint;
  179. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetTickCount';
  180. {$ifndef WINCE}
  181. { process functions }
  182. procedure ExitProcess(uExitCode : UINT);
  183. stdcall;external KernelDLL name 'ExitProcess';
  184. { Startup }
  185. procedure GetStartupInfo(p : pointer);
  186. stdcall;external KernelDLL name 'GetStartupInfoA';
  187. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  188. stdcall;external KernelDLL name 'GetStdHandle';
  189. {$ifdef FPC_UNICODE_RTL}
  190. { command line/environment functions }
  191. function GetCommandLine : pwidechar;
  192. stdcall;external KernelDLL name 'GetCommandLineW';
  193. {$else}
  194. function GetCommandLine : pchar;
  195. stdcall;external KernelDLL name 'GetCommandLineA';
  196. {$endif}
  197. function GetCurrentProcessId:DWORD;
  198. stdcall; external KernelDLL name 'GetCurrentProcessId';
  199. function Win32GetCurrentThreadId:DWORD;
  200. stdcall; external KernelDLL name 'GetCurrentThreadId';
  201. function GetCurrentProcess : THandle;
  202. stdcall;external 'kernel32' name 'GetCurrentProcess';
  203. function ReadProcessMemory(process : THandle;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  204. stdcall;external 'kernel32' name 'ReadProcessMemory';
  205. { module functions }
  206. function GetModuleFileName(l1:THandle;p:PChar;l2:longint):longint;
  207. stdcall;external KernelDLL name 'GetModuleFileNameA';
  208. function GetModuleHandle(p : PChar) : THandle;
  209. stdcall;external KernelDLL name 'GetModuleHandleA';
  210. {$else WINCE}
  211. { module functions }
  212. function GetModuleFileName(l1:THandle;p:PWideChar;l2:longint):longint;
  213. cdecl;external KernelDLL name 'GetModuleFileNameW';
  214. function GetModuleHandle(p : PWideChar) : THandle;
  215. cdecl;external KernelDLL name 'GetModuleHandleW';
  216. {$endif WINCE}
  217. { file functions }
  218. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  219. overlap:pointer):longint;
  220. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WriteFile';
  221. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  222. overlap:pointer):longint;
  223. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ReadFile';
  224. function CloseHandle(h : thandle) : longint;
  225. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CloseHandle';
  226. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  227. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetFilePointer';
  228. function GetFileSize(h:thandle;p:pointer) : longint;
  229. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
  230. function SetEndOfFile(h : thandle) : longbool;
  231. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
  232. function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
  233. {$ifndef WINCE}
  234. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  235. function GetFileType(Handle:thandle):DWord;
  236. stdcall;external KernelDLL name 'GetFileType';
  237. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  238. { File }
  239. function DeleteFileW(p : punicodechar) : longint;
  240. stdcall;external KernelDLL name 'DeleteFileW';
  241. function MoveFileW(old,_new : punicodechar) : longint;
  242. stdcall;external KernelDLL name 'MoveFileW';
  243. function CreateFileW(lpFileName:punicodechar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  244. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  245. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  246. stdcall;external KernelDLL name 'CreateFileW';
  247. function GetFileAttributesW(p : punicodechar) : dword;
  248. stdcall;external KernelDLL name 'GetFileAttributesW';
  249. { Directory }
  250. function CreateDirectoryW(name : pointer;sec : pointer) : longbool;
  251. stdcall;external KernelDLL name 'CreateDirectoryW';
  252. function RemoveDirectoryW(name:pointer):longbool;
  253. stdcall;external KernelDLL name 'RemoveDirectoryW';
  254. function SetCurrentDirectoryW(name : pointer) : longbool;
  255. stdcall;external KernelDLL name 'SetCurrentDirectoryW';
  256. function GetCurrentDirectoryW(bufsize : longint;name : punicodechar) : Dword;
  257. stdcall;external KernelDLL name 'GetCurrentDirectoryW';
  258. { Console functions needed for WriteFile fix for bug 17550 }
  259. function GetConsoleMode(hConsoleHandle:thandle; lpMode:LPDWORD):BOOL;
  260. stdcall;external KernelDLL name 'GetConsoleMode';
  261. function GetConsoleOutputCP : UINT;
  262. stdcall; external KernelDLL name 'GetConsoleOutputCP';
  263. function GetOEMCP: UINT;
  264. stdcall; external KernelDLL name 'GetOEMCP';
  265. function SysAllocStringLen(psz:pointer;len:dword):pointer;
  266. stdcall; external 'oleaut32.dll' name 'SysAllocStringLen';
  267. procedure SysFreeString(bstr:pointer);
  268. stdcall; external 'oleaut32.dll' name 'SysFreeString';
  269. function SysReAllocStringLen(var bstr:pointer;psz: pointer;
  270. len:dword): Integer; stdcall;external 'oleaut32.dll' name 'SysReAllocStringLen';
  271. {$endif WINCE}
  272. Procedure Errno2InOutRes;
  273. var
  274. res : Word;
  275. pErrno : ^longint;
  276. Begin
  277. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  278. pErrno:=@Errno;
  279. case pErrno^ of
  280. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  281. begin
  282. { This is the offset to the Win32 to add to directly map }
  283. { to the DOS/TP compatible error codes when in this range }
  284. res := word(pErrno^)+131;
  285. end;
  286. ERROR_DISK_FULL :
  287. res := 101;
  288. ERROR_DIR_NOT_EMPTY,
  289. ERROR_ALREADY_EXISTS,
  290. ERROR_SHARING_VIOLATION :
  291. begin
  292. res :=5;
  293. end;
  294. else
  295. begin
  296. { other error codes can directly be mapped }
  297. res := Word(pErrno^);
  298. end;
  299. end;
  300. pErrno^:=0;
  301. InOutRes:=res;
  302. end;
  303. function OleStrToString(source: PWideChar) : UnicodeString;inline;
  304. begin
  305. OleStrToStrVar(source,result);
  306. end;
  307. procedure OleStrToStrVar(source : PWideChar;var dest : UnicodeString);inline;
  308. begin
  309. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  310. end;
  311. procedure OleStrToStrVar(source : PWideChar;var dest : AnsiString);inline;
  312. begin
  313. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  314. end;
  315. function StringToOleStr(const source : ansistring) : PWideChar;inline;
  316. begin
  317. result:=nil;
  318. widestringmanager.Ansi2WideMoveProc(pchar(pointer(source)),StringCodePage(source),widestring(pointer(result)),length(source));
  319. end;
  320. function StringToOleStr(const source : UnicodeString) : PWideChar;inline;
  321. begin
  322. result:=nil;
  323. { will call UnicodeString overload if FPC_WIDESTRING_EQUAL_UNICODESTRING is set }
  324. SetString(widestring(pointer(result)),PWideChar(source),length(source));
  325. end;
  326. {$ifndef WINCE}
  327. {$define FPC_HAS_TRANSLATEPLACEHOLDERCP}
  328. function TranslatePlaceholderCP(cp: TSystemCodePage): TSystemCodePage; {$ifdef SYSTEMINLINE}inline;{$endif}
  329. begin
  330. TranslatePlaceholderCP:=cp;
  331. case cp of
  332. CP_OEMCP:
  333. TranslatePlaceholderCP:=GetOEMCP;
  334. CP_ACP:
  335. TranslatePlaceholderCP:=DefaultSystemCodePage;
  336. end;
  337. end;
  338. {$endif not WINCE}