访问读取冲突 C++ 任务调度程序
Acces read violation c++ task scheduler
我正在尝试删除名为 MicrosoftInputProcessor 的计划任务,但我得到:
抛出未处理的异常:读取访问冲突。
pRootFolder->**** 是 0x4D005C。
有什么帮助吗?
我认为这是因为 CoInitializeSecurity 中的 NULL ptr 但我不知道该怎么做。
PS: 我正在以管理员身份打开 VS c++。
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <iaccess.h>
#include <comdef.h>
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
printf("\nCoInitializeEx failed: %x", hr);
return 1;
}
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;
}
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;
}
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;
}
ITaskFolder* pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\"), &pRootFolder);
pService->Release();
if (FAILED(hr))
{
printf("Cannot get Root Folder pointer: %x", hr);
CoUninitialize();
return 1;
}
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks(NULL, &pTaskCollection);
pRootFolder->Release();
if (FAILED(hr))
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
if (numTasks == 0)
{
printf("\nNo Tasks are currently running");
pTaskCollection->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of Tasks : %d", numTasks);
TASK_STATE taskState;
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
if (wcscmp(taskName, L"MicrosoftInputProcessor") == 0)
{
hr = pRootFolder->DeleteTask(taskName, 0);
if (SUCCEEDED(hr))
{
wcout << "\nSucceded deletion of " << taskName << endl;
}
else
{
wcout << "\nFailure deletion of " << taskName << endl;
}
}
SysFreeString(taskName);
hr = pRegisteredTask->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);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}
pTaskCollection->Release();
CoUninitialize();
return 0;
}
您在 Release
d pRootFolder
指向的对象之后调用 pRootFoolder->DeleteTask
。在循环后将您的呼叫移至 pRootFolder->Release()
。
我正在尝试删除名为 MicrosoftInputProcessor 的计划任务,但我得到: 抛出未处理的异常:读取访问冲突。 pRootFolder->**** 是 0x4D005C。 有什么帮助吗? 我认为这是因为 CoInitializeSecurity 中的 NULL ptr 但我不知道该怎么做。 PS: 我正在以管理员身份打开 VS c++。
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <iaccess.h>
#include <comdef.h>
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
printf("\nCoInitializeEx failed: %x", hr);
return 1;
}
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;
}
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;
}
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;
}
ITaskFolder* pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\"), &pRootFolder);
pService->Release();
if (FAILED(hr))
{
printf("Cannot get Root Folder pointer: %x", hr);
CoUninitialize();
return 1;
}
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks(NULL, &pTaskCollection);
pRootFolder->Release();
if (FAILED(hr))
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
if (numTasks == 0)
{
printf("\nNo Tasks are currently running");
pTaskCollection->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of Tasks : %d", numTasks);
TASK_STATE taskState;
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
if (wcscmp(taskName, L"MicrosoftInputProcessor") == 0)
{
hr = pRootFolder->DeleteTask(taskName, 0);
if (SUCCEEDED(hr))
{
wcout << "\nSucceded deletion of " << taskName << endl;
}
else
{
wcout << "\nFailure deletion of " << taskName << endl;
}
}
SysFreeString(taskName);
hr = pRegisteredTask->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);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}
pTaskCollection->Release();
CoUninitialize();
return 0;
}
您在 Release
d pRootFolder
指向的对象之后调用 pRootFoolder->DeleteTask
。在循环后将您的呼叫移至 pRootFolder->Release()
。