从收集器发起的订阅中删除事件源
可以从收集器发起的订阅中删除事件源,而无需删除整个订阅。 必须知道要删除的事件源的地址。 可以使用显示事件收集器订阅的属性中显示的 C++ 示例查找与订阅关联的事件源的地址,也可以在命令提示符处键入以下命令:
wecutil gs SubscriptionName
若要列出本地计算机上的当前订阅,可以使用列出事件收集器订阅中显示的 C++ 代码示例,也可以在命令提示符处键入以下命令:
wecutil es
注意
可以使用此示例从收集器发起的订阅中删除事件源,也可以在命令提示符处键入以下命令:
wecutil ss SubscriptionName **/esa:**EventSourceAddress /res
EventSourceAddress 可以是本地计算机的 localhost,也可以是远程计算机的完全限定域名。
此示例遵循一系列步骤,将事件源从收集器发起的订阅中删除。
从收集器发起的订阅中删除事件源
- 通过将订阅名称和访问权限作为参数提供给 EcOpenSubscription 函数来打开现有订阅。 有关访问权限的详细信息,请参阅 Windows 事件收集器常量。
- 通过调用 EcGetSubscriptionProperty 函数获取订阅的事件源数组。 有关可检索的订阅属性的详细信息,请参阅 EC_SUBSCRIPTION_PROPERTY_ID 枚举。
- 通过调用 EcGetObjectArrayProperty 函数在订阅的事件源数组中搜索指定的事件源。 EcSubscriptionEventSourceAddress 属性的值将是本地计算机的 Localhost,或者是远程计算机的完全限定域名。 有关可检索的事件源属性的详细信息,请参阅 EC_SUBSCRIPTION_PROPERTY_ID 枚举。
- 通过调用 EcRemoveObjectArrayElement 函数从订阅中删除事件源。
- 通过调用 EcSaveSubscription 函数保存订阅。
- 通过调用 EcClose 函数关闭订阅。
以下 C++ 代码示例显示了如何从事件收集器订阅中删除事件源。
#include <windows.h>
#include <EvColl.h>
#include <vector>
#include <string>
#include <strsafe.h>
#pragma comment(lib, "wecapi.lib")
// Subscription Information
DWORD GetSubscriptionProperty(EC_HANDLE hSubscription,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty);
DWORD GetEventSourceProperty(EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD arrayIndex,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProper);
void __cdecl wmain()
{
EC_HANDLE hSubscription;
std::wstring eventSource = L"localhost";
BOOL foundEventSource = false;
LPCWSTR lpSubname = L"TestSubscription-CollectorInitiated";
DWORD dwEventSourceCount;
DWORD deleteEvent = 0;
DWORD dwRetVal = ERROR_SUCCESS;
PEC_VARIANT vProperty = NULL;
std::vector<BYTE> buffer;
LPVOID lpwszBuffer;
EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray = NULL;
// Step 1: Open an existing subscription.
hSubscription = EcOpenSubscription(lpSubname,
EC_READ_ACCESS | EC_WRITE_ACCESS,
EC_OPEN_EXISTING);
if (!hSubscription)
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 2: Get the event sources array to remove an event
// source from the subscription.
dwRetVal = GetSubscriptionProperty(hSubscription,
EcSubscriptionEventSources,
0,
buffer,
vProperty);
if (ERROR_SUCCESS != dwRetVal)
{
goto Cleanup;
}
// Ensure that a handle to the event sources array has been obtained.
if (vProperty->Type != EcVarTypeNull &&
vProperty->Type != EcVarObjectArrayPropertyHandle)
{
dwRetVal = ERROR_INVALID_DATA;
goto Cleanup;
}
hArray = (vProperty->Type == EcVarTypeNull)? NULL:
vProperty->PropertyHandleVal;
if (!hArray)
{
dwRetVal = ERROR_INVALID_DATA;
goto Cleanup;
}
if (!EcGetObjectArraySize(hArray, &dwEventSourceCount))
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 3: Search for the specified event source in the event source array.
for (DWORD I = 0; I < dwEventSourceCount; I++)
{
dwRetVal = GetEventSourceProperty(hArray,
EcSubscriptionEventSourceAddress,
I,
0,
buffer,
vProperty);
if (ERROR_SUCCESS != dwRetVal)
{
goto Cleanup;
}
if (vProperty->StringVal == eventSource)
{
foundEventSource = true;
deleteEvent = I;
break;
}
}
// Step 4: If the event source was found in the array, remove it.
if (foundEventSource)
{
if (!EcRemoveObjectArrayElement(hArray, deleteEvent))
{
dwRetVal = GetLastError();
goto Cleanup;
}
}
else
{
wprintf(L"Could not remove the event source from the subscription.\n");
goto Cleanup;
}
// Step 5: Save the subscription to finalize the removal of the event source.
if( !EcSaveSubscription(hSubscription, NULL) )
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 6: Close the subscription.
Cleanup:
if (hArray)
EcClose(hArray);
if (hSubscription)
EcClose(hSubscription);
if (dwRetVal != ERROR_SUCCESS)
{
FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwRetVal,
0,
(LPWSTR) &lpwszBuffer,
0,
NULL);
if (!lpwszBuffer)
{
wprintf(L"Failed to FormatMessage. Operation Error Code: %u." \
L"Error Code from FormatMessage: %u\n", dwRetVal, GetLastError());
return;
}
wprintf(L"\nFailed to Perform Operation.\nError Code: %u\n" \
L"Error Message: %s\n", dwRetVal, lpwszBuffer);
LocalFree(lpwszBuffer);
}
}
DWORD GetSubscriptionProperty(EC_HANDLE hSubscription,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty)
{
DWORD dwBufferSize, dwRetVal = ERROR_SUCCESS;
buffer.resize(sizeof(EC_VARIANT));
if (!hSubscription)
return ERROR_INVALID_PARAMETER;
// Get the value for the specified property.
if (!EcGetSubscriptionProperty(hSubscription,
propID,
flags,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwRetVal)
{
dwRetVal = ERROR_SUCCESS;
buffer.resize(dwBufferSize);
if (!EcGetSubscriptionProperty(hSubscription,
propID,
flags,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
}
}
}
if (dwRetVal == ERROR_SUCCESS)
{
vProperty = (PEC_VARIANT) &buffer[0];
}
else
{
vProperty = NULL;
}
return dwRetVal;
}
DWORD GetEventSourceProperty(EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD arrayIndex,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty)
{
UNREFERENCED_PARAMETER(flags);
UNREFERENCED_PARAMETER(propID);
DWORD dwBufferSize, dwRetVal = ERROR_SUCCESS;
buffer.resize(sizeof(EC_VARIANT));
if (!hArray)
return ERROR_INVALID_PARAMETER;
// Obtain the value for the specified property.
if (!EcGetObjectArrayProperty(hArray,
EcSubscriptionEventSourceAddress,
arrayIndex,
0,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwRetVal)
{
dwRetVal = ERROR_SUCCESS;
buffer.resize(dwBufferSize);
if (!EcGetObjectArrayProperty(hArray,
EcSubscriptionEventSourceAddress,
arrayIndex,
0,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
}
}
}
if (dwRetVal == ERROR_SUCCESS)
{
vProperty = (PEC_VARIANT) &buffer[0];
}
else
{
vProperty = NULL;
}
return dwRetVal;
}
相关主题