help convert Boot Trigger Example (C++)

Just starting out? Need help? Post your questions and find answers here.
mylover2009
New User
New User
Posts: 2
Joined: Tue Sep 19, 2017 1:09 pm

help convert Boot Trigger Example (C++)

Post by mylover2009 »

i convert this c++ code to pb
https://msdn.microsoft.com/en-us/librar ... s.85).aspx


code in the PBasic(5.60-x86) can't add task
pRootFolder\RegisterTaskDefinition return error $80020008

Code: Select all

EnableExplicit

; ITaskService interface definition
;
Interface ITaskService
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  GetFolder(a.p-bstr, b.l)
  GetRunningTasks(a.l, b.l)
  NewTask(a.l, b.l)
  Connect(a.p-variant, b.p-variant, c.p-variant, d.p-variant)
  get_Connected(a.l)
  get_TargetServer(a.l)
  get_ConnectedUser(a.l)
  get_ConnectedDomain(a.l)
  get_HighestVersion(a.l)
EndInterface
; ITaskFolder interface definition
;
Interface ITaskFolder
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Name(a.l)
  get_Path(a.l)
  GetFolder(a.l, b.l)
  GetFolders(a.l, b.l)
  CreateFolder(a.l, b.p-variant, c.l)
  DeleteFolder(a.l, b.l)
  GetTask(a.l, b.l)
  GetTasks(a.l, b.l)
  DeleteTask(a.p-bstr, b.l)
  RegisterTask(a.l, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  RegisterTaskDefinition(a.p-bstr, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  GetSecurityDescriptor(a.l, b.l)
  SetSecurityDescriptor(a.l, b.l)
EndInterface
; ITaskDefinition interface definition
;
Interface ITaskDefinition
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_RegistrationInfo(a.l)
  put_RegistrationInfo(a.l)
  get_Triggers(a.l)
  put_Triggers(a.l)
  get_Settings(a.l)
  put_Settings(a.l)
  get_Data(a.l)
  put_Data(a.p-bstr)
  get_Principal(a.l)
  put_Principal(a.l)
  get_Actions(a.l)
  put_Actions(a.l)
  get_XmlText(a.l)
  put_XmlText(a.p-bstr)
EndInterface
; IRegistrationInfo interface definition
;
Interface IRegistrationInfo
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Description(a.l)
  put_Description(a.p-bstr)
  get_Author(a.l)
  put_Author(a.p-bstr)
  get_Version(a.l)
  put_Version(a.p-bstr)
  get_Date(a.l)
  put_Date(a.p-bstr)
  get_Documentation(a.l)
  put_Documentation(a.p-bstr)
  get_XmlText(a.l)
  put_XmlText(a.p-bstr)
  get_URI(a.l)
  put_URI(a.p-bstr)
  get_SecurityDescriptor(a.l)
  put_SecurityDescriptor(a.p-variant)
  get_Source(a.l)
  put_Source(a.p-bstr)
EndInterface

; ITaskSettings interface definition
;
Interface ITaskSettings
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_AllowDemandStart(a.l)
  put_AllowDemandStart(a.l)
  get_RestartInterval(a.l)
  put_RestartInterval(a.p-bstr)
  get_RestartCount(a.l)
  put_RestartCount(a.l)
  get_MultipleInstances(a.l)
  put_MultipleInstances(a.l)
  get_StopIfGoingOnBatteries(a.l)
  put_StopIfGoingOnBatteries(a.l)
  get_DisallowStartIfOnBatteries(a.l)
  put_DisallowStartIfOnBatteries(a.l)
  get_AllowHardTerminate(a.l)
  put_AllowHardTerminate(a.l)
  get_StartWhenAvailable(a.l)
  put_StartWhenAvailable(a.l)
  get_XmlText(a.l)
  put_XmlText(a.p-bstr)
  get_RunOnlyIfNetworkAvailable(a.l)
  put_RunOnlyIfNetworkAvailable(a.l)
  get_ExecutionTimeLimit(a.l)
  put_ExecutionTimeLimit(a.p-bstr)
  get_Enabled(a.l)
  put_Enabled(a.l)
  get_DeleteExpiredTaskAfter(a.l)
  put_DeleteExpiredTaskAfter(a.p-bstr)
  get_Priority(a.l)
  put_Priority(a.l)
  get_Compatibility(a.l)
  put_Compatibility(a.l)
  get_Hidden(a.l)
  put_Hidden(a.l)
  get_IdleSettings(a.l)
  put_IdleSettings(a.l)
  get_RunOnlyIfIdle(a.l)
  put_RunOnlyIfIdle(a.l)
  get_WakeToRun(a.l)
  put_WakeToRun(a.l)
  get_NetworkSettings(a.l)
  put_NetworkSettings(a.l)
EndInterface
; ITriggerCollection interface definition
;
Interface ITriggerCollection
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Count(a.l)
  get_Item(a.l, b.l)
  get__NewEnum(a.l)
  Create(a.l, b.l)
  Remove(a.p-variant)
  Clear()
EndInterface
; ITrigger interface definition
;
Interface ITrigger
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Type(a.l)
  get_Id(a.l)
  put_Id(a.p-bstr)
  get_Repetition(a.l)
  put_Repetition(a.l)
  get_ExecutionTimeLimit(a.l)
  put_ExecutionTimeLimit(a.p-bstr)
  get_StartBoundary(a.l)
  put_StartBoundary(a.p-bstr)
  get_EndBoundary(a.l)
  put_EndBoundary(a.p-bstr)
  get_Enabled(a.l)
  put_Enabled(a.l)
EndInterface
; IBootTrigger interface definition
;
Interface IBootTrigger
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Type(a.l)
  get_Id(a.l)
  put_Id(a.p-bstr)
  get_Repetition(a.l)
  put_Repetition(a.l)
  get_ExecutionTimeLimit(a.l)
  put_ExecutionTimeLimit(a.p-bstr)
  get_StartBoundary(a.l)
  put_StartBoundary(a.p-bstr)
  get_EndBoundary(a.l)
  put_EndBoundary(a.p-bstr)
  get_Enabled(a.l)
  put_Enabled(a.l)
  get_Delay(a.l)
  put_Delay(a.p-bstr)
EndInterface
; IActionCollection interface definition
;
Interface IActionCollection
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Count(a.l)
  get_Item(a.l, b.l)
  get__NewEnum(a.l)
  get_XmlText(a.l)
  put_XmlText(a.p-bstr)
  Create(a.l, b.l)
  Remove(a.p-variant)
  Clear()
  get_Context(a.l)
  put_Context(a.p-bstr)
EndInterface
; IAction interface definition
;
Interface IAction
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Id(a.l)
  put_Id(a.p-bstr)
  get_Type(a.l)
EndInterface
; IExecAction interface definition
;
Interface IExecAction
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Id(a.l)
  put_Id(a.p-bstr)
  get_Type(a.l)
  get_Path(a.l)
  put_Path(a.p-bstr)
  get_Arguments(a.l)
  put_Arguments(a.p-bstr)
  get_WorkingDirectory(a.l)
  put_WorkingDirectory(a.p-bstr)
EndInterface
Interface IRegisteredTask
  QueryInterface(a.l, b.l)
  AddRef()
  Release()
  GetTypeInfoCount(a.l)
  GetTypeInfo(a.l, b.l, c.l)
  GetIDsOfNames(a.l, b.l, c.l, d.l, e.l)
  Invoke(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l)
  get_Name(a.l)
  get_Path(a.l)
  get_State(a.l)
  get_Enabled(a.l)
  put_Enabled(a.l)
  Run(a.p-variant, b.l)
  RunEx(a.p-variant, b.l, c.l, d.l, e.l)
  GetInstances(a.l, b.l)
  get_LastRunTime(a.l)
  get_LastTaskResult(a.l)
  get_NumberOfMissedRuns(a.l)
  get_NextRunTime(a.l)
  get_Definition(a.l)
  get_Xml(a.l)
  GetSecurityDescriptor(a.l, b.l)
  SetSecurityDescriptor(a.l, b.l)
  Stop(a.l)
  GetRunTimes(a.l, b.l, c.l, d.l)
EndInterface
DataSection
  CLSID_TaskScheduler:  ; {0f87369f-a4e5-4cfc-bd3e-73e6154572dd}
    Data.l $0f87369f
    Data.w $a4e5, $4cfc
    Data.b $bd, $3e, $73, $e6, $15, $45, $72, $dd
  IID_ITaskService:  ; {2faba4c7-4da9-4013-9697-20cc3fd40f85}
    Data.l $2faba4c7
    Data.w $4da9, $4013
    Data.b $96, $97, $20, $cc, $3f, $d4, $0f, $85
  IID_ILogonTrigger:  ; {72DADE38-FAE4-4b3e-BAF4-5D009AF02B1C}
    Data.l $72DADE38
    Data.w $FAE4, $4b3e
    Data.b $ba, $f4, $5d, $00, $9a, $f0, $2b, $1c
  IID_IBootTrigger:  ; {2A9C35DA-D357-41f4-BBC1-207AC1B1F3CB}
    Data.l $2A9C35DA
    Data.w $D357, $41f4
    Data.b $bb, $c1, $20, $7a, $c1, $b1, $f3, $cb
  IID_IExecAction:  ; {4c3d624d-fd6b-49a3-b9b7-09cb3cd3f047}
    Data.l $4c3d624d
    Data.w $fd6b, $49a3
    Data.b $b9, $b7, $09, $cb, $3c, $d3, $f0, $47
EndDataSection
#TASK_ACTION_EXEC=0
#TASK_TRIGGER_BOOT=8
#TASK_CREATE=$2
#TASK_UPDATE=$4
#TASK_CREATE_OR_UPDATE=#TASK_CREATE|#TASK_UPDATE
#TASK_LOGON_SERVICE_ACCOUNT=5
Procedure.l ComJobAdd(TaskName$,BinPath$)
Protected hr.i
;Initialize COM
hr=CoInitializeEx_(#Null,#COINIT_MULTITHREADED)
If hr
  ProcedureReturn 0
EndIf
;Set general COM security levels
hr=CoInitializeSecurity_(#Null,-1,#Null,#Null,#RPC_C_AUTHN_LEVEL_PKT_PRIVACY,#RPC_C_IMP_LEVEL_IMPERSONATE,#Null,0,#Null)
If hr
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Create an instance of the Task Service
Protected pService.ITaskService
hr=CoCreateInstance_(?CLSID_TaskScheduler,#Null,#CLSCTX_INPROC_SERVER,?IID_ITaskService,@pService)
If hr
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Connect to the task service

hr=pService\connect(@"",@"",@"",@"")
If hr
  pService\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Get the pointer to the root task folder
;This folder will hold the new task that is registered
Protected pRootFolder.ITaskFolder
hr=pService\GetFolder("\",@pRootFolder)
If hr
  pService\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;If the same task exists, remove it
pRootFolder\DeleteTask(TaskName$,0)
;Create the task builder object to create the task
Protected pTask.ITaskDefinition
hr=pService\NewTask(0,@pTask)
;COM clean up.  Pointer is no longer used
pService\Release()
If hr
  pRootFolder\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Get the registration info for setting the identification
Protected pRegInfo.IRegistrationInfo
hr=pTask\get_RegistrationInfo(@pRegInfo)
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
hr=pRegInfo\put_Author("Author Name")
pRegInfo\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Create the settings for the task
Protected pSettings.ITaskSettings
hr=pTask\get_Settings(@pSettings)
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Set setting values for the task
hr=pSettings\put_StartWhenAvailable(#VARIANT_TRUE)
pSettings\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Get the trigger collection to insert the boot trigger
Protected pTriggerCollection.ITriggerCollection
hr=pTask\get_Triggers(@pTriggerCollection)
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Add the boot trigger to the task
Protected pTrigger.ITrigger
hr=pTriggerCollection\Create(#TASK_TRIGGER_BOOT,@pTrigger)
pTriggerCollection\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
Protected pBootTrigger.IBootTrigger
hr=pTrigger\QueryInterface(?IID_IBootTrigger,@pBootTrigger)
pTrigger\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
hr=pBootTrigger\put_Id("Trigger1")
If hr

EndIf
;Set the task to start at a certain time. The time
;format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone)
;For example, the start boundary below
;is January 1st 2005 at 12:05
hr=pBootTrigger\put_StartBoundary("2005-01-01T12:05:00")
If hr
Debug hr
EndIf
hr=pBootTrigger\put_EndBoundary("2019-05-02T08:00:00")
If hr
Debug hr
EndIf
;Delay the task to start 30 seconds after system start
hr=pBootTrigger\put_Delay("PT30S")
pBootTrigger\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Add an Action to the task. This task will execute
Protected pActionCollection.IActionCollection
;Get the task action collection pointer
hr=pTask\get_Actions(@pActionCollection)
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Create the action, specifying it as an executable action
Protected pAction.IAction
hr=pActionCollection\Create(#TASK_ACTION_EXEC,@pAction)
pActionCollection\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;QI for the executable task pointer
Protected pExecAction.IExecAction
hr=pAction\QueryInterface(?IID_IExecAction,@pExecAction)
pAction\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Set the path of the executable to Notepad.exe.
hr=pExecAction\put_Path(BinPath$)
pExecAction\Release()
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
;Save the task in the root folder
Protected pRegisteredTask.IRegisteredTask
Protected varPassword.VARIANT
varPassword\vt=#VT_EMPTY
hr=pRootFolder\RegisterTaskDefinition(TaskName$,@pTask,#TASK_CREATE_OR_UPDATE,@"Local Service",varPassword,#TASK_LOGON_SERVICE_ACCOUNT,@"",@pRegisteredTask)
Debug Hex(hr,#PB_Long)
If hr
  pRootFolder\Release()
  pTask\Release()
  CoUninitialize_()
  ProcedureReturn 0
EndIf
pRootFolder\Release()
pTask\Release()
pRegisteredTask\Release()
CoUninitialize_()

ProcedureReturn 1
EndProcedure

ComJobAdd("hi8","C:\Windows\notepad.exe")
changed topic, RINGS
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: help with code

Post by mk-soft »

Invalid variable type...

Code: Select all

Procedure.s FormatMessage(Errorcode)

  Protected *Buffer, len, result.s

  len = FormatMessage_(#FORMAT_MESSAGE_ALLOCATE_BUFFER|#FORMAT_MESSAGE_FROM_SYSTEM,0,Errorcode,0,@*Buffer,0,0)
  If len
    result = PeekS(*Buffer, len)
    LocalFree_(*Buffer)
    ProcedureReturn result
  Else
    ProcedureReturn "Errorcode: " + Hex(Errorcode)
  EndIf

EndProcedure

Debug FormatMessage($80020008)
Perhaps helps
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: help with code

Post by mk-soft »

The declaration of interface is not right.
HRESULT RegisterTaskDefinition(
[in] BSTR path,
[in] ITaskDefinition *pDefinition,
[in] LONG flags,
[in] VARIANT userId,
[in] VARIANT password,
[in] TASK_LOGON_TYPE logonType,
[in, optional] VARIANT sddl,
[out] IRegisteredTask **ppTask
)
VARIANT is ByRef -> *pointer to var.variant

You are use purebasic prototpye "p-variant". This is a variant ByVal

Code: Select all

  ;RegisterTask(a.l, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  RegisterTask(a.l, b.l, c.l, d.p-variant, *e.variant, f.l, *g.variant, h.l)
  ;RegisterTaskDefinition(a.p-bstr, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  RegisterTaskDefinition(a.p-bstr, b.l, c.l, *d.variant, *e.variant, f.l, *g.variant, h.l)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mylover2009
New User
New User
Posts: 2
Joined: Tue Sep 19, 2017 1:09 pm

Re: help with code

Post by mylover2009 »

mk-soft wrote:The declaration of interface is not right.
HRESULT RegisterTaskDefinition(
[in] BSTR path,
[in] ITaskDefinition *pDefinition,
[in] LONG flags,
[in] VARIANT userId,
[in] VARIANT password,
[in] TASK_LOGON_TYPE logonType,
[in, optional] VARIANT sddl,
[out] IRegisteredTask **ppTask
)
VARIANT is ByRef -> *pointer to var.variant

You are use purebasic prototpye "p-variant". This is a variant ByVal

Code: Select all

  ;RegisterTask(a.l, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  RegisterTask(a.l, b.l, c.l, d.p-variant, *e.variant, f.l, *g.variant, h.l)
  ;RegisterTaskDefinition(a.p-bstr, b.l, c.l, d.p-variant, e.p-variant, f.l, g.p-variant, h.l)
  RegisterTaskDefinition(a.p-bstr, b.l, c.l, *d.variant, *e.variant, f.l, *g.variant, h.l)
I have modified the code, and the result is error
I'm a newbie on pb
can you test that code?and help me with the error
Post Reply