1 | unit SunriseChatCoreUtils;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, SysUtils, WinSock;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TArrayOfString = array of string;
|
---|
10 |
|
---|
11 | function LocalHostName: string;
|
---|
12 | function GetUserName: string;
|
---|
13 | function Explode(Separator: Char; Data: string): TArrayOfString;
|
---|
14 | function SecondsIdle: Cardinal;
|
---|
15 | function GetWindowsVersionStr: string;
|
---|
16 |
|
---|
17 | implementation
|
---|
18 |
|
---|
19 | var
|
---|
20 | GetLastInputInfo2: function(var plii: TLastInputInfo): BOOL; stdcall;
|
---|
21 | LibHandle: HInst; //dll handle
|
---|
22 |
|
---|
23 | function GetWindowsVersionStr: string;
|
---|
24 | begin
|
---|
25 | case Win32Platform of
|
---|
26 | 1: case Win32MajorVersion of
|
---|
27 | 4: case Win32MinorVersion of
|
---|
28 | 0: Result:= '95';
|
---|
29 | 10: Result:= '98';
|
---|
30 | 90: Result:= 'Me';
|
---|
31 | end;
|
---|
32 | end;
|
---|
33 | 2: case Win32MajorVersion of
|
---|
34 | 4: case Win32MinorVersion of
|
---|
35 | 0: Result:= 'NT 4.0';
|
---|
36 | end;
|
---|
37 | 5: case Win32MinorVersion of
|
---|
38 | 0: Result:= '2000';
|
---|
39 | 1: Result:= 'XP';
|
---|
40 | 2: Result:= 'Server 2003';
|
---|
41 | end;
|
---|
42 | 6: case Win32MinorVersion of
|
---|
43 | 0: Result := 'Vista';
|
---|
44 | end
|
---|
45 | end;
|
---|
46 | end;
|
---|
47 | Result:= 'Windows ' + Result;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | function GetUserName: string;
|
---|
51 | const
|
---|
52 | MaxUsernameLength = 256;
|
---|
53 | var
|
---|
54 | L : LongWord;
|
---|
55 | begin
|
---|
56 | L := MaxUsernameLength + 2;
|
---|
57 | SetLength(Result, L);
|
---|
58 | if Windows.GetUserName(PChar(Result), L) and (L > 0) then
|
---|
59 | SetLength(Result, StrLen(PChar(Result))) else Result := '';
|
---|
60 | end;
|
---|
61 |
|
---|
62 | function Explode(Separator: Char; Data: string): TArrayOfString;
|
---|
63 | begin
|
---|
64 | SetLength(Result, 0);
|
---|
65 | while Pos(Separator, Data) > 0 do begin
|
---|
66 | SetLength(Result, Length(Result) + 1);
|
---|
67 | Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
|
---|
68 | Delete(Data, 1, Pos(Separator, Data));
|
---|
69 | end;
|
---|
70 | SetLength(Result, Length(Result) + 1);
|
---|
71 | Result[High(Result)] := Data;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | function SecondsIdle: Cardinal;
|
---|
75 | var
|
---|
76 | liInfo: TLastInputInfo;
|
---|
77 | begin
|
---|
78 | // Try to load indirectly function GetLastInputInfo for compatibility with Win9x
|
---|
79 | LibHandle := LoadLibrary('user32.dll');
|
---|
80 | if LibHandle = 0 then begin
|
---|
81 | GetLastInputInfo2 := nil;
|
---|
82 | end else begin
|
---|
83 | @GetLastInputInfo2 := GetProcAddress(LibHandle,'GetLastInputInfo');
|
---|
84 | if (@GetLastInputInfo2 = nil) then FreeLibrary(LibHandle);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | // Only WinNT+
|
---|
88 | if Assigned(GetLastInputInfo2) then begin
|
---|
89 | liInfo.cbSize := SizeOf(TLastInputInfo) ;
|
---|
90 | GetLastInputInfo2(liInfo) ;
|
---|
91 | Result := (GetTickCount - liInfo.dwTime) DIV 1000;
|
---|
92 | end else Result:= 0;
|
---|
93 | // MainWindow.RichEdit1.Lines.Add(IntToStr(Result));
|
---|
94 | // SysUtils.Beep;
|
---|
95 | // Result:= IdleTrackerGetLastTickCount div 1000;
|
---|
96 | // Button1.Caption:= IntToStr(Result);
|
---|
97 | // Result:= 0;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | function LocalHostName: String;
|
---|
101 | var
|
---|
102 | Buf: array[0..255] of Char;
|
---|
103 | begin
|
---|
104 | // if gethostname(@Buf, Sizeof(Buf)) <> 0 then
|
---|
105 | // raise Exception.Create('LocalHostName not available');
|
---|
106 | // Result := PChar(@Buf);
|
---|
107 | Result := '';
|
---|
108 | end;
|
---|
109 |
|
---|
110 | end.
|
---|