使用 C++ 操作命令行,并接收命令行返回信息,通过 Dos 命令获取 Windows 系统日志。
Wevtutil 命令介绍
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc732848%28v=ws.11%29
文档上介绍的很全,我就不一一列举了,这里只说下我用到的一行命令:

意思就是打印最近三条系统日志
下面是接收命令行返回信息的代码:
#include "stdafx.h" #include <iostream>
using namespace std;
int execmd(char* cmd, char* result) { char buffer[128]; FILE* pipe = _popen(cmd, "r"); if (!pipe){return 0;}
while(!feof(pipe)){ if (fgets(buffer, 128, pipe)) { strcat(result, buffer); } }
_pclose(pipe);
return 1; }
int main() { char result[0x7ffff] = ""; if (execmd("wevtutil qe System /c:300 /rd:true /f:text", result) == 1) { cout << result << endl; }
string s = result; while ((s.find("igfx")) != -1) { cout << "找到了 igfx " << endl; break; }
system("pause"); return 0; }
|
效果图:
