1

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.

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;

Can you help me? Thanks.

mh m
  • 75
  • 8

1 Answers1

1

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

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

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>

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

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;

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).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I don't know can I ask this problem here: I want to delete the created task during uninstall. It shows a confirmation window (Y/N). Is it possible to skip it by “Y” reply? – mh m May 11 '17 at 03:50
  • What confirmation window? Anyway, did you try `/F`? See `schtasks /delete /?`. If you have further questions, post them separately. – Martin Prikryl May 11 '17 at 06:56