Nov 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
program GenerateVersionFile;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

type
  TFileVersionInfo = record
    fCompanyName,
    fFileDescription,
    fFileVersion,
    fInternalName,
    fLegalCopyRight,
    fLegalTradeMark,
    fOriginalFileName,
    fProductName,
    fProductVersion,
    fComments         : string;
    fMajor,
    fMinor,
    fRelease,
    fBuild            : Word;
  end;

{$R *.res}

procedure GetAppVersionInfo(sAppNamePath: string; var aFileVersionInfo: TFileVersionInfo);
var
  iVerSize     : Integer;
  pcVerBuf     : PChar;
  pVerBufValue : Pointer;
  {$IFDEF Delphi3Below}
  iVerHandle   : Integer;
  iVerBufLen   : Integer;
  {$ELSE}
  iVerHandle   : Cardinal;
  iVerBufLen   : Cardinal;
  {$ENDIF}
  sVerKey      : string;

  function GetInfo(ThisKey: string): string;
  begin
    Result  := '';
    sVerKey := '\StringFileInfo\' + IntToHex(loword(integer(pVerBufValue^)), 4) +
                  IntToHex(hiword(integer(pVerBufValue^)), 4) + '\' + ThisKey;
   
    if VerQueryValue(pcVerBuf, PChar(sVerKey), pVerBufValue, iVerBufLen) then
      Result := StrPas(PChar(pVerBufValue));
     
  end;

  function QueryValue(ThisValue: string): string;
  begin
    Result := '';
   
    if GetFileVersionInfo(PChar(sAppNamePath), iVerHandle, iVerSize, pcVerBuf) and
        VerQueryValue(pcVerBuf, '\VarFileInfo\Translation', pVerBufValue, iVerBufLen) then
      Result := GetInfo(ThisValue);
     
  end;

  function GetFileVersion(const FileName: TFileName): boolean;
  // Returns True on success and False on failure.
  var
    lwSize, lwLen : LongWord;
    hFile         : THandle;
    pcBuffer      : PChar;
    pInfo         : ^VS_FIXEDFILEINFO;
  begin
    Result := False;
    lwSize := GetFileVersionInfoSize(Pointer(FileName), hFile);
    if lwSize > 0 then
    begin
      GetMem(pcBuffer, lwSize);
      if GetFileVersionInfo(Pointer(FileName), 0, lwSize, pcBuffer) then
        if VerQueryValue(pcBuffer, '\', pointer(pInfo), lwLen) then
        begin
          aFileVersionInfo.fMajor   := HiWord(pInfo.dwFileVersionMS);
          aFileVersionInfo.fMinor   := LoWord(pInfo.dwFileVersionMS);
          aFileVersionInfo.fRelease := HiWord(pInfo.dwFileVersionLS);
          aFileVersionInfo.fBuild   := LoWord(pInfo.dwFileVersionLS);
          Result  := True;
        end;
      FreeMem(pcBuffer);
    end;
  end;

begin
  if sAppNamePath = '' then
    Exit;

  GetFileVersion(sAppNamePath);
  iVerSize := GetFileVersionInfoSize(PChar(sAppNamePath), iVerHandle);
  pcVerBuf := AllocMem(iVerSize);
  try
    with aFileVersionInfo do
    begin
      fCompanyName      := QueryValue('CompanyName');
      fFileDescription  := QueryValue('FileDescription');
      fFileVersion      := QueryValue('FileVersion');
      fInternalName     := QueryValue('InternalName');
      fLegalCopyRight   := QueryValue('LegalCopyRight');
      fLegalTradeMark   := QueryValue('LegalTradeMark');
      fOriginalFileName := QueryValue('OriginalFileName');
      fProductName      := QueryValue('ProductName');
      fProductVersion   := QueryValue('ProductVersion');
      fComments         := QueryValue('Comments');
    end;
  finally
    FreeMem(pcVerBuf, iVerSize);
  end;
end;

var
  sSource, sTarget : TFileName;
  fvi              : TFileVersionInfo;
  tfTarget         : TextFile;
begin
  case ParamCount of
    2:
    begin
      sSource := ParamStr(1);
      sTarget := ParamStr(2);
      if (FileExists(sSource)) then
      begin
        if (DirectoryExists(ExtractFilePath(sTarget))) then
        begin
          GetAppVersionInfo(sSource, fvi);
          if (fvi.fFileVersion <> '') then
          begin
            AssignFile(tfTarget, sTarget);
            Rewrite(tfTarget);
            Write(tfTarget, fvi.fFileVersion);
            Flush(tfTarget);
            Close(tfTarget);
            Writeln('');
            Writeln('''' + ExtractFileName(sTarget) + ''' updated with value: ' + fvi.fFileVersion);
          end
          else
          begin
            Writeln('File Version is emmpty!');
          end;
        end
        else
        begin
          Writeln('Folder ' + ExtractFilePath(sTarget) + ' doesn''t exist!');
        end;
      end
      else
      begin
        Writeln(sSource + ' doesn''t exist!');
      end;
    end;
    else
    begin
      Writeln('This program expects 2 parameters!');
    end;
  end;

end.
Share

Pages : 1 2

Lien permanent vers Récupération de la version d’un fichier Rédigé par Whiler \\ Tags : , , , ,

Laisser une réponse

(requis)

(requis)

*

;) (lol) (y) |-( (hi) 8-) (angel) :s (clap) (bow) (tmi) (:| plus »

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.