> #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; }
|=== Build: Debug in TaskScheduler (compiler: GNU GCC Compiler) ===|
E:\WorkSpace\Code\TaskScheduler\main.cpp|9|warning: ignoring #pragma comment [-Wunknown-pragmas]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|10|warning: ignoring #pragma comment [-Wunknown-pragmas]|
E:\WorkSpace\Code\TaskScheduler\main.cpp||In function 'int wmain()':|
E:\WorkSpace\Code\TaskScheduler\main.cpp|22|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|40|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|47|error: 'ITaskService' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|47|error: 'pService' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|48|error: 'CLSID_TaskScheduler' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|51|error: 'IID_ITaskService' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|55|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|65|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|72|error: 'IRunningTaskCollection' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|72|error: 'pRunningTasks' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|73|error: 'TASK_ENUM_HIDDEN' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|78|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|94|warning: format '%d' expects argument of type 'int', but argument 2 has type 'LONG {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|96|error: 'TASK_STATE' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|100|error: 'IRunningTask' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|100|error: 'pRunningTask' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|112|error: 'taskState' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|116|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|120|warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|126|warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]|
E:\WorkSpace\Code\TaskScheduler\main.cpp|126|warning: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'HRESULT {aka long int}' [-Wformat=]|
||=== Build failed: 11 error(s), 12 warning(s) (0 minute(s), 1 second(s)) ===|
E:\WorkSpace\Code\TaskScheduler\main.cpp|47|error: 'ITaskService' was not declared in this scope|this are not linker but compiler errors, so you are missing a header here.
E:\WorkSpace\Code\TaskScheduler\main.cpp|47|error: 'pService' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|48|error: 'CLSID_TaskScheduler' was not declared in this scope|
E:\WorkSpace\Code\TaskScheduler\main.cpp|51|error: 'IID_ITaskService' was not declared in this scope|
I am sure I have linked the header into code::block (Build Option -> Linker Setting -> Add and type the header name) it still doesnt work!
-------------- Build: Debug in TaskScheduler (compiler: GNU GCC Compiler)---------------
x86_64-w64-mingw32-g++.exe -Wall -g -std=c++98 -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -IC:\TDM-GCC-64\bin -IC:\TDM-GCC-64 -c E:\WorkSpace\Code\TaskScheduler\main.cpp -o obj\Debug\main.o
E:\WorkSpace\Code\TaskScheduler\main.cpp:9:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib, "taskschd.lib")
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:10:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib, "comsupp.lib")
^
E:\WorkSpace\Code\TaskScheduler\main.cpp: In function 'int wmain()':
E:\WorkSpace\Code\TaskScheduler\main.cpp:22:50: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("\nCoInitializeEx failed: %x", hr );
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:40:56: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("\nCoInitializeSecurity failed: %x", hr );
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:47:5: error: 'ITaskService' was not declared in this scope
ITaskService *pService = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:47:19: error: 'pService' was not declared in this scope
ITaskService *pService = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:48:28: error: 'CLSID_TaskScheduler' was not declared in this scope
hr = CoCreateInstance( CLSID_TaskScheduler,
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:51:28: error: 'IID_ITaskService' was not declared in this scope
IID_ITaskService,
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:55:83: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:65:55: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("ITaskService::Connect failed: %x", hr );
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:72:5: error: 'IRunningTaskCollection' was not declared in this scope
IRunningTaskCollection* pRunningTasks = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:72:29: error: 'pRunningTasks' was not declared in this scope
IRunningTaskCollection* pRunningTasks = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:73:36: error: 'TASK_ENUM_HIDDEN' was not declared in this scope
hr = pService->GetRunningTasks(TASK_ENUM_HIDDEN, &pRunningTasks);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:78:57: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("Cannot get Root Folder pointer: %x", hr );
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:94:55: warning: format '%d' expects argument of type 'int', but argument 2 has type 'LONG {aka long int}' [-Wformat=]
printf("\nNumber of running tasks : %d", numTasks );
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:96:5: error: 'TASK_STATE' was not declared in this scope
TASK_STATE taskState;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:100:9: error: 'IRunningTask' was not declared in this scope
IRunningTask* pRunningTask = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:100:23: error: 'pRunningTask' was not declared in this scope
IRunningTask* pRunningTask = NULL;
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:112:47: error: 'taskState' was not declared in this scope
hr = pRunningTask->get_State(&taskState);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:116:78: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("\n\tCannot get the registered task state: %x", hr);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:120:71: warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'HRESULT {aka long int}' [-Wformat=]
printf("\nCannot get the registered task name: %x", hr);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:126:84: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
^
E:\WorkSpace\Code\TaskScheduler\main.cpp:126:84: warning: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'HRESULT {aka long int}' [-Wformat=]
Process terminated with status 1 (0 minute(s), 8 second(s))
11 error(s), 12 warning(s) (0 minute(s), 8 second(s))
E:\WorkSpace\Code\TaskScheduler\main.cpp:9:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib, "taskschd.lib")
E:\WorkSpace\Code\TaskScheduler\main.cpp:47:5: error: 'ITaskService' was not declared in this scope
ITaskService *pService = NULL;
Thanks for your clearly and fully helping. I tried following your recomendations but I could not find the class ITaskService in the MinGW64 header :(. What the version of MinGW did you use?
@stahta01:
do you have a working msys2 environment with codeblocks?
Is it as easy as simply setting the compiler path to the bin folder and all works?Yes
Does the msys compiler needs the paths in a msys format?
I can not test it at the moment, but i am interested...