sysos.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. { command line/environment functions }
  190. function GetCommandLine : pchar;
  191. stdcall;external KernelDLL name 'GetCommandLineA';
  192. function GetCommandLineW : pwidechar;
  193. stdcall;external KernelDLL name 'GetCommandLineW';
  194. function GetCurrentProcessId:DWORD;
  195. stdcall; external KernelDLL name 'GetCurrentProcessId';
  196. function Win32GetCurrentThreadId:DWORD;
  197. stdcall; external KernelDLL name 'GetCurrentThreadId';
  198. function GetCurrentProcess : THandle;
  199. stdcall;external 'kernel32' name 'GetCurrentProcess';
  200. function ReadProcessMemory(process : THandle;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  201. stdcall;external 'kernel32' name 'ReadProcessMemory';
  202. { module functions }
  203. function GetModuleFileName(l1:THandle;p:PChar;l2:longint):longint;
  204. stdcall;external KernelDLL name 'GetModuleFileNameA';
  205. function GetModuleHandle(p : PChar) : THandle;
  206. stdcall;external KernelDLL name 'GetModuleHandleA';
  207. {$else WINCE}
  208. { module functions }
  209. function GetModuleFileName(l1:THandle;p:PWideChar;l2:longint):longint;
  210. cdecl;external KernelDLL name 'GetModuleFileNameW';
  211. function GetModuleHandle(p : PWideChar) : THandle;
  212. cdecl;external KernelDLL name 'GetModuleHandleW';
  213. {$endif WINCE}
  214. { file functions }
  215. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  216. overlap:pointer):longint;
  217. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WriteFile';
  218. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  219. overlap:pointer):longint;
  220. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ReadFile';
  221. function CloseHandle(h : thandle) : longint;
  222. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CloseHandle';
  223. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  224. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetFilePointer';
  225. function GetFileSize(h:thandle;p:pointer) : longint;
  226. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
  227. function SetEndOfFile(h : thandle) : longbool;
  228. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
  229. function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
  230. {$ifndef WINCE}
  231. function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
  232. function GetFileType(Handle:thandle):DWord;
  233. stdcall;external KernelDLL name 'GetFileType';
  234. function GetFileAttributes(p : pchar) : dword;
  235. stdcall;external KernelDLL name 'GetFileAttributesA';
  236. function DeleteFile(p : pchar) : longint;
  237. stdcall;external KernelDLL name 'DeleteFileA';
  238. function MoveFile(old,_new : pchar) : longint;
  239. stdcall;external KernelDLL name 'MoveFileA';
  240. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  241. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  242. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):THandle;
  243. stdcall;external KernelDLL name 'CreateFileA';
  244. function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
  245. { Directory }
  246. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  247. stdcall;external KernelDLL name 'CreateDirectoryA';
  248. function RemoveDirectory(name:pointer):longbool;
  249. stdcall;external KernelDLL name 'RemoveDirectoryA';
  250. function SetCurrentDirectory(name : pointer) : longbool;
  251. stdcall;external KernelDLL name 'SetCurrentDirectoryA';
  252. function GetCurrentDirectory(bufsize : longint;name : pchar) : longbool;
  253. stdcall;external KernelDLL name 'GetCurrentDirectoryA';
  254. { Console functions needed for WriteFile fix for bug 17550 }
  255. function GetConsoleMode(hConsoleHandle:thandle; lpMode:LPDWORD):BOOL;
  256. stdcall;external KernelDLL name 'GetConsoleMode';
  257. function GetConsoleOutputCP : UINT;
  258. stdcall; external KernelDLL name 'GetConsoleOutputCP';
  259. function SysAllocStringLen(psz:pointer;len:dword):pointer;
  260. stdcall; external 'oleaut32.dll' name 'SysAllocStringLen';
  261. procedure SysFreeString(bstr:pointer);
  262. stdcall; external 'oleaut32.dll' name 'SysFreeString';
  263. function SysReAllocStringLen(var bstr:pointer;psz: pointer;
  264. len:dword): Integer; stdcall;external 'oleaut32.dll' name 'SysReAllocStringLen';
  265. {$endif WINCE}
  266. Procedure Errno2InOutRes;
  267. var
  268. res : Word;
  269. pErrno : ^longint;
  270. Begin
  271. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  272. pErrno:=@Errno;
  273. case pErrno^ of
  274. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  275. begin
  276. { This is the offset to the Win32 to add to directly map }
  277. { to the DOS/TP compatible error codes when in this range }
  278. res := word(pErrno^)+131;
  279. end;
  280. ERROR_DISK_FULL :
  281. res := 101;
  282. ERROR_DIR_NOT_EMPTY,
  283. ERROR_ALREADY_EXISTS,
  284. ERROR_SHARING_VIOLATION :
  285. begin
  286. res :=5;
  287. end;
  288. else
  289. begin
  290. { other error codes can directly be mapped }
  291. res := Word(pErrno^);
  292. end;
  293. end;
  294. pErrno^:=0;
  295. InOutRes:=res;
  296. end;
  297. function OleStrToString(source: PWideChar) : UnicodeString;inline;
  298. begin
  299. OleStrToStrVar(source,result);
  300. end;
  301. procedure OleStrToStrVar(source : PWideChar;var dest : UnicodeString);inline;
  302. begin
  303. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  304. end;
  305. procedure OleStrToStrVar(source : PWideChar;var dest : AnsiString);inline;
  306. begin
  307. WideCharLenToStrVar(source,length(WideString(pointer(source))),dest);
  308. end;
  309. function StringToOleStr(const source : ansistring) : PWideChar;inline;
  310. begin
  311. result:=nil;
  312. widestringmanager.Ansi2WideMoveProc(pchar(pointer(source)),StringCodePage(source),widestring(pointer(result)),length(source));
  313. end;
  314. function StringToOleStr(const source : UnicodeString) : PWideChar;inline;
  315. begin
  316. result:=nil;
  317. { will call UnicodeString overload if FPC_WIDESTRING_EQUAL_UNICODESTRING is set }
  318. SetString(widestring(pointer(result)),PWideChar(source),length(source));
  319. end;