Author Topic: RunningTask was not declared in this scope  (Read 1796 times)

Offline Cloud_Strife_Han

  • Single posting newcomer
  • *
  • Posts: 8
RunningTask was not declared in this scope
« on: March 16, 2018, 05:22:42 pm »
Hi you guys, It's the first time I used code::block to build a project. So I encounter some problems.

When I use VS2012 to build a example code[below] from msdn, it's completely successful. But it is opposite with Code::Block. I figured the problem occured when linking library or something similar, so I have to add "C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86" to Search Directories. I alse add name of using library into Linker Settings. But I still dont work, it show some error about library! I checked file dll in my computer then it exist.

This is my code:

Code: [Select]
> #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; }

And I got these errors

Quote
|=== 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)) ===|

Any ideas for my problem?
Thank for reading! Sorry for my bad english.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2464
Re: RunningTask was not declared in this scope
« Reply #1 on: March 16, 2018, 06:47:33 pm »
Hi,
first of all codeblocks is not a compiler. So technically you do not compile with codeblocks but with gcc. So you use a new compiler to compile your code. If you have any problems and you search the web you should not search for "help compiler error codeblocks" but "help compiler error gcc"

Second. Thank you for providing source and some build messages. But we need other information:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_(general)#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

also:
Quote
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|
this are not linker but compiler errors, so you are missing a header here.

BTW. you can use codeblocks also with the msvc compiler so it should compile straight the way...

Offline Cloud_Strife_Han

  • Single posting newcomer
  • *
  • Posts: 8
Re: RunningTask was not declared in this scope
« Reply #2 on: March 16, 2018, 07:03:50 pm »
Thank for your reply.

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!

I'm using code::block version 17.04 on windows 7 with TDM-GCC MinGW Compiler (ver 5.1.0). This occur happens immedialy when I build project.

I attempted to:
1. Linking the header
2. Add Search Directories
3. Copy header files into project then add as #include "header" then I see no error about library, just has last one error ||error: ld returned 1 exit status|!

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2464
Re: RunningTask was not declared in this scope
« Reply #3 on: March 16, 2018, 07:16:32 pm »
Quote
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!

Header (compiler) file: https://en.wikipedia.org/wiki/Include_directive
vs
Library (linker) https://en.wikipedia.org/wiki/Library_(computing)

and to help you we need the full rebuild log as is described in the FAQs and not the build messages

Offline Cloud_Strife_Han

  • Single posting newcomer
  • *
  • Posts: 8
Re: RunningTask was not declared in this scope
« Reply #4 on: March 16, 2018, 07:41:38 pm »
This is my build log:

Quote
-------------- 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))
 

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2464
Re: RunningTask was not declared in this scope
« Reply #5 on: March 16, 2018, 08:24:41 pm »
Now, you can see from this build log that you not arrive to the linking stage. You are still at compiling and try to fix linking problems...

This forum is not the right place for this kind of questions (from the type of: "help  get compiler error blabla, what should i do?" This forum is for questions like "Help, i get compiler error blabla and i know this is because i am missing the compiler flag xa. Where do i put this flag?" ) and by this violating the forum rules and probably will get locked. But anyway:

Code: [Select]
E:\WorkSpace\Code\TaskScheduler\main.cpp:9:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(lib, "taskschd.lib")
gcc does not understand the  "#pragma comment" and so you should remove all of them. But you should add all this ".lib" entries to
Project->Build options->Select the project name on the left->Linker settings->Additional libraries

Code: [Select]
E:\WorkSpace\Code\TaskScheduler\main.cpp:47:5: error: 'ITaskService' was not declared in this scope
     ITaskService *pService = NULL;
Ok, lets google this "'ITaskService' was not declared in this scope"...
according https://msdn.microsoft.com/en-us/library/windows/desktop/aa381832(v=vs.85).aspx this class is declared in taskschd.h . You included this header file but the compiler still can not find this class. I opened the file and searched for this class an can not find it. This sounds like tdm mingw what is shipped with codeblocks does not support this interface. A quick search showed that mingw64 has this interface. So you will need this compiler...

