Saturday, September 24, 2011

[Text Vesion]Document-ize of ring3 api list diriver listing

Author:benyanwk
Code:
/*++
Module Name:
  ListDrvCon.cpp

Enviroment:
  All Windows NT Platfrom;Console

Abstract:
  List all the driver's name & baseaddr & fileaddr

Note:
  Using documented API in psapi.h

Revision:
  23-Nov-2008 Created.

--*/

//
// directives
//
#define MAXNUM 255
#define UNICODE  // to support chinese
#include <windows.h>
#include <psapi.h>
#include <stdio.h>
#pragma comment(lib,"psapi.lib")

//
// strcut defintion
//

typedef struct _DRIVER_INFO {
  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;

//
// function declaration
//
int main();
int myGetDriverInfo(
  IN PVOID &pDrvInfo,
  IN BOOLEAN bAlloc
  );

//
// function definition
//
int myGetDriverInfo(
  IN PALL_DRIVER_INFO* pDrvInfo, 
  IN BOOLEAN bAlloc
  )
{
/*++
Arguments:
  pDrvInfo-->the buffer to store the driver information
  bAlloc-->alloc the global memory by the function or not
  return 0 indicate success otherwise error
--*/
  DWORD cbNum = 0;
  PDWORD pBaseAddr = NULL ;
  PWCHAR pFileName = NULL;
  PWCHAR pBaseName = NULL;
  PALL_DRIVER_INFO pAllDrvInfo;

   
  pBaseAddr = (PDWORD)GlobalAlloc( GMEM_FIXED, sizeof(DWORD)*MAXNUM );


  if( EnumDeviceDrivers( (LPVOID*)pBaseAddr, sizeof(DWORD)*MAXNUM, &cbNum ) != TRUE )
  {
    //
    // indicate EnumDeviceDriver failed!
    //
    wprintf( L"EnumDeviceDriver failed! ErrorCode = %8x\n", GetLastError() );
    return 1;
  }
 
  cbNum /=4; // cbNum return the bytes

  if( bAlloc == TRUE )
    pAllDrvInfo = (PALL_DRIVER_INFO)GlobalAlloc( GMEM_FIXED, sizeof(DRIVER_INFO)*cbNum + 4 );
  else
    pAllDrvInfo = (PALL_DRIVER_INFO)pDrvInfo;

  pAllDrvInfo->cbNum = cbNum;

  for( int i = 0; i < cbNum; i++ )
  {
    pAllDrvInfo->DrvInfo[i].BaseAddr = *pBaseAddr;

    GetDeviceDriverBaseName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].BaseName, MAX_PATH );
    GetDeviceDriverFileName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].FileName, MAX_PATH );

    pBaseAddr++;
  }

  *pDrvInfo = pAllDrvInfo; // return the drv info buffer pointer
  return 0;  // indicate success
}

int main()
{
  PALL_DRIVER_INFO pDrvInfo = NULL;
  myGetDriverInfo(
    (PALL_DRIVER_INFO*)&pDrvInfo,
    TRUE
    );
  wprintf(L"=====the list of driver as follows === \n");
  wprintf(L"Order\tBaseAddr \t BaseName \t FileName \t \n");
  for( int i = 0; i<pDrvInfo->cbNum; i++ )
  {
    wprintf(L"%d\t%8x\t%s\t%s\n",
        i,
        pDrvInfo->DrvInfo[i].BaseAddr,
        pDrvInfo->DrvInfo[i].BaseName,
        pDrvInfo->DrvInfo[i].FileName
        );
  }
  return 0;
}

No comments:

Post a Comment