Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,284 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,034 people online right now. Registration is fast and FREE... Join Now!




Delete in Task scheduler

 
Reply to this topicStart new topic

Delete in Task scheduler

ashakumar
2 Jan, 2009 - 01:52 AM
Post #1

New D.I.C Head
*

Joined: 31 Dec, 2008
Posts: 3

Hi ,
I found out in a forum that ITaskscheduler's method 'Delete' can be used to delete task from task scheduler;
So i used the following code; Program is executed successfully; But task was not deleted; Where i may be wrong? Please advice me;
CODE

  #include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>
#include<stdio.h>
#include<iostream>
#pragma comment(lib, "Mstask.lib")
#pragma comment(lib, "ole32.lib")
using namespace std;

int main(int argc, char * argv[])
{
HRESULT hr = S_OK;
ITaskScheduler *pITS;
///////////////////////////////////////////////////////////////////
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///////////////////////////////////////////////////////////////////
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
cout<<"Failed to create object"<<std::endl;
return 1;
}
else
{
cout<<"Object created successfully"<<std::endl;
}
}
else
{
cout<<"Failed to initialize"<<std::endl;
return 1;
}
///////////////////////////////////////////////////////////////////
// Deleting already existing task
///////////////////////////////////////////////////////////////////

pITS->Delete(L"Test Task16");
if(FAILED(hr))
{
cout<<"Test task is not deleted";
}
else
{
cout<<"Test task deleted successfully"<<std::endl;
}
pITS->Release();

return 0;
}
    


User is offlineProfile CardPM
+Quote Post


no2pencil
RE: Delete In Task Scheduler
2 Jan, 2009 - 02:05 AM
Post #2

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,438



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
QUOTE

CODE

pITS->Delete(L"Test Task16");

Should the L really be outside of the quote?
User is offlineProfile CardPM
+Quote Post

ashakumar
RE: Delete In Task Scheduler
2 Jan, 2009 - 02:18 AM
Post #3

New D.I.C Head
*

Joined: 31 Dec, 2008
Posts: 3

[quote name='ashakumar' date='2 Jan, 2009 - 01:52 AM' post='502459']
I tried with the following code also
CODE

LPCWSTR pwszName;
pwszName = L"Test Task16";
pITS->Delete(pwszName);

But i got the same result;


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Delete In Task Scheduler
2 Jan, 2009 - 02:19 AM
Post #4

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,438



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
No, I meant something more like :

cpp

LPCWSTR pwszName;
pwszName = "Test Task16";
pITS->Delete(pwszName);

User is offlineProfile CardPM
+Quote Post

ashakumar
RE: Delete In Task Scheduler
2 Jan, 2009 - 02:35 AM
Post #5

New D.I.C Head
*

Joined: 31 Dec, 2008
Posts: 3

When i used like that , i got the following error
"error C2440: '=' : cannot convert from 'const char [12]' to 'LPCWSTR'"
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Delete In Task Scheduler
2 Jan, 2009 - 02:42 AM
Post #6

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,438



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
What if you just try removing the L, like so:

cpp

pITS->Delete("Test Task16");

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Delete In Task Scheduler
2 Jan, 2009 - 08:02 AM
Post #7

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Yes, the L belongs outside the quotes...it designates a wide-character string literal.

My guess as to why it's executing but not deleting the task is that the process attempting to delete the task requires permissions it does not have.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 02:56PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month