zl程序教程

您现在的位置是:首页 >  其他

当前栏目

C/C++ 应用层下遍历驱动列表

2023-02-18 16:46:18 时间

实现在应用层下遍历输出驱动文件路径列表信息。

实现代码:

#include <stdio.h>
#include <windows.h>
#include <Psapi.h>
#include <shlwapi.h>  //PathFileExists
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "shlwapi.lib")
#define  ARRAY_SIZE 1024

int _tmain(int argc, _TCHAR* argv[]){
	DWORD cbNeeded = 0; // drivers[] 返回的字节数
    LPVOID drivers[ARRAY_SIZE] = {0}; // 驱动程序地址列表数组
    int cDrivers = 0;	// 驱动个数
    if (EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) && cbNeeded < sizeof(drivers)) // EnumDeviceDrivers 检索每个驱动文件的加载地址
    {
        char szDriver[ARRAY_SIZE] = {0};	// 驱动文件名
        char szPath[ARRAY_SIZE] = {0};	// 存放驱动文件全路径
        char szSystemPath[ARRAY_SIZE] = {0}; // 存放 system32 文件夹路径
        cDrivers = cbNeeded / sizeof(LPVOID);	// 驱动个数
        
		//得到C:\Windows\system32\dbghelp.dll
        GetSystemDirectory(szSystemPath, sizeof(szSystemPath));
        strcat_s(szSystemPath, "\\dbghelp.dll");

        for (int i = 0; i < cDrivers; i++)
        {
            if (GetDeviceDriverBaseName(drivers[i], szDriver, sizeof(szDriver) / sizeof(LPVOID)))
            {
            	// 打印驱动名
				printf("【%d】:%s\n", i+1, szDriver);

				// 打印驱动文件路径
				//GetDeviceDriverFileName(drivers[i], szPath, sizeof(szPath));
				//printf("%s\n", szPath);
            }
        }
    }

	getchar();
	return 0;
}

代码基本上每一句都做了注释,应该蛮好理解的,效果图如下: