Rev 73 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
73 | daniel-mar | 1 | unit todo_more_identifications; |
2 | |||
3 | interface |
||
4 | |||
5 | function IsConnected: boolean; |
||
6 | function GetHostname: string; |
||
7 | function GetComputerName: String; |
||
8 | function GetUserName: String; |
||
9 | function GetSystemWinDir: string; |
||
10 | function GetSystemDrive: AnsiChar; |
||
11 | function GetOSVersion: string; |
||
12 | function GetRegisteredOrganisation: string; |
||
13 | function GetRegisteredOwner: string; |
||
14 | function LaufwerkBereit(root: string): boolean; |
||
15 | function GetMyDocuments: string; |
||
16 | function GetLocalAppData: string; |
||
17 | function GetWindowsDirectory: string; |
||
18 | // function GetWifiSSID: string; |
||
19 | function GetTempDirectory: String; |
||
20 | |||
21 | implementation |
||
22 | |||
23 | uses |
||
24 | Windows, SysUtils, Registry, wininet, shlobj; |
||
25 | |||
26 | type |
||
27 | EAPICallError = Exception; |
||
28 | |||
29 | function IsConnected: boolean; |
||
30 | {$IF defined(ANDROID)} |
||
31 | begin |
||
32 | result := IsConnectedAndroid; |
||
33 | end; |
||
34 | {$ELSEIF defined(MACOS)} |
||
35 | //var |
||
36 | //IPW: TIdHTTP; |
||
37 | begin |
||
38 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
39 | result := false; // TODO: im zweifelsfall lieber true? |
||
40 | |||
41 | // head verzögert den Programmfluss um 1-2 Sekunden... |
||
42 | // Ip-Watch würde auch eine LAN-Adresse zeigen |
||
43 | //TIdHTTP.Head('http://registration.rinntech.com'); |
||
44 | //response.code=200 -> true |
||
45 | end; |
||
46 | {$ELSEIF defined(MSWINDOWS)} |
||
47 | var |
||
48 | origin: Cardinal; |
||
49 | begin |
||
50 | result := InternetGetConnectedState(@origin, 0); |
||
51 | end; |
||
52 | {$ELSE} |
||
53 | begin |
||
54 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
55 | result := false; |
||
56 | end; |
||
57 | {$IFEND} |
||
58 | |||
59 | var CacheHostname: string; |
||
60 | {$IFDEF MSWindows} |
||
61 | function GetHostname: string; |
||
62 | var |
||
63 | reg: TRegistry; |
||
64 | begin |
||
65 | if CacheHostname <> '' then |
||
66 | begin |
||
67 | result := CacheHostname; |
||
68 | Exit; |
||
69 | end; |
||
70 | result := ''; |
||
71 | reg := TRegistry.Create; |
||
72 | try |
||
73 | reg.RootKey := HKEY_LOCAL_MACHINE; |
||
74 | if reg.OpenKeyReadOnly |
||
75 | ('\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters') then |
||
76 | begin |
||
77 | result := reg.ReadString('Hostname'); |
||
78 | reg.CloseKey; |
||
79 | end; |
||
80 | finally |
||
81 | reg.Free; |
||
82 | end; |
||
83 | CacheHostname := result; |
||
84 | end; |
||
85 | {$ELSE} |
||
86 | function GetHostname: string; |
||
87 | {$IFDEF MACOS} |
||
88 | var |
||
89 | buff: array [0 .. 255] of AnsiChar; |
||
90 | {$ENDIF} |
||
91 | begin |
||
92 | if CacheHostname <> '' then |
||
93 | begin |
||
94 | result := CacheHostname; |
||
95 | Exit; |
||
96 | end; |
||
97 | {$IFDEF MACOS} |
||
98 | Posix.Unistd.gethostname(buff,sizeof(buff)); |
||
99 | SetString(result, buff, AnsiStrings.strlen(buff)); |
||
100 | CacheHostname := result; |
||
101 | {$ELSE} |
||
102 | {$IFDEF ANDROID} |
||
103 | result := ''; |
||
104 | {$ELSE} |
||
105 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
106 | {$ENDIF} |
||
107 | {$ENDIF} |
||
108 | end; |
||
109 | {$ENDIF} |
||
110 | |||
111 | {$IFDEF MSWindows} |
||
112 | function GetComputerName: String; |
||
113 | var |
||
114 | buffer: array [0 .. MAX_PATH] of Char; |
||
115 | Size: dWord; |
||
116 | begin |
||
117 | Size := SizeOf(buffer); |
||
118 | Windows.GetComputerName(buffer, Size); |
||
119 | SetString(result, buffer, lstrlen(buffer)); |
||
120 | end; |
||
121 | {$ELSE} |
||
122 | function GetComputerName: String; |
||
123 | {$IFDEF MACOS} |
||
124 | var |
||
125 | Pool: NSAutoreleasePool; |
||
126 | h : NSHost; |
||
127 | {$ENDIF} |
||
128 | begin |
||
129 | {$IFDEF MACOS} |
||
130 | NSDefaultRunLoopMode; |
||
131 | Pool := TNSAutoreleasePool.Create; |
||
132 | try |
||
133 | h := TNSHost.Wrap(TNSHost.OCClass.currentHost); |
||
134 | result := Format('%s',[h.localizedName.UTF8String]); |
||
135 | finally |
||
136 | Pool.drain; |
||
137 | end; |
||
138 | {$ELSE} |
||
139 | {$IFDEF ANDROID} |
||
140 | //TODO: anderer/richtiger name ... AccountManager for email adress, Telephony mngr etc. |
||
141 | result := JStringToString(TJBuild.JavaClass.SERIAL); |
||
142 | {$ELSE} |
||
143 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
144 | result := ''; |
||
145 | {$ENDIF} |
||
146 | {$ENDIF} |
||
147 | end; |
||
148 | {$ENDIF} |
||
149 | |||
150 | {$IFDEF MACOS} |
||
151 | function NSUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name _PU +'NSUserName'; |
||
152 | function NSFullUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name _PU + 'NSFullUserName'; |
||
153 | {$ENDIF} |
||
154 | |||
155 | {$IFDEF MSWindows} |
||
156 | function GetUserName: String; |
||
157 | var |
||
158 | buffer: array [0 .. MAX_PATH] of Char; |
||
159 | Size: dWord; |
||
160 | begin |
||
161 | Size := SizeOf(buffer); |
||
162 | |||
163 | if Windows.GetUserName(Buffer, Size) then |
||
164 | begin |
||
165 | // SetString(result, buffer, lstrlen(buffer)); |
||
166 | Result := StrPas(Buffer); |
||
167 | end |
||
168 | else |
||
169 | begin |
||
170 | Result := ''; |
||
171 | end; |
||
172 | end; |
||
173 | {$ELSE} |
||
174 | function GetUserName: String; |
||
175 | begin |
||
176 | {$IFDEF MACOS} |
||
177 | result := Format('%s',[TNSString.Wrap(NSUserName).UTF8String]); |
||
178 | {$ELSE} |
||
179 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
180 | result := ''; |
||
181 | {$ENDIF} |
||
182 | end; |
||
183 | {$ENDIF} |
||
184 | |||
185 | |||
186 | |||
187 | |||
188 | |||
189 | |||
190 | |||
191 | {$IFDEF MSWindows} |
||
192 | function GetSystemWinDir: string; |
||
193 | var |
||
194 | h: HModule; |
||
195 | {$IFDEF UNICODE} |
||
196 | f: function(lpBuffer: LPWSTR; uSize: UINT): UINT; stdcall; |
||
197 | {$ELSE} |
||
198 | f: function(lpBuffer: LPSTR; uSize: UINT): UINT; stdcall; |
||
199 | {$ENDIF} |
||
200 | res: string; |
||
201 | cnt: UINT; |
||
202 | begin |
||
203 | h := LoadLibrary(kernel32); |
||
204 | if h = 0 then RaiseLastOSError; |
||
205 | |||
206 | {$IFDEF UNICODE} |
||
207 | @f := GetProcAddress(h, 'GetSystemWindowsDirectoryW'); |
||
208 | {$ELSE} |
||
209 | @f := GetProcAddress(h, 'GetSystemWindowsDirectoryA'); |
||
210 | {$ENDIF} |
||
211 | |||
212 | SetLength(res, MAX_PATH); |
||
213 | if @f = nil then // Assigned? |
||
214 | begin |
||
215 | // We are probably on Win9x where GetSystemWindowsDirectory* does not exist. |
||
216 | cnt := Windows.GetWindowsDirectory(PChar(res), MAX_PATH); |
||
217 | end |
||
218 | else |
||
219 | begin |
||
220 | // We are on a modern system where GetSystemWindowsDirectory* does exist. |
||
221 | // http://objectmix.com/delphi/402836-getting-hard-drive-letter-windows-runs.html |
||
222 | // Im Gegensatz zu GetWindowsDirectory zeigt GetSystemWindowsDirectory bei |
||
223 | // Terminalservern das System-Windows-Verzeichnis und nicht das "private" |
||
224 | // Windows-Verzeichnis des Users. |
||
225 | cnt := f(PChar(res), MAX_PATH); |
||
226 | end; |
||
227 | |||
228 | if cnt <= 0 then RaiseLastOSError; |
||
229 | result := res; |
||
230 | end; |
||
231 | {$ELSE} |
||
232 | function GetSystemWinDir: string; |
||
233 | begin |
||
234 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
235 | result := ''; |
||
236 | end; |
||
237 | {$ENDIF} |
||
238 | |||
239 | function GetSystemDrive: AnsiChar; |
||
240 | var |
||
241 | res: string; |
||
242 | begin |
||
243 | res := ExtractFileDrive(GetSystemWinDir); |
||
244 | Assert(Length(res) >= 1); |
||
245 | result := AnsiChar(res[1]); |
||
246 | end; |
||
247 | |||
248 | function GetOSVersion: string; |
||
249 | {$IF Declared(TOSVersion)} |
||
250 | begin |
||
251 | result := TOSVersion.ToString; |
||
252 | {$ELSE} |
||
253 | var |
||
254 | VersionInfo: TOSVersionInfo; |
||
255 | begin |
||
256 | VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo); |
||
257 | GetVersionEx(VersionInfo); |
||
258 | result := IntToStr(VersionInfo.dwPlatformId) + '-' + |
||
259 | IntToStr(VersionInfo.dwMajorVersion) + '.' + |
||
260 | IntToStr(VersionInfo.dwMinorVersion) + '-' + |
||
261 | IntToStr(VersionInfo.dwBuildNumber) |
||
262 | {$IFEND} |
||
263 | end; |
||
264 | |||
265 | {$IFDEF MSWindows} |
||
266 | function GetRegisteredOrganisation: string; |
||
267 | var |
||
268 | reg: TRegistry; |
||
269 | k: string; |
||
270 | VersionInfo: TOSVersionInfo; |
||
271 | begin |
||
272 | result := ''; |
||
273 | reg := TRegistry.Create; |
||
274 | try |
||
275 | reg.rootkey := HKEY_LOCAL_MACHINE; |
||
276 | |||
277 | VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo); |
||
278 | GetVersionEx(VersionInfo); |
||
279 | |||
280 | if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then |
||
281 | begin |
||
282 | k := '\Software\Microsoft\Windows NT\CurrentVersion'; |
||
283 | end |
||
284 | else |
||
285 | begin |
||
286 | k := '\Software\Microsoft\Windows\CurrentVersion'; |
||
287 | end; |
||
288 | if reg.OpenKeyReadOnly(k) then |
||
289 | begin |
||
290 | result := reg.ReadString('RegisteredOrganization'); |
||
291 | reg.CloseKey; |
||
292 | end; |
||
293 | finally |
||
294 | reg.Free; |
||
295 | end; |
||
296 | end; |
||
297 | {$ELSE} |
||
298 | function GetRegisteredOrganisation: string; |
||
299 | begin |
||
300 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
301 | result := ''; |
||
302 | end; |
||
303 | {$ENDIF} |
||
304 | |||
305 | {$IFDEF MSWindows} |
||
306 | function GetRegisteredOwner: string; |
||
307 | var |
||
308 | reg: TRegistry; |
||
309 | k: string; |
||
310 | VersionInfo: TOSVersionInfo; |
||
311 | begin |
||
312 | result := ''; |
||
313 | reg := TRegistry.Create; |
||
314 | try |
||
315 | reg.rootkey := HKEY_LOCAL_MACHINE; |
||
316 | |||
317 | VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo); |
||
318 | GetVersionEx(VersionInfo); |
||
319 | |||
320 | if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then |
||
321 | begin |
||
322 | k := '\Software\Microsoft\Windows NT\CurrentVersion'; |
||
323 | end |
||
324 | else |
||
325 | begin |
||
326 | k := '\Software\Microsoft\Windows\CurrentVersion'; |
||
327 | end; |
||
328 | if reg.OpenKeyReadOnly(k) then |
||
329 | begin |
||
330 | result := reg.ReadString('RegisteredOwner'); |
||
331 | reg.CloseKey; |
||
332 | end; |
||
333 | finally |
||
334 | reg.Free; |
||
335 | end; |
||
336 | end; |
||
337 | {$ELSE} |
||
338 | function GetRegisteredOwner: string; |
||
339 | begin |
||
340 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
341 | result := ''; |
||
342 | end; |
||
343 | {$ENDIF} |
||
344 | |||
345 | {$IFDEF MSWindows} |
||
346 | function LaufwerkBereit(root: string): boolean; |
||
347 | var |
||
348 | Oem: cardinal; |
||
349 | Dw1, Dw2: DWORD; |
||
350 | begin |
||
351 | // http://www.delphi-treff.de/tipps/system/hardware/feststellen-ob-ein-laufwerk-bereit-ist/ |
||
352 | Oem := SetErrorMode(SEM_FAILCRITICALERRORS); |
||
353 | result := GetVolumeInformation(PCHAR(Root), nil, 0, nil, Dw1, Dw2, nil, 0); |
||
354 | SetErrorMode(Oem) ; |
||
355 | end; |
||
356 | {$ELSE} |
||
357 | function LaufwerkBereit(root: string): boolean; |
||
358 | begin |
||
359 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
360 | result := false; |
||
361 | end; |
||
362 | {$ENDIF} |
||
363 | |||
364 | {$IFDEF MSWindows} |
||
365 | function GetMyDocuments: string; |
||
366 | var |
||
367 | r: Bool; |
||
368 | path: array[0..Max_Path] of Char; |
||
369 | begin |
||
370 | // TODO: Stattdessen ShGetFolderPath verwenden? |
||
371 | r := ShGetSpecialFolderPath(0, path, CSIDL_Personal, False); |
||
372 | if not r then |
||
373 | raise EAPICallError.Create('Could not find MyDocuments folder location.'); |
||
374 | Result := Path; |
||
375 | end; |
||
376 | {$ELSE} |
||
377 | function GetMyDocuments: string; |
||
378 | begin |
||
379 | result := TPath.GetDocumentsPath; |
||
380 | end; |
||
381 | {$ENDIF} |
||
382 | |||
383 | {$IF not Defined(CSIDL_LOCAL_APPDATA)} |
||
384 | const |
||
385 | CSIDL_LOCAL_APPDATA = $001c; |
||
386 | {$IFEND} |
||
387 | |||
388 | {$IFDEF MSWindows} |
||
389 | function GetLocalAppData: string; |
||
390 | var |
||
391 | r: Bool; |
||
392 | path: array[0..Max_Path] of Char; |
||
393 | begin |
||
394 | // TODO: Stattdessen ShGetFolderPath verwenden? |
||
395 | r := ShGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, False); |
||
396 | if not r then |
||
397 | raise EAPICallError.Create('Could not find LocalAppData folder location.'); |
||
398 | Result := Path; |
||
399 | end; |
||
400 | {$ELSE} |
||
401 | function GetLocalAppData: string; |
||
402 | begin |
||
403 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
404 | result := ''; |
||
405 | end; |
||
406 | {$ENDIF} |
||
407 | |||
408 | {$IFDEF MSWindows} |
||
409 | function GetWindowsDirectory: string; |
||
410 | var |
||
411 | WinDir: PChar; |
||
412 | begin |
||
413 | WinDir := StrAlloc(MAX_PATH); |
||
414 | try |
||
415 | Windows.GetWindowsDirectory(WinDir, MAX_PATH); |
||
416 | result := string(WinDir); |
||
417 | finally |
||
418 | StrDispose(WinDir); |
||
419 | end; |
||
420 | end; |
||
421 | {$ELSE} |
||
422 | function GetWindowsDirectory: string; |
||
423 | begin |
||
424 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
425 | result := ''; |
||
426 | end; |
||
427 | {$ENDIF} |
||
428 | |||
429 | {$IFDEF MSWindows} |
||
430 | function GetTempDirectory: String; |
||
431 | var |
||
432 | tempFolder: array [0 .. MAX_PATH] of Char; |
||
433 | begin |
||
434 | GetTempPath(MAX_PATH, @tempFolder); |
||
435 | result := StrPas(tempFolder); |
||
436 | end; |
||
437 | {$ELSE} |
||
438 | function GetTempDirectory: String; |
||
439 | begin |
||
440 | {$MESSAGE Warn 'Nicht implementiert für dieses OS'} |
||
441 | result := ''; |
||
442 | end; |
||
443 | {$ENDIF} |
||
444 | |||
445 | end. |