Click here to Skip to main content
15,890,043 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: VC Profiler Pin
RChin28-Jul-05 1:58
RChin28-Jul-05 1:58 
GeneralRe: VC Profiler Pin
Giles28-Jul-05 6:36
Giles28-Jul-05 6:36 
GeneralSearching for help on-line Pin
onlysaint27-Jul-05 23:54
onlysaint27-Jul-05 23:54 
GeneralRe: Searching for help on-line Pin
mark novak28-Jul-05 1:03
mark novak28-Jul-05 1:03 
GeneralProblems with Task Scheduler Pin
beedub27-Jul-05 23:21
beedub27-Jul-05 23:21 
GeneralRe: Problems with Task Scheduler Pin
mark novak28-Jul-05 0:46
mark novak28-Jul-05 0:46 
GeneralRe: Problems with Task Scheduler Pin
David Crow28-Jul-05 5:19
David Crow28-Jul-05 5:19 
GeneralRe: Problems with Task Scheduler Pin
beedub28-Jul-05 6:17
beedub28-Jul-05 6:17 
I've pasted the contents of the file here. The scheduling code has not been integrated into my app yet - I'm trying to get it to work first.

--------------------------------------------------------------------------------------------------
#include <afx.h>
#include <objbase.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>
#include "AFXPRIV.H"

int scheduleTask(SYSTEMTIME Time,CString ExeAndPath,CString ArgList,CString name);
int CreateTrigger(CString name, SYSTEMTIME Time);


int main(int argc, char **argv)
{
USES_CONVERSION;
SYSTEMTIME Time;

Time.wYear = 2005;
Time.wMonth = 7;
Time.wDayOfWeek = 2;
Time.wDay= 27;
Time.wHour= 18;
Time.wMinute= 0;
Time.wSecond= 0;
Time.wMilliseconds=0;

CString ExeAndPath = "C:\\Program Files\\Exterity";
CString ArgList = "\"ExterityLimitedRecord.exe\" -a 239.192.64.105 -p 49408 -t 120 -d C:\\temp\\ScheduledRecord1.mpg";

CString name = "Scheduled Record1";

int result = scheduleTask(Time,ExeAndPath,ArgList,name);
return result;
}

int scheduleTask(SYSTEMTIME Time,CString ExeAndPath,CString ArgList,CString name)
{
HRESULT hr = S_OK;
ITaskScheduler *pITS;
USES_CONVERSION;

/////////////////////////////////////////////////////////////////
// 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();
return 1;
}
}
else
{
return 1;
}


/////////////////////////////////////////////////////////////////
// Call ITaskScheduler::NewWorkItem to create new task.
/////////////////////////////////////////////////////////////////

ITask *pITask;
IPersistFile *pIPersistFile;
LPCWSTR pwszAccountName = L"";// run as local account
LPCWSTR pwszPassword = NULL;// run as local account


hr = pITS->NewWorkItem(A2CW(name),//pwszTaskName, // Name of task
CLSID_CTask, // Class identifier
IID_ITask, // Interface identifier
(IUnknown**)&pITask); // Address of task interface



pITS->Release(); // Release object
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
return 1;
}

hr = pITask->SetWorkingDirectory(A2CW(ExeAndPath));

if(FAILED(hr)){
CoUninitialize();
fprintf(stderr, "Failed setting Working directory, error = 0x%x\n",hr);
return 1;
}

hr = pITask->SetParameters(A2CW(ArgList));
if(FAILED(hr)){
CoUninitialize();
fprintf(stderr, "Failed setting Parameters, error = 0x%x\n",hr);
return 1;
}

pITask->SetFlags(TASK_FLAG_DELETE_WHEN_DONE);

//run as local acocunt
hr = pITask->SetAccountInformation(pwszAccountName,pwszPassword);
if(FAILED(hr)){
CoUninitialize();
fprintf(stderr, "Failed setting Account Information, error = 0x%x\n",hr);
return 1;
}
/////////////////////////////////////////////////////////////////
// Call IUnknown::QueryInterface to get a pointer to
// IPersistFile and IPersistFile::Save to save
// the new task to disk.
/////////////////////////////////////////////////////////////////

hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);

pITask->Release();
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
return 1;
}


hr = pIPersistFile->Save(NULL,
TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
return 1;
}



printf("Created task.\n");

hr =CreateTrigger(name,Time);
return 0;
}

int CreateTrigger(CString name, SYSTEMTIME Time)
{
HRESULT hr = S_OK;
ITaskScheduler *pITS;
USES_CONVERSION;
/////////////////////////////////////////////////////////////////
// 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();
return 1;
}
}
else
{
return 1;
}

///////////////////////////////////////////////////////////////////
// Call ITaskScheduler::Activate to get the Task object.
///////////////////////////////////////////////////////////////////

ITask *pITask;

