卸载时需要从NSIS安装程序中删除计划任务中的任务

时间:2015-03-02 16:57:24

标签: scheduled-tasks nsis

我使用NSIS创建了一个安装程序。现在我面临一个问题,比如我的应用程序可以创建一个计划任务,但是当我卸载它时,它不会删除/删除我从我的应用程序创建的任务,它们仍然可以在Schedule Tasks中看到。我每次都安排一个自动运行任务。

如何在卸载时从Schedule Tasks中删除自动运行任务?

1 个答案:

答案 0 :(得分:1)

最好的选择可能只是让你的应用程序这样做; ExecWait '"$instdir\myapp.exe" /uninstalltask'

您还可以使用nsExec plugin执行at.exe / schtasks.exe。

直接调用任务调度程序API很复杂,需要处理两个版本:

!include LogicLib.nsh
;V1:
!define CLSID_CTaskScheduler {148BD52A-A2AB-11CE-B11F-00AA00530503}
!define IID_ITaskScheduler {148BD527-A2AB-11CE-B11F-00AA00530503}
;V2:
!define CLSID_TaskScheduler {0F87369F-A4E5-4CFC-BD3E-73E6154572DD}
!define IID_ITaskService {2FABA4C7-4DA9-4013-9697-20CC3FD40F85}

RequestExecutionLevel Admin

!define MyTaskFolder "\"
!define MyTaskName "Test"
Section
System::Call "ole32::CoCreateInstance(g '${CLSID_TaskScheduler}', i 0, i 5, g '${IID_ITaskService}', *i.r1)i.r0"
${If} $0 = 0
    !define VARIANTBYVALPARAM_EMPTY "i0xcccc0000,i0xcccccccc,i0xcccccccc,i0xcccccccc"
    System::Call "$1->10(${VARIANTBYVALPARAM_EMPTY},${VARIANTBYVALPARAM_EMPTY},${VARIANTBYVALPARAM_EMPTY},${VARIANTBYVALPARAM_EMPTY})i.r0"
    ${If} $0 = 0
        System::Call '$1->7(w "${MyTaskFolder}", *i.r2)i.r0' ; ITaskService::GetFolder
        ${If} $0 = 0
            System::Call '$2->15(w "${MyTaskName}", i0)i.r0' ; ITaskFolder::DeleteTask
            System::Call 'kernel32::FormatMessage(i 0x00001000|0x00000100|0x00000200,i0,i$0,i0,*i.r9,i0,i0)'
            System::Call '*$9(&t${NSIS_MAX_STRLEN}.r3)'
            System::Call 'kernel32::LocalFree(i$9)'
            DetailPrint "ITaskFolder::DeleteTask HRESULT=$0 ($3)"
            System::Call '$2->2()' ; ITaskFolder::Release
        ${EndIf}
    ${EndIf}
    System::Call '$1->2()' ; ITaskService::Release
${EndIf}
${IfThen} $0 = 0 ${|} Goto done ${|}
System::Call "ole32::CoCreateInstance(g '${CLSID_CTaskScheduler}', i 0, i 5, g '${IID_ITaskScheduler}', *i.r1)i.r0"
${If} $0 = 0
    System::Call '$1->7(w "${MyTaskName}")i.r0' ; ITaskScheduler::Delete
    System::Call 'kernel32::FormatMessage(i 0x00001000|0x00000100|0x00000200,i0,i$0,i0,*i.r9,i0,i0)'
    System::Call '*$9(&t${NSIS_MAX_STRLEN}.r3)'
    System::Call 'kernel32::LocalFree(i$9)'
    DetailPrint " ITaskScheduler::Delete HRESULT=$0 ($3)"
    System::Call '$1->2()' ; ITaskScheduler::Release
${EndIf}
done:
SectionEnd