如何使用Inno Setup在网络连接/断开连接事件上添加计划任务

如何使用Inno Setup在网络连接/断开连接事件上添加计划任务

问题描述:

我想使用Inno Setup进行程序安装,并且需要Inno Setup在Windows Task Scheduler中创建一个任务,以在每次Internet连接时启动我的program.exe.

I want to use Inno Setup for my program installation and I need Inno Setup to create a task in Windows Task Scheduler to launch my program.exe every time the internet connects.

我可以做到这一点,但是我希望Inno安装程序通过来执行此操作Schtasks命令. 这是我的inno设置代码(此处):

I can do this manually, but I want Inno Setup do this by Schtasks command. This is my inno setup code(here):

#define MyAppName "Desktop"
#define MyAppVersion "2"
#define MyAppPublisher "MH"
#define MyAppExeName "Desktop.exe"

[Setup]
AppId={{EFBBA2D3-C6F0-4D3D-BBD5-5AF126C3E8E9}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup 2
Compression=lzma
SolidCompression=yes

PrivilegesRequired=admin

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "E:\IdeaProjects\Desktop\Desktop.exe"; DestDir: "{app}"; Flags: ignoreversion

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent 

Filename: "schtasks.exe"; \
    Parameters: "/Create /TN MyTask /XML ""{tmp}\Task.xml"""; \
    StatusMsg: "Scheduling task..."; BeforeInstall: CreateTaskXml 
    ;Flags: runhidden;

[Code]
procedure CreateTaskXml;
var
  TaskXml: string;
begin
  TaskXml :=
    '<?xml version="1.0"?>' + #13#10 +
    '<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'
      + #13#10 +
    '  <Triggers>' + #13#10 +
    { The EventTrigger here }
    '    <EventTrigger>' +
    '      <Enabled>true</Enabled>' +
    '      <Subscription><QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[Provider[@Name='Microsoft-Windows-NetworkProfile'] and EventID=10000]]</Select></Query></QueryList></Subscription>' +
    '    </EventTrigger>' +
    '  </Triggers>' + #13#10 +
    { ... }
    '  <Actions Context="Author">' + #13#10 +
    '    <Exec>' + #13#10 +
    '      <Command>' + ExpandConstant('{app}\{#MyAppExeName}') + '</Command>' + #13#10 +
    '    </Exec>' + #13#10 +
    '  </Actions>' + #13#10 +
    '</Task>' + #13#10;

  if SaveStringToFile(ExpandConstant('{tmp}\Task.xml'), TaskXml, False) then
  begin
    Log('Task XML successfully created');
  end
    else
  begin
    Log('Failed to create task XML');
  end;
end;

你能帮我吗?谢谢.

与其他任何晦涩的计划设置一样,您必须使用任务的XML定义.

As with any other obscure scheduling settings, you have to use XML definition of the task.

最简单的方法是在Windows Task Scheduler GUI中配置任务,选择创建的任务,然后选择 Export 命令.

The easiest is to configure the task in Windows Task Scheduler GUI, select the created task and choose Export command.

它将创建任务的XML定义,其中将包含以下内容:

It will create an XML definition of the task, which will contain something like:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <!-- ... -->
  <Triggers>
    <EventTrigger>
      <Enabled>true</Enabled>
      <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"&gt;&lt;Select Path="Microsoft-Windows-NetworkProfile/Operational"&gt;*[System[Provider[@Name='NetworkProfile'] and EventID=10000]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
    </EventTrigger>
  </Triggers>
  <!-- ... -->
</Task>

编辑XML以删除所有特定于您计算机的内容.

Edit the XML to remove everything, what is specific to your computer.

然后将导出的XML用作文件模板,该文件将在安装程序中动态创建,并带有指向已安装应用程序的实际路径:

And then use the exported XML as a template for a file, that you will create on the fly in the installer with an actual path to your installed application:

[Run]
Filename: "schtasks.exe"; \
    Parameters: "/Create /TN MyTask /XML ""{tmp}\Task.xml"""; \
    StatusMsg: "Scheduling task..."; Flags: runhidden; BeforeInstall: CreateTaskXml

[Code]

procedure CreateTaskXml;
var
  TaskXml: string;
begin
  TaskXml :=
    '<?xml version="1.0"?>' + #13#10 +
    '<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'
      + #13#10 +
    '  <Triggers>' + #13#10 +
    { The EventTrigger here }
    '  </Triggers>' + #13#10 +
    { ... }
    '  <Actions Context="Author">' + #13#10 +
    '    <Exec>' + #13#10 +
    '      <Command>' + ExpandConstant('{app}\MyProg.exe') + '</Command>' + #13#10 +
    '    </Exec>' + #13#10 +
    '  </Actions>' + #13#10 +
    '</Task>' + #13#10;

  if SaveStringToFile(ExpandConstant('{tmp}\Task.xml'), TaskXml, False) then
  begin
    Log('Task XML successfully created');
  end
    else
  begin
    Log('Failed to create task XML');
  end;
end;


CreateTaskXml中的 SaveStringToFile函数创建Ansi编码的XML.如果需要Unicode,则必须以UTF-16编码创建XML(schtasks不支持UTF-8).


The SaveStringToFile function in CreateTaskXml creates the XML in Ansi encoding. If you need Unicode, you have to create the XML in UTF-16 encoding (schtasks does not support UTF-8).