How to add a scheduled task on network connection/disconnection event with Inno Setup

前端 未结 1 1513
心在旅途
心在旅途 2021-01-16 11:10

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 in

相关标签:
1条回答
  • 2021-01-16 12:00

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

    0 讨论(0)
提交回复
热议问题