Inno Setup function not called

2019-09-05 02:16发布

问题:

I would like the [InstallDelete] section to call a custom function that will check if an older version is installed (in which case certain files need to be deleted prior installation of the new version).

Extract from my Inno Setup script. First the function that returns True if an older version is installed.

[Code]
function deleteExistingHhd: Boolean;
var Version: String;
begin
  MsgBox('Checking for key:'+'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('AppId')+'_is1', mbInformation, MB_OK);

  if RegValueExists(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('AppId')+'_is1', 'DisplayVersion') then
    begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('AppId')+'_is1', 'DisplayVersion', Version);
      MsgBox('Existing version:'+Version+'  New version:'+ExpandConstant('AppVersion'), mbInformation, MB_OK);
      if (Version < '1.013') then
        begin
          Result := True;
        end
      else
        begin
          Result := False;
        end
    end
  else
    begin
      Result := False;
    end
end;

Then the section that should call this function:

[InstallDelete]
Type: files; Name: {userappdata}\xxx\*.hhd; Check:deleteExistingHhd;

Unfortunately the generated setup seems to never call the custom function (when installing my program with this setup I never get the MsgBox located in the custom function and the files are not deleted).

Is it possible that my function has some errors that are not indicated when Inno Setup compiles? If so, where could I find them?

Any help / hint would be very much appreciated; thanks!

回答1:

If the MsgBox is never called, there is something else wrong.
I created a new project, pasted your lines as-is and the first msgbox pops-up.

Maybe just start a new, and keep adding parts from your old setup script until you'll find what is stopping the function from executing.

Did you know you can step through the code using F7 and/or breakpoints F5? That should help you locate the problem, It should go: [InstallDelete] -> [Dirs] -> [Files]

@IZB is right, ExpandConstant('AppId') will be resolved as AppId and not the actual ID. Check the output from the debugging section I added the your script below. Watch the "Debug output" on bottom of Inno Seput Compiler while stepping through the code.

AND, You also need the ExpandConstant :) because otherwise you'll get the leading '{' doubled. It should be doubled in [Setup] section to escape the bracket character. The pre-compiler will pass the escape leading bracket too, using #SetupSetting("AppId") . ExpandConstant will not actually Expand any constant here, but will remove this doubled bractet.

Here is paste of complete working iss file:

#define MyAppName "My Program"
#define MyAppVersion "1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "MyProg.exe"

[Setup]
AppId={{CB77C990-DECF-4697-B377-8F76799CC6B7}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Code]
function deleteExistingHhd: Boolean;
var Version: String;
begin

  // Debugging
  // {#SetupSetting("AppId")} is short from {#emit SetupSetting("AppId")}
  Log('Note the double bracket: {#SetupSetting("AppId")}');
  Log('Now it''s fine: ' + ExpandConstant('{# SetupSetting("AppId")}'));
  Log(' This won''t expand: ' + ExpandConstant('AppId'));

  if RegValueExists(HKEY_LOCAL_MACHINE,ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}_is1'), 'DisplayVersion') then
    begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE,ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}_is1'), 'DisplayVersion', Version);
      MsgBox('Existing version:' + Version + '  New version:{#SetupSetting("AppVersion")}', mbInformation, MB_OK);
      if (Version < '1.013') then Result := True
      else Result := False;
    end
  else Result := False;
end;

[InstallDelete]
Type: files; Name: {userappdata}\xxx\*.hhd; Check: deleteExistingHhd


回答2:

If you want to unistall it before install it with new one you can use this code:

#define AppName  SetupSetting('AppName')     
#define AppVersion  SetupSetting('AppVersion')
#define AppId  SetupSetting('AppId')             
#if AppId == ""   
  #define AppId AppName
#endif 

[Code]
procedure InitializeWizard;
var
  Uninstall,Version: String;
  ResultCode:Integer;
begin
  MsgBox('Checking for key:'+'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', mbInformation, MB_OK);
  if RegValueExists(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', 'DisplayVersion') then begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', 'DisplayVersion', Version);
      MsgBox('Existing version:'+Version+'  New version:'+ExpandConstant('{#AppVersion}'), mbInformation, MB_OK);
      if (Version < ExpandConstant('{#AppVersion}')) then begin               
          RegQueryStringValue(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1','UninstallString', Uninstall);
          Exec(RemoveQuotes(Uninstall), ' /RECAll /SILENT' , '', SW_SHOW, ewWaitUntilTerminated, ResultCode);   
        end
    end
end;

I think I'll keep code above and for you problem may this will help:

#define AppName  SetupSetting('AppName')     
#define AppVersion  SetupSetting('AppVersion')
#define AppId  SetupSetting('AppId')             
#if AppId == ""   
  #define AppId AppName
#endif 

[InstallDelete]
Type: files; Name: {userappdata}\xxx\*.hhd; Check:deleteExistingHhd;

[Code]
function deleteExistingHhd: Boolean;
var Version: String;
begin
  MsgBox('Checking for key:'+'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', mbInformation, MB_OK);                                                                                                                                                                                                                                               
  if RegValueExists(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', 'DisplayVersion') then begin
    RegQueryStringValue(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+ExpandConstant('{#AppId}')+'_is1', 'DisplayVersion', Version);
    MsgBox('Existing version:'+Version+'  New version:'+ExpandConstant('{#AppVersion}'), mbInformation, MB_OK);
    if (Version < '1.013') then begin
        Result := True;
    end else Result := False;
  end else Result := False;
end;