Visual C++ saving task error 80070005

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, All,<br/>
<br/>
Last week I downloaded and installed Visual C++ 2010 Express(Free). My OS is Windows7 Professional. I also copied Microsoft "Time Trigger Example(C++)" code as following.
Unchanged any code except the start time to 1 minute after current time:<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre
<pre style="font-size:12px; padding:5px; font-family:Consolas,Courier,monospace; word-break:break-all; word-wrap:break-word; line-height:normal hr = pTimeTrigger->put_StartBoundary( _bstr_t(L<span style="color:#a31515 "2012-07-21T18:05:00") );[/code]
<br/>
After built and ran, it was successfully opened Notepad after 1 minute. Today, a prompt from Microsoft said, only 22 days left for expiration if I do not registration the Visual C++ 2010 Express (if registered, continue for free). Then I registered and got
the registration key to licence the software. But I found when I built and ran the Time Trigger Example(C++), it always got "Error saving the Task: 80070005" at last code block:
<pre style="border:none; font-family:Consolas,Courier,monospace; outline:0px; padding:5px; list-style-type:none; word-wrap:break-word; text-align:left; color:#333333; word-break:break-all; font-size:12px; line-height:normal <span style="border:0px; font-style:inherit; font-family:inherit; margin:0px; outline:0px; padding:0px; color:green // Save the task in the root folder.[/code]
The "hr" value is always "E_ACCESSDENIED"<br/>
<br/>
Anybody has clue about this issue?<br/>
<br/>
Many thanks in advance!

<pre style="padding:5px; font-family:Consolas,Courier,monospace; word-break:break-all; word-wrap:break-word; font-size:12px; line-height:normal <span style="color:green /********************************************************************
This sample schedules a task to start notepad.exe 1 minute from the
time the task is registered.
********************************************************************/

<span style="color:blue #define _WIN32_DCOM

#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <comdef.h>
#include <wincred.h>
<span style="color:green // Include the task header file.
#include <taskschd.h>
# pragma comment(lib, <span style="color:#a31515 "taskschd.lib")
# pragma comment(lib, <span style="color:#a31515 "comsupp.lib")
# pragma comment(lib, <span style="color:#a31515 "credui.lib")

<span style="color:blue using <span style="color:blue namespace std;


<span style="color:blue int __cdecl wmain()
{
<span style="color:green // ------------------------------------------------------
<span style="color:green // Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCoInitializeEx failed: %x", hr );
<span style="color:blue return 1;
}

<span style="color:green // 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);

<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Create a name for the task.
LPCWSTR wszTaskName = L<span style="color:#a31515 "Time Trigger Test Task";

