Link to home
Start Free TrialLog in
Avatar of sbondalapati
sbondalapatiFlag for India

asked on

Version.dll problem in delphi 2010 application

HI,

  I developed a delphi application on Delphi 2010 and windows xp 32bit version. It uses the Version.dll to get the version of another exe. When i try to run the same application in Windows 7 64 bit version it is throwing an error some thing like problem with Version.dll. Please tell me how to resolve this?
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

I don't know about Version.dll, but here is Delphi code to get all file versions you might want

It first declares a type TVersion : version of a file as a record of 4 integer values
Var Version:TVersion;

If you want the TVersion of a file, use
Version:=GetAppVersion('MyApp.Exe');

if you want to convert a TVersion into a string, you can use
Caption:=VersionStr(Version);

GetModuleVersion will do that in one step. if you give it a filename as parameter, it will give you the string version of it. If you don't, or use an empty string parameter (default value), it will return the version of the current module. a module is either the Exe, or the dll, bpl etc .
So easiest way to get the current app version as a string is :
Caption:=GetModuleVersion;

This module file name is get by calling the windows API GetModuleFileName, using the instance number of the module. In all Delphi module, this is a global variable called hInstance .
IF you add this code in a dll or bpl and use it from another application or module, beware that GetModuleVersion will return the version of the dll/bpl where lies this code, not the calling module. To do so, you have to use GetInstanceVersion with hInstance (which will refer to the calling instance)
Caption:=GetInstanceVersion(hInstance);

Type
 TVersion=Record Major,Minor,Release,Build:Integer; end;

Function VersionStr(V:TVersion):String;
begin
 With V do Result:=Format('%d.%d.%d.%d',[Major,Minor,Release,Build]);
 While RightStr(Result,2)='.0' do Result:=LeftStr(Result,Length(Result)-2);
end;

Function GetAppVersion(ExecFile:String=''):TVersion;
var
 Size,Size2: DWord;
 Pt, Pt2: Pointer;
begin
 if ExecFile='' Then ExecFile:=ParamStr(0);
 Size := GetFileVersionInfoSize(PChar (ExecFile), Size2);
 FillChar(Result,Sizeof(Result),0);
 if Size > 0 then
  begin
   GetMem (Pt, Size);
   try
    GetFileVersionInfo (PChar (ExecFile), 0, Size, Pt);
    VerQueryValue (Pt, '\', Pt2, Size);
    with TVSFixedFileInfo (Pt2^),Result do
     begin
      Major:=HiWord (dwFileVersionMS);
      Minor:=LoWord (dwFileVersionMS);
      Release:=HiWord (dwFileVersionLS);
      Build:=LoWord (dwFileVersionLS);
     end;
   finally
     FreeMem (Pt);
   end;
 end;
end;

function GetModuleName(instance:LongWord=0): string;
var
  szFileName: array[0..MAX_PATH] of Char;
begin
  FillChar(szFileName, SizeOf(szFileName), #0);
  if instance=0 Then instance:=hInstance;
  GetModuleFileName(instance, szFileName, MAX_PATH);
  Result := szFileName;
end;

function GetModuleVersion(ModuleName:String=''):String;
begin
 if ModuleName='' Then ModuleName:=GetModuleName;
 Result:=VersionStr(GetAppVersion(ModuleName));
end;

function GetInstanceVersion(Instance:LongWord=0):String;
begin
 Result:=VersionStr(GetAppVersion(GetModuleName(instance)));
end;

Open in new window

Avatar of sbondalapati

ASKER

will this code works with both 32bit os and 64bit os.
GetFileVersionInfo (PChar (ExecFile), 0, Size, Pt);
    VerQueryValue (Pt, '\', Pt2, Size);

both the above functions will use version.dll which is platform dependent. if you have any other solution which is platform independent it will be useful.
really ? it works for me on Vista64 as well as XP 32
I guess the problem is not 32 or 64 bits, but Windows 7

You are right on one point, it is API of version.dll, I never figured nor cared because it's all transparent in Delphi (it's defined in Windows.pas)

I'll check on MSDN if they say something about this & Seven
can you specify which error do you get ?
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
find the image
Sans-titre.png
that is a pointer problem. I guess it's a an incompatibility with unicode and/or code compiled with Delphi 2010.
If not done already, can you check with the second version I provided ?

can you trace the exact function call that is throwing this error ?
Thanks. Your second version worked perfectly on 2 different types of windows.