#include #include void MyBackupEventLog(const wchar_t* logName, const wchar_t* backupFilePath) { HANDLE hEventLog = NULL; HANDLE hBackupFile = NULL; char buffer[4096]; DWORD bytesRead, bytesWritten; BOOL success = FALSE; // イベントログを開く hEventLog = OpenEventLog(NULL, logName); if (hEventLog == NULL) { wprintf(L"イベントログのオープンに失敗しました: %lu\n", GetLastError()); return; } // バックアップファイルを作成 hBackupFile = CreateFile(backupFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hBackupFile == INVALID_HANDLE_VALUE) { wprintf(L"バックアップファイルの作成に失敗しました: %lu\n", GetLastError()); CloseEventLog(hEventLog); return; } // イベントログを読み取りながらバックアップファイルに書き込む while (ReadEventLog(hEventLog, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, buffer, sizeof(buffer), &bytesRead, &bytesWritten)) { if (bytesWritten > 0) { if (!WriteFile(hBackupFile, buffer, bytesWritten, &bytesWritten, NULL)) { wprintf(L"バックアップファイルへの書き込みに失敗しました: %lu\n", GetLastError()); break; } } if (bytesRead < sizeof(buffer)) { // イベントログの末尾に達したためループを終了 break; } } // エラーチェック if (!success) { wprintf(L"イベントログのバックアップに失敗しました: %lu\n", GetLastError()); } else { wprintf(L"イベントログのバックアップが完了しました\n"); } // ハンドルをクローズ if (hEventLog != NULL) { CloseEventLog(hEventLog); } if (hBackupFile != NULL && hBackupFile != INVALID_HANDLE_VALUE) { CloseHandle(hBackupFile); } } int main() { const wchar_t* logName = L"Application"; // バックアップするイベントログの名前 const wchar_t* backupFilePath = L"EventLogBackup.evtx"; // バックアップファイルの保存先パス MyBackupEventLog(logName, backupFilePath); return 0; }