https://bbs.pediy.com/thread-77339.htm
参考资料
直接源码 , 基于 C C++ 。代码检测枚举 查找 驱动 列表
//.h
#pragma once
#include "stdafx.h" //MFC , 可以删。
#define MAXNUM 255
#define UNICODE // to support chinese
#include <windows.h>
#include <psapi.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib,"psapi.lib")
using namespace std;
typedef struct _DRIVER_INFO {
WCHAR DriverName[MAX_PATH];
WCHAR BaseName[MAX_PATH];
WCHAR FileName[MAX_PATH];
DWORD BaseAddr;
} DRIVER_INFO,*PDRIVER_INFO ;
typedef struct _ALL_DRIVER_INFO {
DWORD cbNum;
DRIVER_INFO DrvInfo[1]; // define variable length structure
} ALL_DRIVER_INFO,*PALL_DRIVER_INFO;
class CMyDriver{
private:
PALL_DRIVER_INFO m_pDriversInfo;
void ClearDriverInfo();
void GetDriverInfo();
BOOLEAN m_bDriver_Info_Status;
public:
CMyDriver();
void FlushDriversInfo();
//枚举驱动列表。
void ShowDriversInfo();
//查询指定驱动,存在返回1,不存在返回0,异常返回-1;
int DriverExists(WCHAR* wszDriverName);
~CMyDriver();
CMyDriver& operator=(const CMyDriver&);
};
//CPP
#include "stdafx.h"
#include "MyDriver.h"
CMyDriver::CMyDriver()
{
GetDriverInfo();
}
void CMyDriver::ShowDriversInfo()
{
for(int i = 0 ; i < m_pDriversInfo->cbNum ; i ++)
{
wprintf(L"%d\t%8X\t%s\t%s\n",
i,
m_pDriversInfo->DrvInfo[i].BaseAddr,
m_pDriversInfo->DrvInfo[i].BaseName,
m_pDriversInfo->DrvInfo[i].FileName
);
}
return ;
}
void CMyDriver::ClearDriverInfo()
{
GlobalFree(m_pDriversInfo);
m_pDriversInfo = NULL;
}
void CMyDriver::GetDriverInfo()
{
DWORD cbNum = 0;
PDWORD pBaseAddr = NULL;
pBaseAddr = (PDWORD)GlobalAlloc(GMEM_FIXED,sizeof(DWORD)*MAXNUM);
if( EnumDeviceDrivers ( (LPVOID*)pBaseAddr,sizeof(DWORD)*MAXNUM,&cbNum ) != TRUE )
{
wprintf( L"EnumDeviceDriver failed! ErrorCode = %8x\n", GetLastError() );
m_bDriver_Info_Status = FALSE;
GlobalFree(pBaseAddr);
pBaseAddr = NULL;
return ;
}else
{
m_bDriver_Info_Status = TRUE;
}
cbNum /= 4;
m_pDriversInfo = (PALL_DRIVER_INFO)GlobalAlloc( GMEM_FIXED,sizeof(DRIVER_INFO)*cbNum + 4 );
m_pDriversInfo -> cbNum = cbNum;
PDWORD pBaseAddrBegin = pBaseAddr;
for(int i = 0 ; i < cbNum ; i ++)
{
m_pDriversInfo->DrvInfo[i].BaseAddr = *pBaseAddr;
GetDeviceDriverBaseName( (LPVOID)*pBaseAddr,(LPWSTR)&m_pDriversInfo->DrvInfo[i].BaseName,MAX_PATH );
GetDeviceDriverFileName( (LPVOID)*pBaseAddr,(LPWSTR)&m_pDriversInfo->DrvInfo[i].FileName,MAX_PATH );
pBaseAddr ++;
}
GlobalFree(pBaseAddrBegin);
pBaseAddrBegin = NULL;
return ;
}
void CMyDriver::FlushDriversInfo()
{
ClearDriverInfo();
GetDriverInfo();
}
CMyDriver::~CMyDriver()
{
ClearDriverInfo();
}
int CMyDriver::DriverExists( WCHAR* wszDriverName )
{
if(m_bDriver_Info_Status == FALSE)
{
return -1;
}
for(int i = 0 ; i < m_pDriversInfo -> cbNum ; i ++)
{
if(wcscmp(m_pDriversInfo->DrvInfo[i].BaseName,wszDriverName) == 0)
{
return 1;
}
}
return 0;
}