Přístup ke vzdáleným počítačům
Rozhraní API protokolu událostí systému Windows můžete použít k přístupu k datům na místním počítači nebo na vzdáleném počítači. Pokud chcete získat přístup k datům na vzdáleném počítači, musíte volat funkci EvtOpenSession a vytvořit tak kontext vzdálené relace. Při volání této funkce zadáte název vzdáleného počítače, ke kterému se chcete připojit, přihlašovací údaje uživatele, které chcete použít k vytvoření připojení, a typ ověřování, který se má použít k ověření uživatele. Pokud chcete určit aktuálního uživatele, nastavte členy Domény, Uživatel a Heslo na NULL.
Při volání rozhraní API protokolu událostí systému Windows předáte popisovač vzdálené relaci kontextu, který vrátí funkce EvtOpenSession. (Pokud chcete získat přístup k datům v místním počítači, předejte NULL a zadejte výchozí relaci.) Pro přístup k datům ve vzdáleném počítači musí vzdálený počítač povolit výjimku brány Windows Firewall vzdálené správy protokolu událostí, jinak se při pokusu o použití popisovače relace volání zobrazí chyba s RPC_S_SERVER_UNAVAILABLE. Počítač, ke kterému se připojujete, musí mít systém Windows Vista nebo novější.
Následující příklad ukazuje, jak se připojit ke vzdálenému počítači.
#include <windows.h>
#include <stdio.h>
#include <winevt.h>
#pragma comment(lib, "wevtapi.lib")
EVT_HANDLE ConnectToRemote(LPWSTR lpwszRemote);
void EnumProviders(EVT_HANDLE hRemote);
void main(void)
{
EVT_HANDLE hRemote = NULL;
LPWSTR pwsComputerName = L"<name of the remote computer goes here>";
// Enumerate the registered providers on the local computer.
wprintf(L"Registered providers on the local computer\n\n");
EnumProviders(hRemote);
hRemote = ConnectToRemote(pwsComputerName);
if (NULL == hRemote)
{
wprintf(L"Failed to connect to remote computer. Error code is %d.\n", GetLastError());
goto cleanup;
}
// Enumerate the registered providers on the remote computer.
// To access a remote computer, the remote computer must enable
// Remote Event Log Management as an exception in the firewall;
// otherwise, the remote call fails with RPC_S_SERVER_UNAVAILABLE.
wprintf(L"\n\nRegistered providers on the remote computer\n\n");
EnumProviders(hRemote);
cleanup:
if (hRemote)
EvtClose(hRemote);
}
// Create a session context for the remote computer. Set the
// Domain, User, and Password member to NULL to specify
// the current user.
EVT_HANDLE ConnectToRemote(LPWSTR lpwszRemote)
{
EVT_HANDLE hRemote = NULL;
EVT_RPC_LOGIN Credentials;
RtlZeroMemory(&Credentials, sizeof(EVT_RPC_LOGIN));
Credentials.Server = lpwszRemote;
Credentials.Domain = NULL;
Credentials.User = NULL;
Credentials.Password = NULL;
Credentials.Flags = EvtRpcLoginAuthNegotiate;
// This call creates a remote session context; it does not actually
// create a connection to the remote computer. The connection to
// the remote computer happens when you use the context.
hRemote = EvtOpenSession(EvtRpcLogin, &Credentials, 0, 0);
SecureZeroMemory(&Credentials, sizeof(EVT_RPC_LOGIN));
return hRemote;
}
// Enumerate the registered providers on the computer.
void EnumProviders(EVT_HANDLE hRemote)
{
EVT_HANDLE hPublishers = NULL;
WCHAR wszPublisherName[255 + 1];
DWORD dwBufferUsed = 0;
DWORD status = ERROR_SUCCESS;
hPublishers = EvtOpenPublisherEnum(hRemote, 0);
if (NULL == hPublishers)
{
wprintf(L"EvtOpenPublisherEnum failed with %d\n", GetLastError());
goto cleanup;
}
while (true)
{
if (EvtNextPublisherId(hPublishers, sizeof(wszPublisherName)/sizeof(WCHAR), wszPublisherName, &dwBufferUsed))
{
wprintf(L"%s\n", wszPublisherName);
}
else
{
status = GetLastError();
if (ERROR_NO_MORE_ITEMS == status)
{
break;
}
else
{
wprintf(L"EvtNextPublisherId failed with 0x%ud\n", GetLastError());
}
}
}
cleanup:
if (hPublishers)
EvtClose(hPublishers);
}