安装程序启动时只运行一次批处理文件

时间:2014-04-21 22:22:43

标签: inno-setup

我在这里遇到以下问题:

function InitializeSetup(): Boolean;
var
  ResultCode:Integer;
begin
  Result := true;
  if MsgBox('Wanna help?',mbConfirmation, MB_YESNO )= IDYES then
  begin
    CreateBatch();
    Exec('cmd.exe', '/c "' +ExpandConstant('{tmp}\batch.bat'),'',SW_HIDE, ewWaitUntilTerminated, ResultCode);
    Result:= false;
  end;  
end;

在批处理文件中我得到以下内容:

@ECHO OFF 
D:
cd D:\_INSTALLER\Output
"installer.exe" /SAVEINF="opt.txt"

所以它基本上重新打开安装程序,一遍又一遍......(无限循环)

有没有办法问:Wanna help?只是第一次?如果用户单击是,则应执行批处理,否则如果用户单击否,则应正常继续安装。

预先感谢您的支持, 开始。

2 个答案:

答案 0 :(得分:1)

在TLama和his post的帮助下,我通过使用以下脚本实现了我的目标:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

type
  HINSTANCE = THandle;
procedure ExitProcess(exitCode:integer);external 'ExitProcess@kernel32.dll stdcall';
function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;lpParameters: string; lpDirectory: string; nShowCmd: Integer): HINSTANCE;
  external 'ShellExecute{#AW}@shell32.dll stdcall';

var
  withINF: Boolean;

function CmdLineParamExists(const Value: string): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 1 to ParamCount do
    if CompareText(ParamStr(I), Value) = 0 then
    begin
      Result := True;
      Break;
    end;
end;

//Initialize setup
function InitializeSetup(): Boolean;
var
  ResultCode:Integer;
  Params: string;
  RetVal: HINSTANCE;
begin
  Result := true;
  withINF := CmdLineParamExists('/SAVEINF=opt.txt');
  if not withINF then
  begin
    Params := '/SAVEINF=opt.txt';
    ShellExecute(0, 'open',ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
    ExitProcess(0);
  end;
end;

L.E:TLama也做了同样的事情的更短代码(添加多语言支持):

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
ShowLanguageDialog = yes

[Languages]
Name: "en"; MessagesFile: "compiler:Default.isl"
Name: "nl"; MessagesFile: "compiler:Languages\Dutch.isl"

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

type
  HINSTANCE = THandle;
function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;lpParameters: string; lpDirectory: string; nShowCmd: Integer): HINSTANCE;
  external 'ShellExecute{#AW}@shell32.dll stdcall';


function CmdLineParamExists(const Value: string): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 1 to ParamCount do
    if CompareText(ParamStr(I), Value) = 0 then
    begin
      Result := True;
      Break;
    end;
end;


function InitializeSetup: Boolean;
var
  Params: string;
begin
  // prepare Params variable for reusing
  if ExpandConstant('{language}') = 'en' then begin
    Params := '/SAVEINF=opt.txt /LANG=en';
  end;
  if ExpandConstant('{language}') = 'nl' then begin
    Params := '/SAVEINF=opt.txt /LANG=nl';
  end;
  // allow this setup to run if the expected parameter is specified; or, if it is not, allow to run it
  // when ShellExecute fails; it works like this - first evaluates the CmdLineParamExists function and
  // if that returns True, the second part of the statement (ShellExecute) won't run (evaluate); when
  // the parameter is not found, the CmdLineParamExists returns False and statement evaluation goes on,
  // ShellExecute attempts to run the setup and to the Result returns True (allow this instance to run)
  // when the function fails for some reason (the returned value <= 32); in other words, you will allow
  // this setup instance to run if executing of the new setup instance fails
  Result := CmdLineParamExists('/SAVEINF=opt.txt') or (ShellExecute(0, '', ExpandConstant('{srcexe}'), Params, '', SW_SHOW) <= 32);
end;

此致 BEGIN

答案 1 :(得分:0)

我认为没有理由,但你可以通过创建临时文件来解决这个问题。

如果用户点击是(第一次),则在调用 CreateBatch()之前创建一些文件(任何包含一些随机内容的文件)。

然后只需检查是否存在此文件 - 如果存在,则用户已单击是(一次)并执行您想要的操作。