How to set up mingw64 in codeblocks?
I am not on a windows machine so i can not help you here, but generally it should be like this:
1) Install mingw64
2) in codeblocks Settings->Compiler->global compiler settings->Selected compiler->GNU GCC -> copy
3) For the new name enter "mingw64 gcc" -> ok
4)Toolchain executables->Compilers' installation directory -> point to the "bin" directory from the mingw64 installation->Try "auto detect"
5)If it fails you have to put the names of the corresponding executables at the bottom in the "Program Files"
6) Ok
7) Open your project->Project->Build options->On the left select your project name->From the drop down menu on the right top-> Select  "mingw64 gcc"  -> Ok
8) Build -> Clean
9) Build -> Run

if you have problems after installing mingw64 and setting it up in codeblocks make a new thread (This is the kind of questions this forum is for)
If you have problems after the installation of mingw64 and compiling your project ask again here...

This are all assumption. I can not test them because i do not use windows at the moment and i can not test this....

Offline Cloud_Strife_Han

  • Single posting newcomer
  • *
  • Posts: 8
Re: RunningTask was not declared in this scope
« Reply #6 on: March 17, 2018, 06:14:48 am »
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?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6613
    • My Best Post
Re: RunningTask was not declared in this scope
« Reply #7 on: March 17, 2018, 07:04:26 am »
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?

Please state what version of MinGW GCC you are using?
Please state where you got the MinGW GCC you are using?

Do you understand that MinGW64 is a fork of MinGW GCC from mingw.org?
And, the MinGW64 fork of GCC has more complete headers.

Edit: I have searched my MinGW GCC compilers to see which ones have "ITaskService"
Failed to find it in i686-7.2.0-release-win32-sjlj-rt_v5-rev1 I think other than MSys2 is the only MinGW64 compiler I have
Did find it in MSys2 MinGW64 GCC Compiler.
Edit2: gcc.exe (Rev1, Built by MSYS2 project) 7.3.0

Did not find it in any other of my MinGW GCC Compilers; I think the others are all mingw.org based ones.

Tim S.

« Last Edit: March 17, 2018, 07:34:30 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2464
Re: RunningTask was not declared in this scope
« Reply #8 on: March 17, 2018, 09:03:56 am »
@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? Does the msys compiler needs the paths in a msys format?  I can not test it at the moment, but i am interested...

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6613
    • My Best Post
Re: RunningTask was not declared in this scope
« Reply #9 on: March 17, 2018, 04:20:58 pm »
@stahta01:
do you have a working msys2 environment with codeblocks?

I have set up the normal release Code::Blocks to use the MSys2 mingw64 32-bit GCC compiler.
Not, really did much testing except for building some wxWidgets samples.
Link to CB Projects I used to build wxWidgets sample https://github.com/stahta01/CodeBlocks_UserTemplates_for_MSys2/tree/master/wxwidgets/samples

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?

No

For a while I used a patch to edit one of the CB variables to MSys2 windows forward slash format.
I am not sure why I needed this; but, it was needed for trying to build CB using that modified CB.
Edit3: I now recall I was trying to use wx-config for building CB; I have since decided this was a bad idea. 

I can not test it at the moment, but i am interested...

Edit: My patches to use MSys2 shell got to old to maintain. But, it was possible to use MSys2 shell without the patch by using sh.exe and
setting additional path correctly. Did almost no testing of this usage.

Tim S.
« Last Edit: March 17, 2018, 04:50:14 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Cloud_Strife_Han

  • Single posting newcomer
  • *
  • Posts: 8
Re: RunningTask was not declared in this scope
« Reply #10 on: March 18, 2018, 09:03:00 am »
Thanks for all! I handled the problems!  8) 8)

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2464
Re: RunningTask was not declared in this scope
« Reply #11 on: March 18, 2018, 10:27:03 am »
@Cloud_Strife_Han: It would be nice if you could describe what you have don, so others with the same problem do not have to ask the same question.

@stahta01: Thank you! Your Git repo is full of wonderful wonders to discover in the msys2 world xD