C/C++ 查询系统日志

使用 C++ 操作命令行,并接收命令行返回信息,通过 Dos 命令获取 Windows 系统日志。

Wevtutil 命令介绍

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc732848(v=ws.11)

文档上介绍的很全,我就不一一列举了,这里只说下我用到的一行命令:

意思就是打印最近三条系统日志

下面是接收命令行返回信息的代码:

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

	// 检测管道中的结束符,0表示没有结束
	while(!feof(pipe)){
		// 从管道中读取数据
		if (fgets(buffer, 128, pipe)) {             
			// 拼接 char
			strcat(result, buffer);
		}
	}

	//关闭管道 
	_pclose(pipe);           

	return 1;                                 
}

int main()
{
	char result[0x7ffff] = "";        // 存放结果
	
	// 获取命令行返回值(保险起见这里获取 300 条日志信息)
	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;
}

效果图:

posted @ 2021-07-16 14:26  lyshark  阅读(565)  评论(0编辑  收藏  举报

loading... | loading...
博客园 - 开发者的网上家园