syswin.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2008 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit part shared by win32/win64.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. Error code definitions for the Win32 API functions
  14. Values are 32 bit values layed out as follows:
  15. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  16. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  17. +---+-+-+-----------------------+-------------------------------+
  18. |Sev|C|R| Facility | Code |
  19. +---+-+-+-----------------------+-------------------------------+
  20. where
  21. Sev - is the severity code
  22. 00 - Success
  23. 01 - Informational
  24. 10 - Warning
  25. 11 - Error
  26. C - is the Customer code flag
  27. R - is a reserved bit
  28. Facility - is the facility code
  29. Code - is the facility's status code
  30. }
  31. const
  32. SEVERITY_SUCCESS = $00000000;
  33. SEVERITY_INFORMATIONAL = $40000000;
  34. SEVERITY_WARNING = $80000000;
  35. SEVERITY_ERROR = $C0000000;
  36. const
  37. STATUS_SEGMENT_NOTIFICATION = $40000005;
  38. DBG_TERMINATE_THREAD = $40010003;
  39. DBG_TERMINATE_PROCESS = $40010004;
  40. DBG_CONTROL_C = $40010005;
  41. DBG_CONTROL_BREAK = $40010008;
  42. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  43. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  44. STATUS_BREAKPOINT = $80000003;
  45. STATUS_SINGLE_STEP = $80000004;
  46. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  47. STATUS_ACCESS_VIOLATION = $C0000005;
  48. STATUS_IN_PAGE_ERROR = $C0000006;
  49. STATUS_INVALID_HANDLE = $C0000008;
  50. STATUS_NO_MEMORY = $C0000017;
  51. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  52. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  53. STATUS_INVALID_DISPOSITION = $C0000026;
  54. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  55. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  56. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  57. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  58. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  59. STATUS_FLOAT_OVERFLOW = $C0000091;
  60. STATUS_FLOAT_STACK_CHECK = $C0000092;
  61. STATUS_FLOAT_UNDERFLOW = $C0000093;
  62. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  63. STATUS_INTEGER_OVERFLOW = $C0000095;
  64. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  65. STATUS_STACK_OVERFLOW = $C00000FD;
  66. STATUS_CONTROL_C_EXIT = $C000013A;
  67. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  68. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  69. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  70. EXCEPTION_EXECUTE_HANDLER = 1;
  71. EXCEPTION_CONTINUE_EXECUTION = -1;
  72. EXCEPTION_CONTINUE_SEARCH = 0;
  73. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  74. CONTEXT_X86 = $00010000;
  75. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  76. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  77. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  78. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  79. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  80. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  81. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  82. MAXIMUM_SUPPORTED_EXTENSION = 512;
  83. {*****************************************************************************
  84. Parameter Handling
  85. *****************************************************************************}
  86. procedure setup_arguments;
  87. var
  88. arglen,
  89. count : longint;
  90. argstart,
  91. pc,arg : pchar;
  92. quote : Boolean;
  93. argvlen : longint;
  94. buf: array[0..259] of char; // need MAX_PATH bytes, not 256!
  95. procedure allocarg(idx,len:longint);
  96. var
  97. oldargvlen : longint;
  98. begin
  99. if idx>=argvlen then
  100. begin
  101. oldargvlen:=argvlen;
  102. argvlen:=(idx+8) and (not 7);
  103. sysreallocmem(argv,argvlen*sizeof(pointer));
  104. fillchar(argv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  105. end;
  106. { use realloc to reuse already existing memory }
  107. { always allocate, even if length is zero, since }
  108. { the arg. is still present! }
  109. sysreallocmem(argv[idx],len+1);
  110. end;
  111. begin
  112. { create commandline, it starts with the executed filename which is argv[0] }
  113. { Win32 passes the command NOT via the args, but via getmodulefilename}
  114. count:=0;
  115. argv:=nil;
  116. argvlen:=0;
  117. ArgLen := GetModuleFileName(0, @buf[0], sizeof(buf));
  118. buf[ArgLen] := #0; // be safe
  119. allocarg(0,arglen);
  120. move(buf,argv[0]^,arglen+1);
  121. { Setup cmdline variable }
  122. cmdline:=GetCommandLine;
  123. { process arguments }
  124. pc:=cmdline;
  125. {$IfDef SYSTEM_DEBUG_STARTUP}
  126. Writeln(stderr,'Win32 GetCommandLine is #',pc,'#');
  127. {$EndIf }
  128. while pc^<>#0 do
  129. begin
  130. { skip leading spaces }
  131. while pc^ in [#1..#32] do
  132. inc(pc);
  133. if pc^=#0 then
  134. break;
  135. { calc argument length }
  136. quote:=False;
  137. argstart:=pc;
  138. arglen:=0;
  139. while (pc^<>#0) do
  140. begin
  141. case pc^ of
  142. #1..#32 :
  143. begin
  144. if quote then
  145. inc(arglen)
  146. else
  147. break;
  148. end;
  149. '"' :
  150. if pc[1]<>'"' then
  151. quote := not quote
  152. else
  153. inc(pc);
  154. else
  155. inc(arglen);
  156. end;
  157. inc(pc);
  158. end;
  159. { copy argument }
  160. { Don't copy the first one, it is already there.}
  161. If Count<>0 then
  162. begin
  163. allocarg(count,arglen);
  164. quote:=False;
  165. pc:=argstart;
  166. arg:=argv[count];
  167. while (pc^<>#0) do
  168. begin
  169. case pc^ of
  170. #1..#32 :
  171. begin
  172. if quote then
  173. begin
  174. arg^:=pc^;
  175. inc(arg);
  176. end
  177. else
  178. break;
  179. end;
  180. '"' :
  181. if pc[1]<>'"' then
  182. quote := not quote
  183. else
  184. inc(pc);
  185. else
  186. begin
  187. arg^:=pc^;
  188. inc(arg);
  189. end;
  190. end;
  191. inc(pc);
  192. end;
  193. arg^:=#0;
  194. end;
  195. {$IfDef SYSTEM_DEBUG_STARTUP}
  196. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  197. {$EndIf SYSTEM_DEBUG_STARTUP}
  198. inc(count);
  199. end;
  200. { get argc }
  201. argc:=count;
  202. { free unused memory, leaving a nil entry at the end }
  203. sysreallocmem(argv,(count+1)*sizeof(pointer));
  204. argv[count] := nil;
  205. end;
  206. function paramcount : longint;
  207. begin
  208. paramcount := argc - 1;
  209. end;
  210. function paramstr(l : longint) : string;
  211. begin
  212. if (l>=0) and (l<argc) then
  213. paramstr:=strpas(argv[l])
  214. else
  215. paramstr:='';
  216. end;
  217. procedure randomize;
  218. begin
  219. randseed:=GetTickCount;
  220. end;
  221. Const
  222. DLL_PROCESS_ATTACH = 1;
  223. DLL_THREAD_ATTACH = 2;
  224. DLL_PROCESS_DETACH = 0;
  225. DLL_THREAD_DETACH = 3;
  226. DLLExitOK : boolean = true;
  227. Var
  228. DLLBuf : Jmp_buf;
  229. function Dll_entry{$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}(const info : TEntryInformation){$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} : longbool; [public,alias:'_FPC_DLL_Entry'];
  230. begin
  231. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  232. EntryInformation:=info;
  233. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  234. IsLibrary:=true;
  235. Dll_entry:=false; { return value is ignored, except when DLLreason=DLL_PROCESS_ATTACH }
  236. case DLLreason of
  237. DLL_PROCESS_ATTACH :
  238. begin
  239. MainThreadIdWin32 := Win32GetCurrentThreadId;
  240. If SetJmp(DLLBuf) = 0 then
  241. begin
  242. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  243. EntryInformation.PascalMain();
  244. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  245. PascalMain;
  246. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  247. Dll_entry:=true;
  248. end
  249. else
  250. Dll_entry:=DLLExitOK;
  251. end;
  252. DLL_THREAD_ATTACH :
  253. begin
  254. { SysInitMultithreading must not be called here,
  255. see comments in exec_tls_callback below }
  256. { Allocate Threadvars }
  257. SysAllocateThreadVars;
  258. { NS : no idea what is correct to pass here - pass dummy value for now }
  259. { passing a dummy is ok, the correct value is read from the coff header of SysInstance (FK) }
  260. InitThread($1000000); { Assume everything is idempotent there, as the thread could have been created with BeginThread... }
  261. if assigned(Dll_Thread_Attach_Hook) then
  262. Dll_Thread_Attach_Hook(DllParam);
  263. end;
  264. DLL_THREAD_DETACH :
  265. begin
  266. if assigned(Dll_Thread_Detach_Hook) then
  267. Dll_Thread_Detach_Hook(DllParam);
  268. { Release Threadvars }
  269. if TlsGetValue(TLSKey)<>nil then
  270. DoneThread; { Assume everything is idempotent there }
  271. end;
  272. DLL_PROCESS_DETACH :
  273. begin
  274. if MainThreadIDWin32=0 then // already been here.
  275. exit;
  276. If SetJmp(DLLBuf) = 0 then
  277. FPC_Do_Exit;
  278. if assigned(Dll_Process_Detach_Hook) then
  279. Dll_Process_Detach_Hook(DllParam);
  280. SysReleaseThreadVars;
  281. { Free TLS resources used by ThreadVars }
  282. SysFiniMultiThreading;
  283. MainThreadIDWin32:=0;
  284. end;
  285. end;
  286. end;
  287. Procedure ExitDLL(Exitcode : longint);
  288. begin
  289. DLLExitOK:=ExitCode=0;
  290. LongJmp(DLLBuf,1);
  291. end;
  292. {$ifdef FPC_USE_TLS_DIRECTORY}
  293. { Process TLS callback function }
  294. { This is only useful for executables
  295. for DLLs, DLL_Entry gets called. PM }
  296. procedure Exec_Tls_callback(Handle : pointer; reason : Dword; Reserved : pointer);
  297. stdcall; [public,alias:'_FPC_Tls_Callback'];
  298. begin
  299. if IsLibrary then
  300. Exit;
  301. case reason of
  302. { For executables, DLL_PROCESS_ATTACH is called *before* the entry point,
  303. and DLL_PROCESS_DETACH is called *after* RTL shuts down and calls ExitProcess.
  304. It isn't a good idea to handle resources of the main thread at these points. }
  305. DLL_THREAD_ATTACH :
  306. begin
  307. { !!! SysInitMultithreading must NOT be called here. Windows guarantees that
  308. the main thread invokes PROCESS_ATTACH, not THREAD_ATTACH. So this always
  309. executes in non-main thread. SysInitMultithreading() here will cause
  310. initial threadvars to be copied to TLS of non-main thread, and threadvars
  311. of the main thread will be reinitialized upon the next access with zeroes,
  312. ending up in a delayed failure which is very hard to debug.
  313. Fortunately this nasty scenario can happen only when the first non-main thread
  314. was created outside of RTL (Sergei).
  315. }
  316. { Allocate Threadvars }
  317. SysAllocateThreadVars;
  318. { NS : no idea what is correct to pass here - pass dummy value for now }
  319. { passing a dummy is ok, the correct value is read from the coff header of SysInstance (FK) }
  320. InitThread($1000000); { Assume everything is idempotent there, as the thread could have been created with BeginThread... }
  321. end;
  322. DLL_THREAD_DETACH :
  323. begin
  324. if TlsGetValue(TLSKey)<>nil then
  325. DoneThread; { Assume everything is idempotent there }
  326. end;
  327. end;
  328. end;
  329. const
  330. FreePascal_TLS_callback : pointer = @Exec_Tls_callback;
  331. public name '__FPC_tls_callbacks' section '.CRT$XLFPC';
  332. FreePascal_end_of_TLS_callback : pointer = nil;
  333. public name '__FPC_end_of_tls_callbacks' section '.CRT$XLZZZ';
  334. var
  335. tls_callbacks : pointer; external name '___crt_xl_start__';
  336. tls_data_start : pointer; external name '___tls_start__';
  337. tls_data_end : pointer; external name '___tls_end__';
  338. _tls_index : dword; cvar; external;
  339. const
  340. _tls_used : TTlsDirectory = (
  341. data_start : @tls_data_start;
  342. data_end : @tls_data_end;
  343. index_pointer : @_tls_index;
  344. callbacks_pointer : @tls_callbacks;
  345. zero_fill_size : 0;
  346. flags : 0;
  347. ); cvar; public;
  348. {$ifdef win64}
  349. { This is a hack to support external linking.
  350. All released win64 versions of GNU binutils miss proper prefix handling
  351. when searching for _tls_used and expect two leading underscores.
  352. The issue has been fixed in binutils snapshots, but not released yet.
  353. TODO: This should be removed as soon as next version of binutils (>2.21) is
  354. released and we upgrade to it. }
  355. __tls_used : TTlsDirectory = (
  356. data_start : @tls_data_start;
  357. data_end : @tls_data_end;
  358. index_pointer : @_tls_index;
  359. callbacks_pointer : @tls_callbacks;
  360. zero_fill_size : 0;
  361. flags : 0;
  362. ); cvar; public;
  363. {$endif win64}
  364. {$endif FPC_USE_TLS_DIRECTORY}
  365. {****************************************************************************
  366. Error Message writing using messageboxes
  367. ****************************************************************************}
  368. function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
  369. stdcall;external 'user32' name 'MessageBoxA';
  370. const
  371. ErrorBufferLength = 1024;
  372. var
  373. ErrorBuf : array[0..ErrorBufferLength] of char;
  374. ErrorLen : SizeInt;
  375. Function ErrorWrite(Var F: TextRec): Integer;
  376. {
  377. An error message should always end with #13#10#13#10
  378. }
  379. var
  380. i : SizeInt;
  381. Begin
  382. while F.BufPos>0 do
  383. begin
  384. begin
  385. if F.BufPos+ErrorLen>ErrorBufferLength then
  386. i:=ErrorBufferLength-ErrorLen
  387. else
  388. i:=F.BufPos;
  389. Move(F.BufPtr^,ErrorBuf[ErrorLen],i);
  390. inc(ErrorLen,i);
  391. ErrorBuf[ErrorLen]:=#0;
  392. end;
  393. if ErrorLen=ErrorBufferLength then
  394. begin
  395. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  396. ErrorLen:=0;
  397. end;
  398. Dec(F.BufPos,i);
  399. end;
  400. ErrorWrite:=0;
  401. End;
  402. Function ErrorClose(Var F: TextRec): Integer;
  403. begin
  404. if ErrorLen>0 then
  405. begin
  406. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  407. ErrorLen:=0;
  408. end;
  409. ErrorLen:=0;
  410. ErrorClose:=0;
  411. end;
  412. Function ErrorOpen(Var F: TextRec): Integer;
  413. Begin
  414. TextRec(F).InOutFunc:=@ErrorWrite;
  415. TextRec(F).FlushFunc:=@ErrorWrite;
  416. TextRec(F).CloseFunc:=@ErrorClose;
  417. ErrorLen:=0;
  418. ErrorOpen:=0;
  419. End;
  420. procedure AssignError(Var T: Text);
  421. begin
  422. Assign(T,'');
  423. TextRec(T).OpenFunc:=@ErrorOpen;
  424. Rewrite(T);
  425. end;
  426. procedure SysInitStdIO;
  427. begin
  428. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  429. displayed in a messagebox }
  430. StdInputHandle:=longint(GetStdHandle(cardinal(STD_INPUT_HANDLE)));
  431. StdOutputHandle:=longint(GetStdHandle(cardinal(STD_OUTPUT_HANDLE)));
  432. StdErrorHandle:=longint(GetStdHandle(cardinal(STD_ERROR_HANDLE)));
  433. if not IsConsole then
  434. begin
  435. AssignError(stderr);
  436. AssignError(StdOut);
  437. Assign(Output,'');
  438. Assign(Input,'');
  439. Assign(ErrOutput,'');
  440. end
  441. else
  442. begin
  443. OpenStdIO(Input,fmInput,StdInputHandle);
  444. OpenStdIO(Output,fmOutput,StdOutputHandle);
  445. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  446. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  447. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  448. end;
  449. end;
  450. { ProcessID cached to avoid repeated calls to GetCurrentProcess. }
  451. var
  452. ProcessID: SizeUInt;
  453. function GetProcessID: SizeUInt;
  454. begin
  455. GetProcessID := ProcessID;
  456. end;
  457. {******************************************************************************
  458. Unicode
  459. ******************************************************************************}
  460. const
  461. { MultiByteToWideChar }
  462. MB_PRECOMPOSED = 1;
  463. CP_ACP = 0;
  464. CP_UTF16 = 1200;
  465. CP_UTF8 = 65001;
  466. WC_NO_BEST_FIT_CHARS = $400;
  467. function MultiByteToWideChar(CodePage:UINT; dwFlags:DWORD; lpMultiByteStr:PChar; cchMultiByte:longint; lpWideCharStr:PWideChar;cchWideChar:longint):longint;
  468. stdcall; external 'kernel32' name 'MultiByteToWideChar';
  469. function WideCharToMultiByte(CodePage:UINT; dwFlags:DWORD; lpWideCharStr:PWideChar; cchWideChar:longint; lpMultiByteStr:PChar;cchMultiByte:longint; lpDefaultChar:PChar; lpUsedDefaultChar:pointer):longint;
  470. stdcall; external 'kernel32' name 'WideCharToMultiByte';
  471. function CharUpperBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD;
  472. stdcall; external 'user32' name 'CharUpperBuffW';
  473. function CharLowerBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD;
  474. stdcall; external 'user32' name 'CharLowerBuffW';
  475. procedure Win32Unicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  476. var
  477. destlen: SizeInt;
  478. begin
  479. // retrieve length including trailing #0
  480. // not anymore, because this must also be usable for single characters
  481. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  482. // this will null-terminate
  483. setlength(dest, destlen);
  484. if destlen>0 then
  485. begin
  486. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  487. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  488. end;
  489. end;
  490. procedure Win32Ansi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:UnicodeString;len:SizeInt);
  491. var
  492. destlen: SizeInt;
  493. dwflags: DWORD;
  494. begin
  495. // retrieve length including trailing #0
  496. // not anymore, because this must also be usable for single characters
  497. if cp=CP_UTF8 then
  498. dwFlags:=0
  499. else
  500. dwFlags:=MB_PRECOMPOSED;
  501. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  502. // this will null-terminate
  503. setlength(dest, destlen);
  504. if destlen>0 then
  505. begin
  506. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  507. PUnicodeRec(pointer(dest)-UnicodeFirstOff)^.CodePage:=CP_UTF16;
  508. end;
  509. end;
  510. function Win32UnicodeUpper(const s : UnicodeString) : UnicodeString;
  511. begin
  512. result:=s;
  513. UniqueString(result);
  514. if length(result)>0 then
  515. CharUpperBuff(LPWSTR(result),length(result));
  516. end;
  517. function Win32UnicodeLower(const s : UnicodeString) : UnicodeString;
  518. begin
  519. result:=s;
  520. UniqueString(result);
  521. if length(result)>0 then
  522. CharLowerBuff(LPWSTR(result),length(result));
  523. end;
  524. {******************************************************************************
  525. Widestring
  526. ******************************************************************************}
  527. procedure Win32Wide2AnsiMove(source:pwidechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  528. var
  529. destlen: SizeInt;
  530. begin
  531. // retrieve length including trailing #0
  532. // not anymore, because this must also be usable for single characters
  533. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  534. // this will null-terminate
  535. setlength(dest, destlen);
  536. if destlen>0 then
  537. begin
  538. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  539. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  540. end;
  541. end;
  542. procedure Win32Ansi2WideMove(source:pchar;cp : TSystemCodePage;var dest:widestring;len:SizeInt);
  543. var
  544. destlen: SizeInt;
  545. dwFlags: DWORD;
  546. begin
  547. // retrieve length including trailing #0
  548. // not anymore, because this must also be usable for single characters
  549. if cp=CP_UTF8 then
  550. dwFlags:=0
  551. else
  552. dwFlags:=MB_PRECOMPOSED;
  553. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  554. // this will null-terminate
  555. setlength(dest, destlen);
  556. if destlen>0 then
  557. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  558. end;
  559. function Win32WideUpper(const s : WideString) : WideString;
  560. begin
  561. result:=s;
  562. if length(result)>0 then
  563. CharUpperBuff(LPWSTR(result),length(result));
  564. end;
  565. function Win32WideLower(const s : WideString) : WideString;
  566. begin
  567. result:=s;
  568. if length(result)>0 then
  569. CharLowerBuff(LPWSTR(result),length(result));
  570. end;
  571. type
  572. PWStrInitEntry = ^TWStrInitEntry;
  573. TWStrInitEntry = record
  574. addr: PPointer;
  575. data: Pointer;
  576. end;
  577. PWStrInitTablesTable = ^TWStrInitTablesTable;
  578. TWStrInitTablesTable = packed record
  579. count : longint;
  580. tables : packed array [1..32767] of PWStrInitEntry;
  581. end;
  582. {$if not(defined(VER2_2) or defined(VER2_4))}
  583. var
  584. WStrInitTablesTable: TWStrInitTablesTable; external name 'FPC_WIDEINITTABLES';
  585. {$endif}
  586. function GetACP:UINT; external 'kernel32' name 'GetACP';
  587. { there is a similiar procedure in sysutils which inits the fields which
  588. are only relevant for the sysutils units }
  589. procedure InitWin32Widestrings;
  590. var
  591. i: longint;
  592. ptable: PWStrInitEntry;
  593. begin
  594. {$if not(defined(VER2_2) or defined(VER2_4))}
  595. { assign initial values to global Widestring typed consts }
  596. for i:=1 to WStrInitTablesTable.count do
  597. begin
  598. ptable:=WStrInitTablesTable.tables[i];
  599. while Assigned(ptable^.addr) do
  600. begin
  601. fpc_widestr_assign(ptable^.addr^, ptable^.data);
  602. Inc(ptable);
  603. end;
  604. end;
  605. {$endif}
  606. { Note: since WideChar=UnicodeChar and PWideChar=PUnicodeChar,
  607. Wide2AnsiMoveProc is identical to Unicode2AnsiStrMoveProc. }
  608. { Widestring }
  609. widestringmanager.Wide2AnsiMoveProc:=@Win32Unicode2AnsiMove;
  610. widestringmanager.Ansi2WideMoveProc:=@Win32Ansi2WideMove;
  611. widestringmanager.UpperWideStringProc:=@Win32WideUpper;
  612. widestringmanager.LowerWideStringProc:=@Win32WideLower;
  613. {$ifndef VER2_2}
  614. { Unicode }
  615. widestringmanager.Unicode2AnsiMoveProc:=@Win32Unicode2AnsiMove;
  616. widestringmanager.Ansi2UnicodeMoveProc:=@Win32Ansi2UnicodeMove;
  617. widestringmanager.UpperUnicodeStringProc:=@Win32UnicodeUpper;
  618. widestringmanager.LowerUnicodeStringProc:=@Win32UnicodeLower;
  619. {$endif VER2_2}
  620. DefaultSystemCodePage:=GetACP;
  621. DefaultUnicodeCodePage:=CP_UTF16;
  622. end;