/[projects]/dao/DelphiScanner/Utils.pas
ViewVC logotype

Diff of /dao/DelphiScanner/Utils.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2682 by torben, Wed Aug 26 19:52:38 2015 UTC revision 2996 by torben, Fri Apr 8 14:01:34 2016 UTC
# Line 3  unit Utils; Line 3  unit Utils;
3  interface  interface
4  uses  uses
5    Controls,    Controls,
6    Graphics //TColor    Graphics, //TColor
7      IniFiles,
8      Windows //TFileTime
9    ;    ;
10    
11  type  type
# Line 20  type Line 22  type
22      class function TColorToHex(Color : TColor) : string;      class function TColorToHex(Color : TColor) : string;
23      class function HexToTColor(sColor : string) : TColor;      class function HexToTColor(sColor : string) : TColor;
24    
25        class function AdobeReaderExists(): Boolean;
26    
27        class function Sto_GetFmtFileVersion(const FileName: String = ''): String;
28    
29        class function CheckUrl(url:string): boolean;
30    
31        class function FileTime2DateTime(FileTime: TFileTime): TDateTime;
32    
33        class function GetComputerNameHelper() : String;
34    
35        class function GetCurrentUserName : string;
36    
37        class function URLEncode(const s : string) : string;
38    
39    
40    end;    end;
# Line 29  implementation Line 43  implementation
43    
44  uses StrUtils,  uses StrUtils,
45    Types, //TRect ,    Types, //TRect ,
   Windows,  
46    Messages,    Messages,
47    SysUtils //IntToStr etc    SysUtils, //IntToStr etc
48      Registry,
49      wininet,   //CheckUrl
50    
51      HTTPApp //HTTPEncode
52    
53    
54    ;    ;
55    
56  {  {
# Line 161  begin Line 180  begin
180  end;  end;
181    
182    
183    class function TUtils.AdobeReaderExists(): Boolean;
184    var
185      AReg: TRegistry;
186    begin
187      result:= false;
188      AReg := TRegistry.Create;
189      AReg.RootKey := HKEY_LOCAL_MACHINE;
190      if AReg.KeyExists('\SOFTWARE\Adobe\Acrobat Reader') then
191        result:= True;
192      AReg.Free;
193    end;
194    
195    
196    /// <summary>
197    ///   This function reads the file resource of "FileName" and returns
198    ///   the version number as formatted text.</summary>
199    /// <example>
200    ///   Sto_GetFmtFileVersion() = '4.13.128.0'
201    ///   Sto_GetFmtFileVersion('', '%.2d-%.2d-%.2d') = '04-13-128'
202    /// </example>
203    /// <remarks>If "Fmt" is invalid, the function may raise an
204    ///   EConvertError exception.</remarks>
205    /// <param name="FileName">Full path to exe or dll. If an empty
206    ///   string is passed, the function uses the filename of the
207    ///   running exe or dll.</param>
208    /// <param name="Fmt">Format string, you can use at most four integer
209    ///   values.</param>
210    /// <returns>Formatted version number of file, '' if no version
211    ///   resource found.</returns>
212    class function TUtils.Sto_GetFmtFileVersion(const FileName: String = ''): String;
213    var
214      sFileName: String;
215      iBufferSize: DWORD;
216      iDummy: DWORD;
217      pBuffer: Pointer;
218      pFileInfo: Pointer;
219      iVer: array[1..4] of Integer;
220    begin
221      // set default value
222      Result := '';
223      // get filename of exe/dll if no filename is specified
224      sFileName := Trim(FileName);
225      if (sFileName = '') then
226        sFileName := GetModuleName(HInstance);
227      // get size of version info (0 if no version info exists)
228      iBufferSize := GetFileVersionInfoSize(PChar(sFileName), iDummy);
229      if (iBufferSize > 0) then
230      begin
231        GetMem(pBuffer, iBufferSize);
232        try
233        // get fixed file info (language independent)
234        GetFileVersionInfo(PChar(sFileName), 0, iBufferSize, pBuffer);
235        VerQueryValue(pBuffer, '\', pFileInfo, iDummy);
236        // read version blocks
237        iVer[1] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
238        iVer[2] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
239        iVer[3] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
240        iVer[4] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
241        finally
242          FreeMem(pBuffer);
243        end;
244    
245        // format result string
246        Result := Format('%d.%d.%d.%d', [iVer[1], iVer[2], iVer[3], iVer[4]]);
247    
248      end;
249    end;
250    
251    
252    class function TUtils.CheckUrl(url:string):boolean;
253    var
254      hSession, hfile: hInternet;
255      dwindex,dwcodelen :dword;
256      dwcode:array[1..20] of char;
257      res : pchar;
258    begin
259      if pos('http://',lowercase(url))=0 then
260        url := 'http://'+url;
261      Result := false;
262      hSession := InternetOpen('InetURL:/1.0',
263                                INTERNET_OPEN_TYPE_PRECONFIG,
264                                nil,
265                                nil,
266                                0);
267      if assigned(hsession) then
268      begin
269        hfile := InternetOpenUrl(hsession,
270                                 pchar(url),
271                                 nil,
272                                 0,
273                                 INTERNET_FLAG_RELOAD,
274                                 0);
275        dwIndex := 0;
276        dwCodeLen := 10;
277        HttpQueryInfo(hfile,
278                      HTTP_QUERY_STATUS_CODE,
279                      @dwcode,
280                      dwcodeLen,
281                      dwIndex);
282        res := pchar(@dwcode);
283        result:= (res ='200') or (res ='302');
284        if assigned(hfile) then
285          InternetCloseHandle(hfile);
286        InternetCloseHandle(hsession);
287      end;
288    
289    end;
290    
291    
292    class function TUtils.FileTime2DateTime(FileTime: TFileTime): TDateTime;
293    var
294       LocalFileTime: TFileTime;
295       SystemTime: TSystemTime;
296    begin
297       FileTimeToLocalFileTime(FileTime, LocalFileTime) ;
298       FileTimeToSystemTime(LocalFileTime, SystemTime) ;
299       Result := SystemTimeToDateTime(SystemTime) ;
300    end;
301    
302    
303    class function TUtils.GetComputerNameHelper : String;
304    var
305      buffer: array[0..255] of char;
306      size: dword;
307    begin
308      size := 256;
309      if GetComputerName(buffer, size) then
310        Result := buffer
311      else
312        Result := ''
313    end;
314    
315    class function TUtils.GetCurrentUserName : string;
316    const
317      cnMaxUserNameLen = 254;
318    var
319      sUserName     : string;
320      dwUserNameLen : DWord;
321    begin
322      dwUserNameLen := cnMaxUserNameLen-1;
323      SetLength( sUserName, cnMaxUserNameLen );
324      GetUserName(PChar( sUserName ),dwUserNameLen );
325      SetLength( sUserName, dwUserNameLen );
326      Result := sUserName;
327    end;
328    
329    
330    class function TUtils.URLEncode(const s : string) : string;
331    begin
332      result := HTTPEncode(s);
333    end;
334    
335    
336  end.  end.

Legend:
Removed from v.2682  
changed lines
  Added in v.2996

  ViewVC Help
Powered by ViewVC 1.1.20