Bagikan melalui


Mengakses Komputer Jarak Jauh

Anda dapat menggunakan API Log Peristiwa Windows untuk mengakses data di komputer lokal atau di komputer jarak jauh. Untuk mengakses data di komputer jarak jauh, Anda perlu memanggil fungsi EvtOpenSession untuk membuat konteks sesi jarak jauh. Ketika Anda memanggil fungsi ini, Anda menentukan nama komputer jarak jauh yang ingin Anda sambungkan, kredensial pengguna yang akan digunakan untuk membuat koneksi, dan jenis autentikasi yang akan digunakan untuk mengautentikasi pengguna. Untuk menentukan pengguna saat ini, atur anggota Domain, Pengguna, dan Kata Sandi ke NULL.

Saat Anda memanggil Windows Event Log API, Anda meneruskan handel ke konteks sesi jarak jauh yang dikembalikan fungsi EvtOpenSession . (Untuk mengakses data di komputer lokal, lewati NULL untuk menentukan sesi default.) Untuk mengakses data pada komputer jarak jauh, komputer jarak jauh harus mengaktifkan pengecualian Firewall Windows "Manajemen Log Peristiwa Jarak Jauh"; jika tidak, ketika Anda mencoba menggunakan handel sesi, panggilan akan bermasalah dengan RPC_S_SERVER_UNAVAILABLE. Komputer yang Anda sambungkan harus menjalankan Windows Vista atau yang lebih baru.

Contoh berikut menunjukkan cara menyambungkan ke komputer jarak jauh.

#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 conext 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);
}