Debian Codegraph @

src/base/windows/castleutils_os_specific_windows.inc

raw
1 {%MainUnit ../castleutils.pas}
2 {
3 Copyright 2000-2021 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Some WinAPI helpers }
18
19 {$ifdef read_interface}
20
21 { Get exe name through WinAPI GetModuleFileNameW.
22
23 @exclude This is internal.
24
25 Using ParamStr(0) on Windows is buggy in case the directory name contains local characters
26 that cannot be expressed in current Windows locale.
27 E.g. if parent directory name contains Chinese or Russian characters on Polish Windows.
28 It seems it's because FPC uses GetModuleFileNameA for this.
29
30 Using GetModuleFileNameW instead works cool.
31
32 TODO: submit to FPC,
33 https://trello.com/c/7Xfek9e4/15-submit-to-fpc-testcase-need-to-use-getmodulefilenamew-instead-of-getmodulefilenamea }
34 function ExeNameFromGetModule: String;
35
36 {$endif read_interface}
37
38 {$ifdef read_implementation}
39
40 procedure InitializationOSSpecific;
41 begin
42 end;
43
44 procedure FinalizationOSSpecific;
45 begin
46 end;
47
48 function ExeNameFromGetModule: String;
49 var
50 S: UnicodeString;
51 begin
52 SetLength(S, MAX_PATH);
53 if GetModuleFileNameW(0, PWideChar(@S[1]), MAX_PATH) = 0 then
54 begin
55 // WritelnWarning('GetModuleFileNameW failed. We will use old method to determine ExeName, which will fail if parent directory contains local characters');
56 Exit(ParamStr(0)); // fallback to old method
57 end;
58 SetLength(S, StrLen(PWideChar(S))); // It's only null-terminated after WinAPI call, set actual length for Pascal UnicodeString
59 Result := UTF8Encode(S);
60 end;
61
62 {$endif read_implementation}