可有对Windowsy计划进行操作的API 接口?

Tom 2004-12-03 10:54:49
比如我要在程序中获取所有的计划任务,或者在程序中添加一个计划任务?
...全文
115 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Tom 2004-12-07
已解决。真奇怪,mstask.h文件竟然不能包含在类的头文件中,不知是何理。
回复
Tom 2004-12-07
kingzai大侠,出现链接错误:
TaskSchedule.obj : error LNK2001: 无法解析的外部符号 _CLSID_CTask
TaskSchedule.obj : error LNK2001: 无法解析的外部符号 _IID_ITask
TaskSchedule.obj : error LNK2001: 无法解析的外部符号 _CLSID_CTaskScheduler
TaskSchedule.obj : error LNK2001: 无法解析的外部符号 _IID_ITaskScheduler
Debug/WinpTask.dll : fatal error LNK1120: 4 个无法解析的外部命令

急急急!
回复
Tom 2004-12-03
谢谢!,加30分,有疑问再请教。
回复
kingzai 2004-12-03
BOOL CTaskSch::Delete(LPCTSTR strName)
{
// TODO: Add your dispatch handler code here

HRESULT hr = ERROR_SUCCESS;
ITaskScheduler *pITS;
//m_strName = strName;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}
unsigned short * pName;
AnsiToUnicode(strName, &pName);
hr=pITS->Delete(pName);
CoTaskMemFree(pName);

pITS->Release();
if(FAILED(hr))
{
CoUninitialize();
return FALSE;
}
else
{
CoUninitialize();
return TRUE;
}
}
else
return FALSE;


}


BOOL CTaskSch::Save()
{
// TODO: Add your dispatch handler code here

HRESULT hr = ERROR_SUCCESS;


///////////////////////////////////////////////////////////////////
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///////////////////////////////////////////////////////////////////
ITaskScheduler *pITS;

hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}
}
else
{
return FALSE;
}



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

ITask *pITask;
unsigned short* lpcwszTaskName;
// unsigned short * pName= new unsigned short[m_strName.GetLength()+2];
// memset(pName,0,sizeof(unsigned short) * (m_strName.GetLength()+2));
//CString2LPCWSTR(pName,m_strName);

AnsiToUnicode(m_strName, &lpcwszTaskName);
// lpcwszTaskName = pName;

hr = pITS->Activate(lpcwszTaskName,
IID_ITask,
(IUnknown**) &pITask);

// delete[] pName;//lpcwszTaskName;
CoTaskMemFree(lpcwszTaskName);

// Release the ITaskScheduler interface.
pITS->Release();

if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}

//Set Comment

unsigned short* pwszComment;// = m_strApplication.GetBuffer(40);
AnsiToUnicode(m_strComment, &pwszComment);
hr = pITask->SetComment(pwszComment);
CoTaskMemFree(pwszComment);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


//set application name

unsigned short* pwszApplicationName;// = m_strApplication.GetBuffer(40);
// unsigned short * pApplicationName= new unsigned short[m_strApplication.GetLength()+2];
// CString2LPCWSTR(pApplicationName,m_strApplication);
// pwszApplicationName = pApplicationName;
AnsiToUnicode(m_strApplication, &pwszApplicationName);
hr = pITask->SetApplicationName(pwszApplicationName);
// delete[] pApplicationName;
CoTaskMemFree(pwszApplicationName);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


//set working directory

unsigned short* pwszWorkingDirectory ;//= m_strStartingDir.GetBuffer(40);
// unsigned short * pDir= new unsigned short[m_strStartingDir.GetLength()+2];
// CString2LPCWSTR(pDir,m_strStartingDir);
// pwszWorkingDirectory = pDir;
AnsiToUnicode(m_strStartingDir, &pwszWorkingDirectory);
hr = pITask->SetWorkingDirectory(pwszWorkingDirectory);
// delete[] pDir;
CoTaskMemFree(pwszWorkingDirectory);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