hr = pITS->Activate(A2CW(name),//pwszTaskName,
IID_ITask,
(IUnknown**) &pITask);
if (FAILED(hr))
{
wprintf(L"Failed calling ITaskScheduler::Activate: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}
pITS->Release();


ITaskTrigger *pITaskTrigger;
WORD piNewTrigger;
hr = pITask->CreateTrigger(&piNewTrigger,
&pITaskTrigger);
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::CreatTrigger: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}
//////////////////////////////////////////////////////
// Define TASK_TRIGGER structure. Note that wBeginDay,
// wBeginMonth, and wBeginYear must be set to a valid
// day, month, and year respectively.
//////////////////////////////////////////////////////

TASK_TRIGGER pTrigger;
ZeroMemory(&pTrigger, sizeof (TASK_TRIGGER));

// Add code to set trigger structure?
pTrigger.wBeginDay =Time.wDay; // Required
pTrigger.wBeginMonth =Time.wMonth; // Required
pTrigger.wBeginYear =Time.wYear; // Required
pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);
pTrigger.wStartHour = Time.wHour;
pTrigger.TriggerType = TASK_TIME_TRIGGER_ONCE;
pTrigger.Type.Daily.DaysInterval = 1;

///////////////////////////////////////////////////////////////////
// Call ITaskTrigger::SetTrigger to set trigger criteria.
///////////////////////////////////////////////////////////////////

hr = pITaskTrigger->SetTrigger (&pTrigger);
if (FAILED(hr))
{
wprintf(L"Failed calling ITaskTrigger::SetTrigger: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}

///////////////////////////////////////////////////////////////////
// Call IPersistFile::Save to save trigger to disk.
///////////////////////////////////////////////////////////////////

IPersistFile *pIPersistFile;
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
hr = pIPersistFile->Save(NULL,
TRUE);

wprintf(L"The trigger was created and IPersistFile::Save was \n");
wprintf(L"called to save the new trigger to disk.\n");


///////////////////////////////////////////////////////////////////
// Release resources.
///////////////////////////////////////////////////////////////////

pITask->Release();
pITaskTrigger->Release();
pIPersistFile->Release();

return 0;
} // End Create Trigger



Thanks,
Brian
GeneralRe: Problems with Task Scheduler Pin
David Crow28-Jul-05 8:57
David Crow28-Jul-05 8:57 
QuestionHow to make a splitted frame? Pin
Olgun27-Jul-05 23:01
Olgun27-Jul-05 23:01 
AnswerRe: How to make a splitted frame? Pin
Mohammed F. Salem28-Jul-05 0:30
Mohammed F. Salem28-Jul-05 0:30 
GeneralRe: How to make a splitted frame? Pin
David Crow28-Jul-05 5:24
David Crow28-Jul-05 5:24 
GeneralRe: How to make a splitted frame? Pin
Olgun28-Jul-05 23:23
Olgun28-Jul-05 23:23 
QuestionHow do i convert CString from MFC application to String in Win32 application Pin
santoshskulkarni27-Jul-05 20:46
santoshskulkarni27-Jul-05 20:46 
AnswerRe: How do i convert CString from MFC application to String in Win32 application Pin
Bob Stanneveld27-Jul-05 21:06
Bob Stanneveld27-Jul-05 21:06 
GeneralRe: How do i convert CString from MFC application to String in Win32 application Pin
santoshskulkarni27-Jul-05 22:14
santoshskulkarni27-Jul-05 22:14 
GeneralRe: How do i convert CString from MFC application to String in Win32 application Pin
Bob Stanneveld27-Jul-05 22:55
Bob Stanneveld27-Jul-05 22:55 
GeneralRe: How do i convert CString from MFC application to String in Win32 application Pin
santoshskulkarni28-Jul-05 0:58
santoshskulkarni28-Jul-05 0:58 
GeneralRe: How do i convert CString from MFC application to String in Win32 application Pin
John R. Shaw28-Jul-05 5:30
John R. Shaw28-Jul-05 5:30 
GeneralCView and Dialog Pin
Mohammed F. Salem27-Jul-05 20:35
Mohammed F. Salem27-Jul-05 20:35 
GeneralRe: CView and Dialog Pin
Cool Ju27-Jul-05 21:18
Cool Ju27-Jul-05 21:18 
GeneralRe: CView and Dialog Pin
Mohammed F. Salem27-Jul-05 22:22
Mohammed F. Salem27-Jul-05 22:22 
GeneralRe: CView and Dialog Pin
Cool Ju27-Jul-05 23:09
Cool Ju27-Jul-05 23:09 
GeneralRe: CView and Dialog Pin
Mohammed F. Salem27-Jul-05 23:58
Mohammed F. Salem27-Jul-05 23:58 
GeneralRe: CView and Dialog Pin
Satishkumar.B28-Jul-05 1:58
Satishkumar.B28-Jul-05 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.