如何在任务计划程序中显示所有任务
How to display all the tasks in Task scheduler
我想使用 COM 库。
显示 Task Scheduler 下的所有任务
我尝试使用 docs.microsoft.com 上使用 COM 对象的程序。
但是它没有显示所有 60 个任务,而是只显示 12 个任务
//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);
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);
我希望输出显示所有任务,但它只显示选定数量的任务。
https://docs.microsoft.com/en-us/windows/desktop/taskschd/displaying-task-names-and-state--c---
这是我用于程序的link
这是一个示例控制台应用程序代码,它将递归地显示所有任务,但正如我在评论中所说,输出将取决于您是否 运行 这是管理员。
// include task scheduler lib from code
#pragma comment(lib, "taskschd.lib")
// some useful macros
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
#define HRCHECK(__expr) {hr=(__expr);if(FAILED(hr)){wprintf(L"FAILURE 0x%08X (%i)\n\tline: %u file: '%s'\n\texpr: '" WIDEN(#__expr) L"'\n",hr, hr, __LINE__,__WFILE__);goto cleanup;}}
#define RELEASE(__p) {if(__p!=nullptr){__p->Release();__p=nullptr;}}
#define STARTUP HRESULT hr=S_OK;
#define CLEANUP {cleanup:return hr;}
// forward declaration
HRESULT DumpFolder(ITaskFolder *fld);
int main()
{
STARTUP;
CoInitialize(NULL);
{
CComPtr<ITaskService> svc;
CComPtr<ITaskFolder> fld;
HRCHECK(svc.CoCreateInstance(CLSID_TaskScheduler));
HRCHECK(svc->Connect(CComVariant(), CComVariant(), CComVariant(), CComVariant()));
HRCHECK(svc->GetFolder(CComBSTR(L"\"), &fld));
HRCHECK(DumpFolder(fld));
}
CoUninitialize();
CLEANUP;
}
HRESULT DumpFolder(ITaskFolder *fld)
{
STARTUP;
CComPtr<IRegisteredTaskCollection> tasks;
CComPtr<ITaskFolderCollection> children;
LONG count;
HRCHECK(fld->GetTasks(TASK_ENUM_HIDDEN, &tasks));
HRCHECK(tasks->get_Count(&count));
// dump out tasks
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<IRegisteredTask> task;
CComBSTR name;
HRCHECK(tasks->get_Item(CComVariant(i), &task));
HRCHECK(task->get_Name(&name));
wprintf(L"name:'%s'\n", name.m_str);
}
// dump out sub folder
HRCHECK(fld->GetFolders(0, &children));
HRCHECK(children->get_Count(&count));
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<ITaskFolder> child;
HRCHECK(children->get_Item(CComVariant(i), &child));
// go recursive
HRCHECK(DumpFolder(child));
}
CLEANUP;
}
我想使用 COM 库。
显示 Task Scheduler 下的所有任务我尝试使用 docs.microsoft.com 上使用 COM 对象的程序。 但是它没有显示所有 60 个任务,而是只显示 12 个任务
//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);
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);
我希望输出显示所有任务,但它只显示选定数量的任务。
https://docs.microsoft.com/en-us/windows/desktop/taskschd/displaying-task-names-and-state--c---
这是我用于程序的link
这是一个示例控制台应用程序代码,它将递归地显示所有任务,但正如我在评论中所说,输出将取决于您是否 运行 这是管理员。
// include task scheduler lib from code
#pragma comment(lib, "taskschd.lib")
// some useful macros
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
#define HRCHECK(__expr) {hr=(__expr);if(FAILED(hr)){wprintf(L"FAILURE 0x%08X (%i)\n\tline: %u file: '%s'\n\texpr: '" WIDEN(#__expr) L"'\n",hr, hr, __LINE__,__WFILE__);goto cleanup;}}
#define RELEASE(__p) {if(__p!=nullptr){__p->Release();__p=nullptr;}}
#define STARTUP HRESULT hr=S_OK;
#define CLEANUP {cleanup:return hr;}
// forward declaration
HRESULT DumpFolder(ITaskFolder *fld);
int main()
{
STARTUP;
CoInitialize(NULL);
{
CComPtr<ITaskService> svc;
CComPtr<ITaskFolder> fld;
HRCHECK(svc.CoCreateInstance(CLSID_TaskScheduler));
HRCHECK(svc->Connect(CComVariant(), CComVariant(), CComVariant(), CComVariant()));
HRCHECK(svc->GetFolder(CComBSTR(L"\"), &fld));
HRCHECK(DumpFolder(fld));
}
CoUninitialize();
CLEANUP;
}
HRESULT DumpFolder(ITaskFolder *fld)
{
STARTUP;
CComPtr<IRegisteredTaskCollection> tasks;
CComPtr<ITaskFolderCollection> children;
LONG count;
HRCHECK(fld->GetTasks(TASK_ENUM_HIDDEN, &tasks));
HRCHECK(tasks->get_Count(&count));
// dump out tasks
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<IRegisteredTask> task;
CComBSTR name;
HRCHECK(tasks->get_Item(CComVariant(i), &task));
HRCHECK(task->get_Name(&name));
wprintf(L"name:'%s'\n", name.m_str);
}
// dump out sub folder
HRCHECK(fld->GetFolders(0, &children));
HRCHECK(children->get_Count(&count));
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<ITaskFolder> child;
HRCHECK(children->get_Item(CComVariant(i), &child));
// go recursive
HRCHECK(DumpFolder(child));
}
CLEANUP;
}