//set parameters
unsigned short* pwszParameters ;//= L"";
AnsiToUnicode(m_strParameters, &pwszParameters);
hr = pITask->SetParameters(pwszParameters);
CoTaskMemFree(pwszParameters);
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::SetParameters: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}

//set prioprity
/*DWORD dwPriority = HIGH_PRIORITY_CLASS;

hr = pITask->SetPriority(dwPriority);

if (FAILED(hr))
{
wprintf(L"Failed calling ITask::SetPriority: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}*/

//set account and password
unsigned short* pwszAccountName;
unsigned short* pwszPassword;
AnsiToUnicode(m_strUserName, &pwszAccountName);
AnsiToUnicode(m_strPassword, &pwszPassword);
hr = pITask->SetAccountInformation(pwszAccountName,
pwszPassword);
CoTaskMemFree(pwszAccountName);
CoTaskMemFree(pwszPassword);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}

//set task trigger
ITaskTrigger *pITaskTrigger;
WORD piNewTrigger;
hr = pITask->CreateTrigger(&piNewTrigger,
&pITaskTrigger);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


//////////////////////////////////////////////////////
// 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 =m_nDay%31; // Required
pTrigger.wBeginMonth =m_nMonth%12; // Required
pTrigger.wBeginYear =m_nYear; // Required
pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);
pTrigger.wStartHour = m_nHour%24;
pTrigger.wStartMinute = m_nMinute%60;
pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY;
pTrigger.Type.Daily.DaysInterval = 1;


hr = pITaskTrigger->SetTrigger (&pTrigger);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}
pITaskTrigger->Release();


///////////////////////////////////////////////////////////////////
// Call IPersistFile::Save to save the modified task to disk.
///////////////////////////////////////////////////////////////////
IPersistFile *pIPersistFile;

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

// Release the ITask interface.
pITask->Release();

hr = pIPersistFile->Save(NULL,
TRUE);

// Release the IPersistFile interface.
pIPersistFile->Release();

CoUninitialize();

return TRUE;
}
回复
Tom 2004-12-03
楼上好,可否给出CTaskSch类的源码,可加分感谢!
回复
kingzai 2004-12-03
BOOL CTaskSch::CreateTask(LPCTSTR name)
{
// TODO: Add your dispatch handler code here
HRESULT hr = ERROR_SUCCESS;
ITaskScheduler *pITS;
m_strName = name;

hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}
}
else
{
return FALSE;
}


/////////////////////////////////////////////////////////////////
// Call ITaskScheduler::NewWorkItem to create new task.
/////////////////////////////////////////////////////////////////
unsigned short* pwszTaskName;
ITask *pITask;
IPersistFile *pIPersistFile;
//pwszTaskName = L"Test Task";
// unsigned short * pName= new unsigned short[m_strName.GetLength()+2];
// CString2LPCWSTR(pName,m_strName);
// pwszTaskName = pName;
AnsiToUnicode(m_strName, &pwszTaskName);
hr = pITS->NewWorkItem(pwszTaskName, // Name of task
CLSID_CTask, // Class identifier
IID_ITask, // Interface identifier
(IUnknown**)&pITask); // Address of task interface
CoTaskMemFree(pwszTaskName);

// delete pName;
pITS->Release(); // Release object
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


/////////////////////////////////////////////////////////////////
// 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();
return FALSE;
}


hr = pIPersistFile->Save(NULL,
TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
CoUninitialize();
return FALSE;
}


/////////////////////////////////////////////////////////////////
// Release all resources.
/////////////////////////////////////////////////////////////////
return TRUE;

}
回复
相关推荐
发帖
硬件/系统
创建于2007-09-28

2594

社区成员

VC/MFC 硬件/系统
申请成为版主
帖子事件
创建了帖子
2004-12-03 10:54
社区公告
暂无公告