<span style="color:green // Get the windows directory and set the path to notepad.exe.
wstring wstrExecutablePath = _wgetenv( L<span style="color:#a31515 "WINDIR");
wstrExecutablePath += L<span style="color:#a31515 "\SYSTEM32\NOTEPAD.EXE";


<span style="color:green // ------------------------------------------------------
<span style="color:green // Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(<span style="color:blue void**)&pService );
<span style="color:blue if (FAILED(hr))
{
printf(<span style="color:#a31515 "Failed to create an instance of ITaskService: %x", hr);
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Get the pointer to the root task folder. This folder will hold the
<span style="color:green // new task that is registered.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder( _bstr_t( L"\") , &pRootFolder );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "Cannot get Root folder pointer: %x", hr );
pService->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // If the same task exists, remove it.
pRootFolder->DeleteTask( _bstr_t( wszTaskName), 0 );

<span style="color:green // Create the task definition object to create the task.
ITaskDefinition *pTask = NULL;
hr = pService->NewTask( 0, &pTask );

pService->Release(); <span style="color:green // COM clean up. Pointer is no longer used.
<span style="color:blue if (FAILED(hr))
{
printf(<span style="color:#a31515 "Failed to CoCreate an instance of the TaskService class: %x", hr);
pRootFolder->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Get the registration info for setting the identification.
IRegistrationInfo *pRegInfo= NULL;
hr = pTask->get_RegistrationInfo( &pRegInfo );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get identification pointer: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

hr = pRegInfo->put_Author( L<span style="color:#a31515 "Author Name" );
pRegInfo->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot put identification info: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Create the principal for the task - these credentials
<span style="color:green // are overwritten with the credentials passed to RegisterTaskDefinition
IPrincipal *pPrincipal = NULL;
hr = pTask->get_Principal( &pPrincipal );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get principal pointer: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Set up principal logon type to interactive logon
hr = pPrincipal->put_LogonType( TASK_LOGON_INTERACTIVE_TOKEN );
pPrincipal->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot put principal info: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Create the settings for the task
ITaskSettings *pSettings = NULL;
hr = pTask->get_Settings( &pSettings );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get settings pointer: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Set setting values for the task.
hr = pSettings->put_StartWhenAvailable(VARIANT_BOOL(<span style="color:blue true));
pSettings->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot put setting information: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Set the idle settings for the task.
IIdleSettings *pIdleSettings = NULL;
hr = pSettings->get_IdleSettings( &pIdleSettings );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get idle setting information: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

hr = pIdleSettings->put_WaitTimeout(L<span style="color:#a31515 "PT5M");
pIdleSettings->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot put idle setting information: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}


<span style="color:green // ------------------------------------------------------
<span style="color:green // Get the trigger collection to insert the time trigger.
ITriggerCollection *pTriggerCollection = NULL;
hr = pTask->get_Triggers( &pTriggerCollection );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get trigger collection: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Add the time trigger to the task.
ITrigger *pTrigger = NULL;
hr = pTriggerCollection->Create( TASK_TRIGGER_TIME, &pTrigger );
pTriggerCollection->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot create trigger: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

ITimeTrigger *pTimeTrigger = NULL;
hr = pTrigger->QueryInterface(
IID_ITimeTrigger, (<span style="color:blue void**) &pTimeTrigger );
pTrigger->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nQueryInterface call failed for ITimeTrigger: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

hr = pTimeTrigger->put_Id( _bstr_t( L<span style="color:#a31515 "Trigger1" ) );
<span style="color:blue if( FAILED(hr) )
printf(<span style="color:#a31515 "nCannot put trigger ID: %x", hr);

hr = pTimeTrigger->put_EndBoundary( _bstr_t(L<span style="color:#a31515 "2015-05-02T08:00:00") );
<span style="color:blue if( FAILED(hr) )
printf(<span style="color:#a31515 "nCannot put end boundary on trigger: %x", hr);

<span style="color:green // Set the task to start at a certain time. The time
<span style="color:green // format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone).
<span style="color:green // For example, the start boundary below
<span style="color:green // is January 1st 2005 at 12:05
hr = pTimeTrigger->put_StartBoundary( _bstr_t(L<span style="color:#a31515 "2012-07-21T18:05:00") );
pTimeTrigger->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot add start boundary to trigger: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}


<span style="color:green // ------------------------------------------------------
<span style="color:green // Add an action to the task. This task will execute notepad.exe.
IActionCollection *pActionCollection = NULL;

<span style="color:green // Get the task action collection pointer.
hr = pTask->get_Actions( &pActionCollection );
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot get Task collection pointer: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Create the action, specifying that it is an executable action.
IAction *pAction = NULL;
hr = pActionCollection->Create( TASK_ACTION_EXEC, &pAction );
pActionCollection->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot create the action: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

IExecAction *pExecAction = NULL;
<span style="color:green // QI for the executable task pointer.
hr = pAction->QueryInterface(
IID_IExecAction, (<span style="color:blue void**) &pExecAction );
pAction->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nQueryInterface call failed for IExecAction: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // Set the path of the executable to notepad.exe.
hr = pExecAction->put_Path( _bstr_t( wstrExecutablePath.c_str() ) );
pExecAction->Release();
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nCannot put action path: %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

<span style="color:green // ------------------------------------------------------
<span style="color:green // Save the task in the root folder.
IRegisteredTask *pRegisteredTask = NULL;
hr = pRootFolder->RegisterTaskDefinition(
_bstr_t( wszTaskName ),
pTask,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_INTERACTIVE_TOKEN,
_variant_t(L<span style="color:#a31515 ""),
&pRegisteredTask);
<span style="color:blue if( FAILED(hr) )
{
printf(<span style="color:#a31515 "nError saving the Task : %x", hr );
pRootFolder->Release();
pTask->Release();
CoUninitialize();
<span style="color:blue return 1;
}

printf(<span style="color:#a31515 "n Success! Task successfully registered. " );

<span style="color:green // Clean up.
pRootFolder->Release();
pTask->Release();
pRegisteredTask->Release();
CoUninitialize();
<span style="color:blue return 0;
}
[/code]

<br/>
<br/>

View the full article
 
Back
Top