diff --git a/CMakeLists.txt b/CMakeLists.txt index 77ecb60..741d4e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,4 @@ find_package(webview REQUIRED) add_executable(${PROJECT_NAME} ${SRC_CPP}) target_link_libraries(${PROJECT_NAME} PRIVATE webview::core Wevtapi) +target_link_options(${PROJECT_NAME} PRIVATE -static) diff --git a/src/main.cpp b/src/main.cpp index 294989d..7bb3567 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,244 @@ -int main(){ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// #pragma comment(lib, "wevtapi.lib") + +#define ARRAY_SIZE 10 +#define TIMEOUT 1000 // 1 second; Set and use in place of INFINITE in EvtNext call + +DWORD PrintResults(EVT_HANDLE hResults); +DWORD PrintEvent(EVT_HANDLE hEvent); // Shown in the Rendering Events topic + +#include +#include +#include +#include +#include +#include + +#pragma comment(lib, "wevtapi.lib") + +#define ARRAY_SIZE 10 +#define TIMEOUT 1000 // 1 second + +DWORD PrintResults(EVT_HANDLE hResults); +DWORD PrintEvent(EVT_HANDLE hEvent); + +#include +#include +#include +#include +#include +#include + +#pragma comment(lib, "wevtapi.lib") + +#define ARRAY_SIZE 10 +#define TIMEOUT 1000 // 1 second + +DWORD PrintResults(EVT_HANDLE hResults); +DWORD PrintEvent(EVT_HANDLE hEvent); + +// 检查是否以管理员权限运行 +bool IsAdmin() { + BOOL isAdmin = FALSE; + SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY; + PSID adminGroup; + if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) { + if (!CheckTokenMembership(NULL, adminGroup, &isAdmin)) { + isAdmin = FALSE; + } + FreeSid(adminGroup); + } + return isAdmin; +} + +// 获取错误消息字符串 +std::wstring GetErrorMessage(DWORD errorCode) { + LPWSTR errorMsg = NULL; + DWORD size = FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&errorMsg, 0, NULL + ); + + std::wstring message = (size > 0) ? std::wstring(errorMsg, size) : L"未知错误"; + if (errorMsg) LocalFree(errorMsg); + return message; +} + +int main(void) +{ + // 设置控制台为UTF-8模式,解决中文显示问题 + SetConsoleOutputCP(CP_UTF8); + + // 检查管理员权限 + if (!IsAdmin()) { + std::wcout << L"警告: 程序未以管理员权限运行,可能无法访问系统事件日志。\n"; + std::wcout << L"请右键点击程序并选择'以管理员身份运行'。\n\n"; + } + + DWORD status = ERROR_SUCCESS; + EVT_HANDLE hResults = NULL; + WCHAR pwsPath[MAX_PATH]; + LPWSTR pwsQuery = L"*"; + + // 展开环境变量 + ExpandEnvironmentStringsW( + L"%SystemRoot%\\System32\\Winevt\\Logs\\Microsoft-Windows-TerminalServices-RemoteConnectionManager%4Operational.evtx", + pwsPath, + MAX_PATH + ); + + std::wcout << L"尝试打开事件日志文件: " << pwsPath << L"\n"; + + // 使用EvtQueryFilePath标志查询文件 + hResults = EvtQuery(NULL, pwsPath, pwsQuery, EvtQueryFilePath | EvtQueryReverseDirection); + if (NULL == hResults) + { + status = GetLastError(); // 获取错误代码 + + std::wcout << L"EvtQuery失败,错误代码: " << status << L"\n"; + std::wcout << L"错误描述: " << GetErrorMessage(status) << L"\n"; + + // 获取扩展错误信息 + DWORD extendedStatusSize = 0; + EvtGetExtendedStatus(NULL, NULL, &extendedStatusSize); + + if (extendedStatusSize > 0) + { + std::wstring extendedStatus; + extendedStatus.resize(extendedStatusSize); + + if (EvtGetExtendedStatus( + extendedStatusSize, + &extendedStatus[0], + &extendedStatusSize + )) + { + std::wcout << L"扩展错误信息: " << extendedStatus << L"\n"; + } + } + + goto cleanup; + } + + std::wcout << L"成功打开事件日志文件\n"; + std::wcout << L"开始查询事件...\n"; + + if (ERROR_SUCCESS == PrintResults(hResults)) { + std::wcout << L"查询完成\n"; + } else { + std::wcout << L"查询过程中发生错误\n"; + } + +cleanup: + if (hResults) + EvtClose(hResults); + + std::wcout << L"程序已完成\n"; + system("pause"); // 保持控制台窗口打开 + std::wcout< os("log.txt"); + os<