代码:
/*++
Module Name:
ListDrvCon.cpp
Enviroment:
All Windows NT Platfrom;GUI
Abstract:
List all the driver's name & baseaddr & fileaddr
Note:
(1) Using documented API in psapi and
ListView control to show the result
(2) Wanted upgraded:
+ ListView control sort functionality(IMPORTANT)
+ Item should can be selected
+ Support the right click menu
Revision:
23-Nov-2008 Created.
Author:
benyanwk
--*/
//////////////////////////////////////////////////
// directives
/////////////////////////////////////////////////
#define MAXNUM 255
#define UNICODE // to support chinese
#include <windows.h>
#include <commctrl.h>
#include <psapi.h>
#include <stdio.h>
#define C_COLUMNS 4 // 4 columns to be added
#include "resource.h" // the resource file
#pragma comment(lib,"psapi.lib")
#pragma comment(lib,"comctl32.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 cnNum;
DRIVER_INFO DrvInfo[1]; // define variable length structure
} ALL_DRIVER_INFO,*PALL_DRIVER_INFO;
////////////////////////////////////////////////
// function declaration
///////////////////////////////////////////////
INT_PTR CALLBACK DlgProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);
int myGetDriverInfo(
IN PVOID &pDrvInfo,
IN BOOLEAN bAlloc
);
BOOLEAN SetListView(
IN HWND hListView
);
BOOLEAN InitListView(
IN HWND hListView
);
//////////////////////////////////////////////
// Global variable declaration
////////////////////////////////////////////
PALL_DRIVER_INFO g_pDrvInfo = NULL; // the global variable used to store the driver information
HWND g_hInst = NULL;
HWND g_hListView = NULL;
///////////////////////////////////////////////
// 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 = NULL;
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->cnNum = 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++;
}
GlobalFree( pBaseAddr );
*pDrvInfo = pAllDrvInfo; // return the drv info buffer pointer
return 0; // indicate success
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInst = (HWND)GetModuleHandle( NULL );
InitCommonControls();
//
// Create the main dialog
//
DialogBoxParam(
(HINSTANCE)g_hInst, // hinstance
MAKEINTRESOURCE(IDD_DIALOGBAR), // temple name
NULL, // parent hwnd
DlgProc, // dialog functoin
NULL // initialize parameter
);
ExitProcess(0);
}
INT_PTR CALLBACK DlgProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
RECT rect;
HWND hwndOwner;
RECT rc,rcDlg,rcOwner;
int iSelCol;
HICON hIcon;
switch( uMsg )
{
case WM_CLOSE:
GlobalFree( g_pDrvInfo);
EndDialog( hwndDlg, NULL );
break;
case WM_INITDIALOG:
//
// set the icon
//
hIcon = LoadIcon( (HINSTANCE)g_hInst, (LPCWSTR)IDI_ICON1 );
SendMessage( hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
SendMessage( hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon );
//
// centre the dialog
//
g_hListView = GetDlgItem( hwndDlg, IDC_LIST2 );
hwndOwner = GetDesktopWindow();
GetWindowRect( hwndOwner, &rcOwner );
GetWindowRect( hwndDlg, &rcDlg );
CopyRect( &rc, &rcOwner );
// Offset the owner and dialog box rectangle so that
// right and bottom values represent the width and
// height, and then offset the owner again to discard
// space taken up by the dialog
OffsetRect( &rcDlg, -rcDlg.left, -rcDlg.top );
OffsetRect( &rc, -rc.left, -rc.top );
OffsetRect( &rc, -rcDlg.right, -rcDlg.bottom );
SetWindowPos(
hwndDlg,
HWND_TOP,
rcOwner.left + ( rc.right / 2 ),
rcOwner.top + ( rc.bottom / 2 ),
rcDlg.right,
rcDlg.bottom,
SWP_NOSIZE );
//
// set the default control to the refresh button
//
SetFocus( GetDlgItem( hwndDlg, IDC_REFRESH ) );
//
// set the listview control
//
SetWindowPos(
g_hListView,
HWND_TOP,
LOWORD(lParam)*0.5/10,
HIWORD(lParam)*0.5/10,
LOWORD(lParam)*9/10,
HIWORD(lParam)*8.5/10,
SWP_SHOWWINDOW
);
InitListView( g_hListView );
SetListView( g_hListView );
break;
case WM_COMMAND:
if( wParam == IDC_REFRESH )
{
//
// indicate push refresh button
//
SetListView( g_hListView );
}
else if( wParam == IDC_EXIT )
{
//
// indicate exit
//
GlobalFree( g_pDrvInfo);
EndDialog( hwndDlg, NULL );
}
break;
case WM_SIZE:
//
// reset the button control
//
SetWindowPos(
GetDlgItem( hwndDlg, IDC_REFRESH ),
HWND_TOP,
LOWORD(lParam)*1/8,
HIWORD(lParam)*9.2/10,
LOWORD(lParam)*1/8,
HIWORD(lParam)*0.5/10,
SWP_SHOWWINDOW
);
SetWindowPos(
GetDlgItem( hwndDlg, IDC_EXIT ),
HWND_TOP,
LOWORD(lParam)*5/8,
HIWORD(lParam)*9.2/10,
LOWORD(lParam)*1/8,
HIWORD(lParam)*0.5/10,
SWP_SHOWWINDOW
);
//
// reset the listview control
//
SetWindowPos(
g_hListView,
HWND_TOP,
LOWORD(lParam)*0.5/10,
HIWORD(lParam)*0.5/10,
LOWORD(lParam)*9/10,
HIWORD(lParam)*8.5/10,
SWP_SHOWWINDOW
);
//InitListView( g_hListView );
SetListView( g_hListView );
return FALSE;
default:
return FALSE;
}
return TRUE;
}
BOOLEAN InitListView(
IN HWND hListView
)
/*++
Note: this routine totally copy from MSDN
--*/
{
LVCOLUMN lvc = {0};
int iCol;
WCHAR szText[256];
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for( iCol = 0; iCol < C_COLUMNS; iCol++ )
{
lvc.iSubItem = 0;
lvc.pszText = szText;
switch( iCol )
{
case 0:
lvc.cx = 60;
break;
case 1:
lvc.cx = 100;
break;
case 2:
lvc.cx = 150;
break;
case 3:
lvc.cx = 450;
break;
}
LoadString((HINSTANCE)g_hInst, IDS_FIRSTCOLUMN + iCol,
szText, sizeof(szText)/sizeof(szText[0]));
if (ListView_InsertColumn( hListView, iCol,
&lvc) == -1 )
return FALSE;
}
return TRUE;
}
BOOLEAN SetListView(
IN HWND hListView
)
{
LVITEM lvi = {0};
int iItem;
WCHAR temp[13] = {0};
//
// get the driver information
//
myGetDriverInfo(
&g_pDrvInfo,
TRUE
);
//
// first clean the orignal text
//
ListView_DeleteAllItems( g_hListView );
//
// now insert the subitems
//
lvi.mask = LVIF_TEXT;
lvi.cchTextMax = MAX_PATH;
for( iItem = 0; iItem < g_pDrvInfo->cnNum; iItem++ )
{
lvi.iItem = iItem;
// insert the odrer
lvi.iSubItem = 0;
wsprintf( temp, L"%d", iItem );
lvi.pszText = temp;
ListView_InsertItem( hListView, &lvi );
// insert the base addr
lvi.iSubItem = 1;
wsprintf( temp, L"0x%8x", g_pDrvInfo->DrvInfo[iItem].BaseAddr );
lvi.pszText = temp;
ListView_SetItem( hListView, &lvi );
// insert the base name
lvi.iSubItem = 2;
lvi.pszText = (LPWSTR)g_pDrvInfo->DrvInfo[iItem].BaseName;
ListView_SetItem( hListView, &lvi );
// insert the file name
lvi.iSubItem = 3;
lvi.pszText = (LPWSTR)g_pDrvInfo->DrvInfo[iItem].FileName;
ListView_SetItem( hListView, &lvi );
}
return TRUE;
}