天天看點

C/C++ 周遊驅動清單(應用層下)

實作代碼:

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

代碼基本上每一句都做了注釋,應該蠻好了解的,效果圖如下:

C/C++ 周遊驅動清單(應用層下)

最後感謝JoyChou老哥提供的思路:https://www.52pojie.cn/thread-243050-1-1.html

文章出處:

https://www.cnblogs.com/LyShark/p/15019710.html

版權聲明:

本部落格文章與代碼均為學習時整理的筆記,部落格中除去明确标注有參考文獻的文章,其他文章

[均為原創]

作品,轉載請

[添加出處]

,您添加出處是我創作的動力!

如果您惡意轉載本人文章并被本人發現,則您的整站文章,将會變為我的原創作品,請互相尊重 !