文档化ring3 api列举驱动列表 --- 做了一些重构。(解决内存泄漏问题)

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

 

转载于:https://www.cnblogs.com/clover-xuqi/p/8005852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值