显示 C++ (的任务名称和状态)
这两个 C++ 示例演示如何枚举任务。 一个示例演示如何在任务文件夹中显示任务的信息,另一个示例演示如何显示所有正在运行的任务的信息。
以下过程介绍如何显示任务文件夹中所有任务的任务名称和状态。
显示任务文件夹中所有任务的任务名称和状态
初始化 COM 并设置常规 COM 安全性。
创建 ITaskService 对象。
此对象允许连接到任务计划程序服务并访问特定的任务文件夹。
获取保存所需任务的任务文件夹。
使用 ITaskService::GetFolder 方法获取文件夹。
从文件夹中获取任务的集合。
使用 ITaskFolder::GetTasks 方法获取 (IRegisteredTaskCollection) 的任务集合。
获取集合中的任务数,并枚举集合中的每个任务。
使用 IRegisteredTaskCollection 的 Item 属性 获取 IRegisteredTask 实例。 每个实例将包含集合中的任务。 然后,可以从每个已注册的任务) 显示 (属性值的信息。
以下 C++ 示例演示如何显示根任务文件夹中所有任务的名称和状态。
/********************************************************************
This sample enumerates through the tasks on the local computer and
displays their name and state.
********************************************************************/
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <comdef.h>
// Include the task header file.
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
// ------------------------------------------------------
// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if( FAILED(hr) )
{
printf("\nCoInitializeEx failed: %x", hr );
return 1;
}
// 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( FAILED(hr) )
{
printf("\nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
return 1;
}
// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
printf("ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Get the pointer to the root task folder.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder );
pService->Release();
if( FAILED(hr) )
{
printf("Cannot get Root Folder pointer: %x", hr );
CoUninitialize();
return 1;
}
// -------------------------------------------------------
// Get the registered tasks in the folder.
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks( NULL, &pTaskCollection );
pRootFolder->Release();
if( FAILED(hr) )
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
if( numTasks == 0 )
{
printf("\nNo Tasks are currently running" );
pTaskCollection->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of Tasks : %d", numTasks );
TASK_STATE taskState;
for(LONG i=0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item( _variant_t(i+1), &pRegisteredTask );
if( SUCCEEDED(hr) )
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if( SUCCEEDED(hr) )
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED (hr) )
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
}
}
pTaskCollection->Release();
CoUninitialize();
return 0;
}
以下过程介绍如何显示所有正在运行的任务的任务名称和状态。
显示所有正在运行的任务的任务名称和状态
初始化 COM 并设置常规 COM 安全性。
创建 ITaskService 对象。
此对象允许连接到任务计划程序服务并访问特定的任务文件夹。
使用 ITaskService::GetRunningTasks 方法可获取 IRunningTaskCollection) (所有正在运行的任务的集合。 可以指定获取正在运行的任务实例,包括或排除隐藏的任务。
获取集合中的任务数,并枚举集合中的每个任务。
使用 IRunningTaskCollection 的 Item 属性 获取 IRunningTask 实例。 每个实例将包含集合中的任务。 然后,可以从每个已注册的任务) 显示 (属性值的信息。
以下 C++ 示例演示如何显示所有正在运行的任务的名称和状态,包括隐藏任务。
/********************************************************************
This sample enumerates through all running tasks on the local computer and
displays their name and state.
********************************************************************/
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <comdef.h>
// Include the task header file.
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
// ------------------------------------------------------
// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if( FAILED(hr) )
{
printf("\nCoInitializeEx failed: %x", hr );
return 1;
}
// 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( FAILED(hr) )
{
printf("\nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
return 1;
}
// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
printf("ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
return 1;
}
// Get the running tasks.
IRunningTaskCollection* pRunningTasks = NULL;
hr = pService->GetRunningTasks(TASK_ENUM_HIDDEN, &pRunningTasks);
pService->Release();
if( FAILED(hr) )
{
printf("Cannot get Root Folder pointer: %x", hr );
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pRunningTasks->get_Count(&numTasks);
if( numTasks == 0 )
{
printf("\nNo Tasks are currently running" );
pRunningTasks->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of running tasks : %d", numTasks );
TASK_STATE taskState;
for(LONG i=0; i < numTasks; i++)
{
IRunningTask* pRunningTask = NULL;
hr = pRunningTasks->get_Item( _variant_t(i+1), &pRunningTask );
if( SUCCEEDED(hr) )
{
BSTR taskName = NULL;
hr = pRunningTask->get_Name(&taskName);
if( SUCCEEDED(hr) )
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRunningTask->get_State(&taskState);
if (SUCCEEDED (hr) )
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRunningTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
}
}
pRunningTasks->Release();
CoUninitialize();
return 0;
}
相关主题