[RESOLU] Taches planifiés : Ajouter un trigger

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
Progi1984
Messages : 2659
Inscription : mar. 14/déc./2004 13:56
Localisation : France > Rennes
Contact :

[RESOLU] Taches planifiés : Ajouter un trigger

Message par Progi1984 »

Voilà ce petit code devrait ajouter une tache planifié se nommant Test : ca pas de problème. Puis ajouter un trigger (Onglet Planification dans la tache planifié) de type "Toutes les semaines" sauf que cela ne marche pas et ajoute un trigger de type "Tous les jours".

Merci de votre aide !

Doc MSDN :
- Taches planifiés : http://msdn2.microsoft.com/en-us/library/aa380706.aspx
- CreateTrigger : http://msdn2.microsoft.com/en-us/library/aa381218.aspx
- SetTrigger : http://msdn2.microsoft.com/en-us/library/aa381867.aspx

Code : Tout sélectionner

...voir ci dessous...
Dernière modification par Progi1984 le jeu. 29/mars/2007 15:24, modifié 2 fois.
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

ouai bah pareil.

j'ai même recodé l'exemple MSDN et on a le meme probleme (la creation du trigger renvoi un code d'erreur).

http://msdn2.microsoft.com/en-us/library/aa446828.aspx

peut etre que ca vient de la déclaration de la structure TASK_TRIGGER

il faudrait voir à quoi correspond l'erreur 0x80070057 !

Code : Tout sélectionner

...voir ci dessous...
Dernière modification par Flype le jeu. 29/mars/2007 15:11, modifié 1 fois.
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

ah bah si l'erreur en question est Hex(#E_INVALIDARG)

donc il y a bien un probleme avec la structure TASK_TRIGGER mais quoi ? le StructureUnion peut etre...

[EDIT]
Une solution pourrait etre de créér à la main la tache désirée.

ensuite de la lire avec la méthode GetTrigger, lire la propriété cbTriggerSize et voir si on a bien la meme taille que notre SizeOf(TASK_TRIGGER)
Avatar de l’utilisateur
Progi1984
Messages : 2659
Inscription : mar. 14/déc./2004 13:56
Localisation : France > Rennes
Contact :

Message par Progi1984 »

Exact : problème de structure !

La taille doit être de 48 octets, elle n'en fait que 46 mais je ne vois pas où j'ai fait mon erreur si ce n'est le trigger type !

Code : Tout sélectionner

;-
;- http://msdn2.microsoft.com/en-us/library/aa446828.aspx
;-

#CLSCTX_INPROC_SERVER = 1
#COR_E_FILENOTFOUND = $80070002
#TASK_TRIGGER_FLAG_HAS_END_DATE = $1

Enumeration 0 ; TriggerType
  #TASK_TIME_TRIGGER_ONCE            = 0
  #TASK_TIME_TRIGGER_DAILY           = 1
  #TASK_TIME_TRIGGER_WEEKLY          = 2
  #TASK_TIME_TRIGGER_MONTHLYDATE     = 3
  #TASK_TIME_TRIGGER_MONTHLYDOW      = 4
  #TASK_EVENT_TRIGGER_ON_IDLE        = 5
  #TASK_EVENT_TRIGGER_AT_SYSTEMSTART = 6
  #TASK_EVENT_TRIGGER_AT_LOGON       = 7
EndEnumeration

Interface IEnumWorkItems     Extends IUnknown
Next(a.l, b.l, c.l)
Skip(a.l)
Reset()
Clone(a.l)
EndInterface
Interface IProvideTaskPage   Extends IUnknown
  GetPage(tpType.l, fPersistChanges.l, phPage.l)
EndInterface
Interface IScheduledWorkItem Extends IUnknown ; http://msdn2.microsoft.com/en-us/library/aa381216.aspx
  CreateTrigger(piNewTrigger.l, ppTrigger.l)                   ;Creates a trigger using a work item object.
  DeleteTrigger(iTrigger.l)                                    ;Deletes a trigger from a work item.
  GetTriggerCount(plCount.l)                                   ;Retrieves the number of triggers associated With a work item.
  GetTrigger(iTrigger.l, ppTrigger.l)                          ;Retrieves a trigger Structure.
  GetTriggerString(iTrigger.l, ppwszTrigger.l)                 ;Retrieves a trigger string.
  GetRunTimes(pstBegin.l, pstEnd.l, pCount.l, rgstTaskTimes.l) ;Retrieves the work item run times For a specified time period.
  GetNextRunTime(pstNextRun.l)                                 ;Retrieves the Next time the work item will run.
  SetIdleWait(wIdleMinutes.l, wDeadlineMinutes.l)              ;Sets the idle wait time For the work item.
  GetIdleWait(pwIdleMinutes.l, pwDeadlineMinutes.l)            ;Retrieves the idle wait time For the work item.
  Run()                                                        ;Runs the work item.
  Terminate()                                                  ;Ends the execution of the work item.
  EditWorkItem(hParent.l, dwReserved.l)                        ;Opens the configuration properties For the work item.
  GetMostRecentRunTime(pstLastRun.l)                           ;Retrieves the most recent time the work item began running.
  GetStatus(phrStatus.l)                                       ;Retrieves the status of the work item.
  GetExitCode(pdwExitCode.l)                                   ;Retrieves the work item's last exit code.
  SetComment(pwszComment.l)                                    ;Sets the comment For the work item.
  GetComment(ppwszComment.l)                                   ;Retrieves the comment For the work item.
  SetCreator(pwszCreator.l)                                    ;Sets the creator of the work item.
  GetCreator(ppwszCreator.l)                                   ;Retrieves the creator of the work item.
  SetWorkItemData(cBytes.l, rgbData.l)                         ;Stores application-defined Data associated With the work item.
  GetWorkItemData(pcBytes.l, ppBytes.l)                        ;Retrieves application-defined Data associated With the work item.
  SetErrorRetryCount(wRetryCount.l)                            ;Not currently implemented.
  GetErrorRetryCount(pwRetryCount.l)                           ;Not currently implemented.
  SetErrorRetryInterval(wRetryInterval.l)                      ;Not currently implemented.
  GetErrorRetryInterval(pwRetryInterval.l)                     ;Not currently implemented.
  SetFlags(dwFlags.l)                                          ;Sets the flags that modify the behavior of the work item.
  GetFlags(pdwFlags.l)                                         ;Retrieves the flags that modify the behavior of the work item.
  SetAccountInformation(pwszAccountName.l, pwszPassword.l)     ;Sets the account name And password For the work item.
  GetAccountInformation(ppwszAccountName.l)                    ;Retrieves the account name For the work item.
EndInterface
Interface ITaskScheduler     Extends IUnknown ; http://msdn2.microsoft.com/en-us/library/aa381811.aspx
  SetTargetComputer(a.l)
  GetTargetComputer(a.l)
  Enum(a.l)
  Activate(Name.p-bstr, b.l, c.l)
  Delete(a.l)
  NewWorkItem(Name.p-bstr, b.l, c.l, d.l)
  AddWorkItem(a.l, b.l)
  IsOfType(a.l, b.l)
EndInterface
Interface ITask              Extends IUnknown ; http://msdn2.microsoft.com/en-us/library/aa381311.aspx
  CreateTrigger(a.l, b.l)
  DeleteTrigger(a.l)
  GetTriggerCount(a.l)
  GetTrigger(a.l, b.l)
  GetTriggerString(a.l, b.l)
  GetRunTimes(a.l, b.l, c.l, d.l)
  GetNextRunTime(a.l)
  SetIdleWait(a.l, b.l)
  GetIdleWait(a.l, b.l)
  Run()
  Terminate()
  EditWorkItem(a.l, b.l)
  GetMostRecentRunTime(a.l)
  GetStatus(a.l)
  GetExitCode(a.l)
  SetComment(a.l)
  GetComment(a.l)
  SetCreator(a.l)
  GetCreator(a.l)
  SetWorkItemData(a.l, b.l)
  GetWorkItemData(a.l, b.l)
  SetErrorRetryCount(a.l)
  GetErrorRetryCount(a.l)
  SetErrorRetryInterval(a.l)
  GetErrorRetryInterval(a.l)
  SetFlags(a.l)
  GetFlags(a.l)
  SetAccountInformation(a.l, b.l)
  GetAccountInformation(a.l)
  SetApplicationName(a.l)
  GetApplicationName(a.l)
  SetParameters(a.l)
  GetParameters(a.l)
  SetWorkingDirectory(a.l)
  GetWorkingDirectory(a.l)
  SetPriority(a.l)
  GetPriority(a.l)
  SetTaskFlags(a.l)
  GetTaskFlags(a.l)
  SetMaxRunTime(a.l)
  GetMaxRunTime(a.l)
EndInterface
Interface ITaskTrigger       Extends IUnknown ; http://msdn2.microsoft.com/en-us/library/aa381864.aspx
  SetTrigger(pTrigger.l)           ;Sets the task trigger values.
  GetTrigger(pTrigger.l)           ;Retrieves the current task trigger.
  GetTriggerString(ppwszTrigger.l) ;Retrieves the current task trigger in the form of a string.
EndInterface

Structure DAILY
  DaysInterval.w
EndStructure
Structure WEEKLY
  WeeksInterval.w
  rgfDaysOfTheWeek.w
EndStructure
Structure MONTHLYDATE
  rgfDays.l
  rgfMonths.w
EndStructure
Structure MONTHLYDOW
  wWhichWeek.w
  rgfDaysOfTheWeek.w
  rgfMonths.w
EndStructure
Structure TRIGGER_TYPE_UNION
  StructureUnion
    Daily.DAILY
    Weekly.WEEKLY
    MonthlyDate.MONTHLYDATE
    MonthlyDOW.MONTHLYDOW
  EndStructureUnion
EndStructure
Structure TASK_TRIGGER
  cbTriggerSize.w
  Reserved1.w
  wBeginYear.w
  wBeginMonth.w
  wBeginDay.w
  wEndYear.w
  wEndMonth.w
  wEndDay.w
  wStartHour.w
  wStartMinute.w
  MinutesDuration.l
  MinutesInterval.l
  rgFlags.l
  TriggerType.l ; TASK_TRIGGER_TYPE = Byte (0->7)
  Type.TRIGGER_TYPE_UNION
  Reserved2.w
  wRandomMinutesInterval.w
EndStructure

Macro FAILED(hr)
  (hr <> #S_OK)
EndMacro
Macro SUCCEEDED(hr)
  (hr = #S_OK)
EndMacro

Procedure.l main()
 
  Protected hr.l
  Protected pITS.ITaskScheduler
  Protected pITask.ITask
  Protected pITaskTrigger.ITaskTrigger
  Protected piNewTrigger.w
  Protected pTrigger.TASK_TRIGGER
  Protected pIPersistFile.IPersistFile
 
  ;///////////////////////////////////////////////////////////////////
  ;// Call CoInitialize to initialize the COM library and then
  ;// Call CoCreateInstance to get the Task Scheduler object.
  ;///////////////////////////////////////////////////////////////////
 
  hr = CoInitialize_(#Null)
 
  If (SUCCEEDED(hr))
    hr = CoCreateInstance_(?CLSID_CTaskScheduler, #Null, #CLSCTX_INPROC_SERVER, ?IID_ITaskScheduler, @pITS)
    If (FAILED(hr))
      CoUninitialize_()
      ProcedureReturn 1
    EndIf
  Else
    ProcedureReturn 1
  EndIf
 
  ;///////////////////////////////////////////////////////////////////
  ;// Call ITaskScheduler::Activate to get the task object.
  ;///////////////////////////////////////////////////////////////////
 
  hr = pITS\Activate("Task Example", ?IID_ITask, @pITask)
 
  pITS\Release()
 
  If (FAILED(hr))
    Debug "Failed calling ITaskScheduler::Activate: error = 0x" + Hex(hr)
    CoUninitialize_()
    ProcedureReturn 1
  EndIf
 
  ;///////////////////////////////////////////////////////////////////
  ;// Call ITask::CreateTrigger to create new Trigger.
  ;///////////////////////////////////////////////////////////////////
 
  hr = pITask\CreateTrigger(@piNewTrigger, @pITaskTrigger)
 
  If (FAILED(hr))
    Debug "Failed calling ITask::CreateTrigger: error = 0x" + Hex(hr)
    pITask\Release()
    CoUninitialize_()
    ProcedureReturn 1
  EndIf
  
  Protected Trigger.ITaskTrigger
  Protected TaskTrigger.TASK_TRIGGER
    If pITask\GetTrigger(Num,@Trigger) = #S_OK
      TaskTrigger\cbTriggerSize = SizeOf(TASK_TRIGGER)
      Debug SizeOf(TASK_TRIGGER)
      If Trigger\GetTrigger(@TaskTrigger) = #S_OK
        Debug ""
        Debug "----- TRIGGER -----Start"
        Debug TaskTrigger\cbTriggerSize.w
        Debug TaskTrigger\Reserved1.w
        Debug TaskTrigger\wBeginYear.w
        Debug TaskTrigger\wBeginMonth.w
        Debug TaskTrigger\wBeginDay.w
        Debug TaskTrigger\wEndYear.w
        Debug TaskTrigger\wEndMonth.w
        Debug TaskTrigger\wEndDay.w
        Debug TaskTrigger\wStartHour.w
        Debug TaskTrigger\wStartMinute.w
        Debug TaskTrigger\MinutesDuration.l
        Debug TaskTrigger\MinutesInterval.l
        Debug TaskTrigger\rgFlags.l
        Debug TaskTrigger\TriggerType.l ; TASK_TRIGGER_TYPE = Byte (0->7)
        Select TaskTrigger\TriggerType.l  
          Case #TASK_TIME_TRIGGER_ONCE
            ; Type is ignored
          Case #TASK_TIME_TRIGGER_DAILY
            Debug TaskTrigger\Type\Daily\DaysInterval.w
          Case #TASK_TIME_TRIGGER_WEEKLY
            Debug TaskTrigger\Type\Weekly\WeeksInterval
            Debug TaskTrigger\Type\Weekly\rgfDaysOfTheWeek
          Case #TASK_TIME_TRIGGER_MONTHLYDATE
            Debug TaskTrigger\Type\MonthlyDate\rgfDays
            Debug TaskTrigger\Type\MonthlyDate\rgfMonths
          Case #TASK_TIME_TRIGGER_MONTHLYDOW
            Debug TaskTrigger\Type\MonthlyDOW\wWhichWeek
            Debug TaskTrigger\Type\MonthlyDOW\rgfDaysOfTheWeek
            Debug TaskTrigger\Type\MonthlyDOW\rgfMonths
          Case #TASK_EVENT_TRIGGER_ON_IDLE
            ; the wStartHour, wStartMinute, and Type member are ignored
          Case #TASK_EVENT_TRIGGER_AT_SYSTEMSTART
            ; Type is ignored
          Case #TASK_EVENT_TRIGGER_AT_LOGON
            ; Type is ignored
        EndSelect
        Debug TaskTrigger\Type.TRIGGER_TYPE_UNION
        Debug TaskTrigger\Reserved2.w
        Debug TaskTrigger\wRandomMinutesInterval.w
        Debug "----- TRIGGER -----End"
        Debug ""
      EndIf
    Else
      ProcedureReturn -1
    EndIf
  
  
  ;//////////////////////////////////////////////////////
  ;// Define TASK_TRIGGER Structure. Note that wBeginDay,
  ;// wBeginMonth, and wBeginYear must be set to a valid
  ;// day, month, and year respectively.
  ;//////////////////////////////////////////////////////
  
  ZeroMemory_(@pTrigger, SizeOf(TASK_TRIGGER))
  Debug SizeOf(TASK_TRIGGER)
  ;// Add code To set Trigger Structure ?
  pTrigger\wBeginDay               = 1     ; // Required
  pTrigger\wBeginMonth             = 1     ; // Required
  pTrigger\wBeginYear              = 1999  ; // Required
  pTrigger\cbTriggerSize           = SizeOf(TASK_TRIGGER)
  pTrigger\wStartHour              = 13
  pTrigger\TriggerType             = #TASK_TIME_TRIGGER_DAILY
  pTrigger\Type\Daily\DaysInterval = 1
 
  ;///////////////////////////////////////////////////////////////////
  ;// Call ITaskTrigger::SetTrigger To set Trigger criteria.
  ;///////////////////////////////////////////////////////////////////
 
  hr = pITaskTrigger\SetTrigger(@pTrigger)
 
  If (FAILED(hr))
    Debug "Failed calling ITaskTrigger::SetTrigger: error = 0x" + Hex(hr)
    pITask\Release()
    pITaskTrigger\Release()
    CoUninitialize_()
    ProcedureReturn 1
  EndIf
 
  ;///////////////////////////////////////////////////////////////////
  ;// Call IPersistFile::Save to save Trigger to disk.
  ;///////////////////////////////////////////////////////////////////
 
  hr = pITask\QueryInterface(?IID_IPersistFile, @pIPersistFile)
  hr = pIPersistFile\Save(#Null, #True)
 
  If (FAILED(hr))
    Debug "Failed calling IPersistFile::Save: error = 0x" + Hex(hr)
    pITask\Release()
    pITaskTrigger\Release()
    pIPersistFile\Release()
    CoUninitialize_()
    ProcedureReturn 1
  EndIf
 
  Debug "The trigger was created and IPersistFile::Save was"
  Debug "called to save the new trigger to disk."
 
  ;///////////////////////////////////////////////////////////////////
  ;// Release resources.
  ;///////////////////////////////////////////////////////////////////
 
  pITask\Release()
  pITaskTrigger\Release()
  pIPersistFile\Release()
  CoUninitialize_()
 
  ProcedureReturn 0
 
EndProcedure

Debug main()

DataSection ;{
CLSID_CTask :         ; {148BD520-A2AB-11CE-B11F-00AA00530503}
Data.l $148BD520
Data.w $A2AB, $11CE
Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03
CLSID_CTaskScheduler: ; {148BD52A-A2AB-11CE-B11F-00AA00530503}
Data.l $148BD52A
Data.w $A2AB, $11CE
Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03
IID_IPersistFile:     ; {0000010b-0000-0000-C000-000000000046}
Data.l $0000010B
Data.w $0000,$0000
Data.b $C0,$00,$00,$00,$00,$00,$00,$46
IID_ITask:            ; {148BD524-A2AB-11CE-B11F-00AA00530503}
Data.l $148BD524
Data.w $A2AB, $11CE
Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03   
IID_ITaskScheduler:   ; {148BD527-A2AB-11CE-B11F-00AA00530503}
Data.l $148BD527
Data.w $A2AB, $11CE
Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03
EndDataSection ;} 
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

ok,

à priori ca pourrait etre juste un petit problème d'alignement mémoire.

donc si on écrit la structure comme çà - çà devrait fonctionner.
j'ai pas eu le temps de tester mais c'est un problème classique.

Code : Tout sélectionner

Structure TASK_TRIGGER
  cbTriggerSize.w
  Reserved1.w
  wBeginYear.w
  wBeginMonth.w
  wBeginDay.w
  wEndYear.w
  wEndMonth.w
  wEndDay.w
  wStartHour.w
  wStartMinute.w
  MinutesDuration.l
  MinutesInterval.l
  rgFlags.l
  TriggerType.l 
  Type.TRIGGER_TYPE_UNION
  Reserved2.w
  wRandomMinutesInterval.w
  wAlignment.w 
EndStructure
cf: voir le dernier champs 'wAlignment.w'
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

après test, c'était presque çà.

le problème d'alignement était dans les autres petites structures :

Code : Tout sélectionner

Structure DAILY
  DaysInterval.w
  wAlignment.w 
EndStructure
Structure WEEKLY
  WeeksInterval.w
  rgfDaysOfTheWeek.w
EndStructure
Structure MONTHLYDATE
  rgfDays.l
  rgfMonths.w
  wAlignment.w
EndStructure
Structure MONTHLYDOW
  wWhichWeek.w
  rgfDaysOfTheWeek.w
  rgfMonths.w
  wAlignment.w
EndStructure
Structure TRIGGER_TYPE_UNION
  StructureUnion
    Daily.DAILY
    Weekly.WEEKLY
    MonthlyDate.MONTHLYDATE
    MonthlyDOW.MONTHLYDOW
  EndStructureUnion
EndStructure
Structure TASK_TRIGGER
  cbTriggerSize.w
  Reserved1.w
  wBeginYear.w
  wBeginMonth.w
  wBeginDay.w
  wEndYear.w
  wEndMonth.w
  wEndDay.w
  wStartHour.w
  wStartMinute.w
  MinutesDuration.l
  MinutesInterval.l
  rgFlags.l
  TriggerType.l 
  Type.TRIGGER_TYPE_UNION
  Reserved2.w
  wRandomMinutesInterval.w
EndStructure
Avatar de l’utilisateur
Progi1984
Messages : 2659
Inscription : mar. 14/déc./2004 13:56
Localisation : France > Rennes
Contact :

Message par Progi1984 »

Je viens de me rendre compte que ct parcequ'il me manquait aussi un "= #S_OK"aprés le SetTrigger.

Donc c'est réglé. Merci encore Flype !
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

peut etre qu'il te manquait ce #S_OK,

mais il va quand meme falloir mettre à jour ta structure avec mon tout dernier post.
Avatar de l’utilisateur
Progi1984
Messages : 2659
Inscription : mar. 14/déc./2004 13:56
Localisation : France > Rennes
Contact :

Message par Progi1984 »

C'est mis à jour....
Répondre