任务计划程序:尝试检索任务对象时出现问题

发布于 2024-10-11 19:34:51 字数 2049 浏览 6 评论 0原文

我正在使用/学习 win32 C++ 中的 Windows 任务计划程序。我正在尝试检索任务对象(对于存在的任务),但它不断失败并且失败。返回错误 COR_E_FILENOTFOUND 0x80070002 = 任务不存在

您认为出了什么问题?我知道这个任务存在,因为我创建了它(它做了它应该做的事情,在指定的时间打开一个应用程序)。

也许我用来检索 ITask 对象的任务名称不正确?我创建的任务的状态(优先级?)为 == 3(如果该信息有帮助)。

我用于创建任务的代码与 < 中的示例代码完全相同a href="http://msdn.microsoft.com/en-us/library/aa383624(v=VS.85).aspx" rel="nofollow">msdn,任务名称为“MyTaskSascha” .

我检索现有任务的代码有什么问题(我相信我尝试检索的任务不被视为“正在运行”而是“已计划”,因此也许是问题?):

bool RemoveTask( std::string taskName )
{
// Post: 

ITaskScheduler  *taskSched = NULL;
ITask           *task      = NULL;
HRESULT         hr         = S_OK;
HRESULT         taskStatus = NULL;

LPCWSTR wTaskName;
wTaskName = L"MyTestSascha";

/// Initialise COM library & obtain Task Scheduler object

hr = CoInitialize( NULL );

if ( FAILED(hr) )
{
    printf( "Failed to coinitialise hresult \n" );
    return false;
}

hr = CoCreateInstance( CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, 
                       IID_ITaskScheduler, (void**) &taskSched );

if ( FAILED(hr) )
{
    printf( "Failed to create instance \n" );
    CoUninitialize();
    return false;
}

/// Obtain task object

hr = taskSched -> Activate( wTaskName, IID_ITask, (IUnknown**) &task );
taskSched -> Release();

std::cout <<  wTaskName << std::endl;
printf( "%s \n", wTaskName );

if ( FAILED(hr) )
{
    // COR_E_FILENOTFOUND E_INVALIDARG E_OUTOFMEMORY SCHED_E_UNKNOWN_OBJECT_VERSION
    if      ( hr == 0x80070002 ) { std::cout << "The task does not exist \n"; }
    else if ( hr == 0x80000003 ) { std::cout << "The pwszName parameter is not valid \n"; }
    else if ( hr == 0x80070057 ) { std::cout << "A memory allocation failed \n"; }
    else if ( hr == 0x80041313 ) { std::cout << "The task object version is either unsupported or invalid \n"; }
    printf( "Failed retrieving task object %x \n", hr );
    CoUninitialize();
    return false;
}

I am using/learning the Windows Task Scheduler in win32 C++. I am attempting to retrieve a task object (for a task that exists) but it continually fails & returns the error COR_E_FILENOTFOUND 0x80070002 = The task does not exist

What do you think is going wrong? I know this task exists because I create it (& it does what its supposed to do, open an app at specified time).

Maybe the task name I use to retrieve the ITask object is not correct? The status (priority?) of the task I create is == 3 (if that info helps).

The code I use to create a task is exactly the same as the example code from msdn, the task name is "MyTaskSascha".

Is there anything wrong with my code to retrieve an existing task (I believe the task I am trying to retrieve is not considered "Running" but "Scheduled" so that maybe the problem?):

bool RemoveTask( std::string taskName )
{
// Post: 

ITaskScheduler  *taskSched = NULL;
ITask           *task      = NULL;
HRESULT         hr         = S_OK;
HRESULT         taskStatus = NULL;

LPCWSTR wTaskName;
wTaskName = L"MyTestSascha";

/// Initialise COM library & obtain Task Scheduler object

hr = CoInitialize( NULL );

if ( FAILED(hr) )
{
    printf( "Failed to coinitialise hresult \n" );
    return false;
}

hr = CoCreateInstance( CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, 
                       IID_ITaskScheduler, (void**) &taskSched );

if ( FAILED(hr) )
{
    printf( "Failed to create instance \n" );
    CoUninitialize();
    return false;
}

/// Obtain task object

hr = taskSched -> Activate( wTaskName, IID_ITask, (IUnknown**) &task );
taskSched -> Release();

std::cout <<  wTaskName << std::endl;
printf( "%s \n", wTaskName );

if ( FAILED(hr) )
{
    // COR_E_FILENOTFOUND E_INVALIDARG E_OUTOFMEMORY SCHED_E_UNKNOWN_OBJECT_VERSION
    if      ( hr == 0x80070002 ) { std::cout << "The task does not exist \n"; }
    else if ( hr == 0x80000003 ) { std::cout << "The pwszName parameter is not valid \n"; }
    else if ( hr == 0x80070057 ) { std::cout << "A memory allocation failed \n"; }
    else if ( hr == 0x80041313 ) { std::cout << "The task object version is either unsupported or invalid \n"; }
    printf( "Failed retrieving task object %x \n", hr );
    CoUninitialize();
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-10-18 19:34:51

您正在使用任务计划程序 2.0 API 创建任务,但尝试使用较旧的 1.0 API 访问它。

任务计划程序及其 COM API 已针对 Windows Vista 进行了重新设计,并且任务计划程序 1.0 API 无法用于访问使用新接口的任务(大概是因为它使用了非常不同的设计,允许许多新功能)。您可以创建一个向后兼容任务,可以使用此示例代码进行访问,方法是在使用 MMC 管理单元创建任务时选择“配置为:Windows Server 2003、Windows XP 或 Windows 2000”,或者可能使用旧版本创建任务API(不过,我在一个简单的测试应用程序中没有成功做到这一点)。根文件夹中的任务以这种方式工作,无论是否有前导反斜杠;子文件夹中的任务似乎根本无法访问。
如果您对与旧版 Windows 版本的兼容性不感兴趣,您可以仅使用新 API 访问该任务。

MSDN 示例< /a> 存在于两个 API 版本(注意顶部/底部分隔)和 与开发人员相关的新功能也列在那里。

You are creating the task using the Task Scheduler 2.0 API, but are trying to access it using the older 1.0 API.

The task scheduler and its COM API have been redesigned for Windows Vista, and the Task Scheduler 1.0 API cannot be used to access tasks that use the new interface (presumably because it uses a very different design that allows for a lot of new features). You can create a backward compatible task that can be accessed using this example code by selecting "Configure for: Windows Server 2003, Windows XP, or Windows 2000" when creating the task using the MMC snap-in, or presumably by creating it using the old API (I did not succeed in doing so in a simple test application, though). Tasks in the root folder work for me this way with or without the leading backslash; tasks in sub-folders do not seem to be accessible at all.
If you are not interested in compatibility with older Windows versions, you could just access the task using the new API.

MSDN examples exist for both API versions (note the top/bottom separation) and the new features relevant for developers are also listed there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文