[代码]文栏化ring3 API列举驱动(Console和GUI版)

 console版如下:
  1. /*++ 
  2. Module Name:
  3.     ListDrvCon.cpp
  4. Enviroment:
  5.     All Windows NT Platfrom;Console
  6. Abstract:
  7.     List all the driver's name & baseaddr & fileaddr
  8. Note:
  9.     Using documented API in psapi.h
  10. Revision:
  11.     23-Nov-2008 Created.
  12. Author:
  13.     benyanwk
  14. --*/
  15. //
  16. // directives
  17. //
  18. #define MAXNUM 255
  19. #define UNICODE // to support chinese 
  20. #include <windows.h>
  21. #include <psapi.h>
  22. #include <stdio.h>
  23. #pragma comment(lib,"psapi.lib")
  24. //
  25. // strcut defintion
  26. //
  27. typedef struct _DRIVER_INFO {
  28.     WCHAR BaseName[MAX_PATH];
  29.     WCHAR FileName[MAX_PATH];
  30.     DWORD BaseAddr;
  31. } DRIVER_INFO,*PDRIVER_INFO ;
  32. typedef struct _ALL_DRIVER_INFO {
  33.     DWORD cbNum;
  34.     DRIVER_INFO DrvInfo[1]; // define variable length structure
  35. } ALL_DRIVER_INFO,*PALL_DRIVER_INFO;
  36. //
  37. // function declaration
  38. //
  39. int main();
  40. int myGetDriverInfo(
  41.     IN PVOID &pDrvInfo,
  42.     IN BOOLEAN bAlloc
  43.     );
  44. //
  45. // function definition
  46. //
  47. int myGetDriverInfo(
  48.     IN PALL_DRIVER_INFO* pDrvInfo,  
  49.     IN BOOLEAN bAlloc
  50.     )
  51. {
  52. /*++
  53. Arguments:
  54.     pDrvInfo-->the buffer to store the driver information
  55.     bAlloc-->alloc the global memory by the function or not
  56.     return 0 indicate success otherwise error 
  57. --*/
  58.     DWORD cbNum = 0;
  59.     PDWORD pBaseAddr = NULL ;
  60.     PWCHAR pFileName = NULL;
  61.     PWCHAR pBaseName = NULL;
  62.     PALL_DRIVER_INFO pAllDrvInfo;
  63.         
  64.     pBaseAddr = (PDWORD)GlobalAlloc( GMEM_FIXED, sizeof(DWORD)*MAXNUM );
  65.     if( EnumDeviceDrivers( (LPVOID*)pBaseAddr, sizeof(DWORD)*MAXNUM, &cbNum ) != TRUE )
  66.     {
  67.         //
  68.         // indicate EnumDeviceDriver failed!
  69.         //
  70.         wprintf( L"EnumDeviceDriver failed! ErrorCode = %8x/n", GetLastError() );
  71.         return 1; 
  72.     }
  73.     
  74.     cbNum /=4; // cbNum return the bytes
  75.     if( bAlloc == TRUE )
  76.         pAllDrvInfo = (PALL_DRIVER_INFO)GlobalAlloc( GMEM_FIXED, sizeof(DRIVER_INFO)*cbNum + 4 );
  77.     else
  78.         pAllDrvInfo = (PALL_DRIVER_INFO)pDrvInfo;
  79.     pAllDrvInfo->cbNum = cbNum;
  80.     forint i = 0; i < cbNum; i++ )
  81.     {
  82.         pAllDrvInfo->DrvInfo[i].BaseAddr = *pBaseAddr;
  83.         GetDeviceDriverBaseName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].BaseName, MAX_PATH );
  84.         GetDeviceDriverFileName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].FileName, MAX_PATH );
  85.         pBaseAddr++;
  86.     }
  87.     *pDrvInfo = pAllDrvInfo; // return the drv info buffer pointer 
  88.     return 0;   // indicate success
  89. }
  90. int main()
  91. {
  92.     PALL_DRIVER_INFO pDrvInfo = NULL;
  93.     myGetDriverInfo(
  94.         (PALL_DRIVER_INFO*)&pDrvInfo,
  95.         TRUE
  96.         );
  97.     wprintf(L"=====the list of driver as follows === /n");
  98.     wprintf(L"Order/tBaseAddr /t BaseName /t FileName /t /n");
  99.     forint i = 0; i<pDrvInfo->cbNum; i++ )
  100.     {
  101.         wprintf(L"%d/t%8x/t%s/t%s/n",
  102.                 i,
  103.                 pDrvInfo->DrvInfo[i].BaseAddr,
  104.                 pDrvInfo->DrvInfo[i].BaseName,
  105.                 pDrvInfo->DrvInfo[i].FileName
  106.                 );
  107.     }
  108.     return 0;
  109. }

 

GUI版如下:

  1. /*++ 
  2. Module Name:
  3.     ListDrvGui.cpp
  4. Enviroment:
  5.     All Windows NT Platfrom;GUI
  6. Abstract:
  7.     List all the driver's name & baseaddr & fileaddr
  8. Note:
  9.     (1) Using documented API in psapi and 
  10.         ListView control to show the result
  11.     (2) Wanted upgraded:
  12.         
  13.         + ListView control sort functionality(IMPORTANT)
  14.         + Item should can be selected
  15.         + Support the right click menu
  16. Revision:
  17.     23-Nov-2008 Created.
  18. Author:
  19.     benyanwk
  20. --*/
  21. //
  22. // directives
  23. /
  24. #define MAXNUM 255
  25. #define UNICODE             // to support chinese 
  26. #include <windows.h>
  27. #include <commctrl.h>   
  28. #include <psapi.h>
  29. #include <stdio.h>
  30. #define C_COLUMNS 4  // 4 columns to be added
  31. #include "resource.h"   // the resource file
  32. #pragma comment(lib,"psapi.lib")
  33. #pragma comment(lib,"comctl32.lib")
  34. // strcut defintion
  35. /
  36. typedef struct _DRIVER_INFO {
  37.     WCHAR BaseName[MAX_PATH];
  38.     WCHAR FileName[MAX_PATH];
  39.     DWORD BaseAddr;
  40. } DRIVER_INFO,*PDRIVER_INFO ;
  41. typedef struct _ALL_DRIVER_INFO {
  42.     DWORD cnNum;
  43.     DRIVER_INFO DrvInfo[1]; // define variable length structure
  44. } ALL_DRIVER_INFO,*PALL_DRIVER_INFO;
  45. // function declaration
  46. ///
  47. INT_PTR CALLBACK DlgProc(          
  48.     HWND hwndDlg,
  49.     UINT uMsg,
  50.     WPARAM wParam,
  51.     LPARAM lParam
  52. );
  53. int WINAPI WinMain(          
  54.     HINSTANCE hInstance,
  55.     HINSTANCE hPrevInstance,
  56.     LPSTR lpCmdLine,
  57.     int nCmdShow
  58. );
  59. int myGetDriverInfo(
  60.     IN PVOID &pDrvInfo,
  61.     IN BOOLEAN bAlloc
  62.     );
  63. BOOLEAN SetListView(
  64.       IN HWND hListView
  65.       );
  66. BOOLEAN InitListView(
  67.       IN HWND hListView
  68.       );
  69. //
  70. // Global variable declaration
  71. PALL_DRIVER_INFO g_pDrvInfo = NULL; // the global variable used to store the driver information
  72. HWND g_hInst = NULL;
  73. HWND g_hListView = NULL;
  74. ///
  75. // function definition
  76. //
  77. int myGetDriverInfo(
  78.     IN PALL_DRIVER_INFO* pDrvInfo,  
  79.     IN BOOLEAN bAlloc
  80.     )
  81. {
  82. /*++
  83. Arguments:
  84.     pDrvInfo-->the buffer to store the driver information
  85.     bAlloc-->alloc the global memory by the function or not
  86.     return 0 indicate success otherwise error 
  87. --*/
  88.     DWORD cbNum = 0;
  89.     PDWORD pBaseAddr = NULL ;
  90.     PWCHAR pFileName = NULL;
  91.     PWCHAR pBaseName = NULL;
  92.     PALL_DRIVER_INFO pAllDrvInfo = NULL;
  93.         
  94.     pBaseAddr = (PDWORD)GlobalAlloc( GMEM_FIXED, sizeof(DWORD)*MAXNUM );
  95.     if( EnumDeviceDrivers( (LPVOID*)pBaseAddr, sizeof(DWORD)*MAXNUM, &cbNum ) != TRUE )
  96.     {
  97.         //
  98.         // indicate EnumDeviceDriver failed!
  99.         //
  100.         wprintf( L"EnumDeviceDriver failed! ErrorCode = %8x/n", GetLastError() );
  101.         return 1; 
  102.     }
  103.     
  104.     cbNum /=4; // cbNum return the bytes
  105.     if( bAlloc == TRUE )
  106.         pAllDrvInfo = (PALL_DRIVER_INFO)GlobalAlloc( GMEM_FIXED, sizeof(DRIVER_INFO)*cbNum + 4 );
  107.     else
  108.         pAllDrvInfo = (PALL_DRIVER_INFO)pDrvInfo;
  109.     pAllDrvInfo->cnNum = cbNum;
  110.     forint i = 0; i < cbNum; i++ )
  111.     {
  112.         pAllDrvInfo->DrvInfo[i].BaseAddr = *pBaseAddr;
  113.         GetDeviceDriverBaseName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].BaseName, MAX_PATH );
  114.         GetDeviceDriverFileName( (LPVOID)*pBaseAddr, (LPWSTR)&pAllDrvInfo->DrvInfo[i].FileName, MAX_PATH );
  115.         pBaseAddr++;
  116.     }
  117.     GlobalFree( pBaseAddr );
  118.     *pDrvInfo = pAllDrvInfo; // return the drv info buffer pointer 
  119.     return 0;   // indicate success
  120. }
  121. int WINAPI WinMain(          
  122.     HINSTANCE hInstance,
  123.     HINSTANCE hPrevInstance,
  124.     LPSTR lpCmdLine,
  125.     int nCmdShow
  126. )
  127. {
  128.     g_hInst = (HWND)GetModuleHandle( NULL );
  129.     InitCommonControls();
  130.     //
  131.     // Create the main dialog
  132.     //
  133.     DialogBoxParam( 
  134.         (HINSTANCE)g_hInst, // hinstance
  135.         MAKEINTRESOURCE(IDD_DIALOGBAR), // temple name
  136.         NULL,   // parent hwnd
  137.         DlgProc, // dialog functoin
  138.         NULL    // initialize parameter
  139.         );  
  140.     ExitProcess(0);
  141. }
  142. INT_PTR CALLBACK DlgProc(          
  143.     HWND hwndDlg,
  144.     UINT uMsg,
  145.     WPARAM wParam,
  146.     LPARAM lParam
  147. )
  148. {
  149.     RECT rect;
  150.     HWND hwndOwner;
  151.     RECT rc,rcDlg,rcOwner;
  152.     int  iSelCol;
  153.     HICON hIcon;
  154.     switch( uMsg )
  155.     {
  156.     case WM_CLOSE:
  157.         GlobalFree( g_pDrvInfo);
  158.         EndDialog( hwndDlg, NULL );
  159.         break;
  160.     case WM_INITDIALOG:
  161.         //
  162.         // set the icon
  163.         //
  164.         hIcon = LoadIcon( (HINSTANCE)g_hInst, (LPCWSTR)IDI_ICON1 );
  165.         SendMessage( hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
  166.         SendMessage( hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon );
  167.         // 
  168.         // centre the dialog
  169.         //
  170.         g_hListView = GetDlgItem( hwndDlg, IDC_LIST2 );
  171.         
  172.         hwndOwner = GetDesktopWindow();
  173.         GetWindowRect( hwndOwner, &rcOwner );
  174.         GetWindowRect( hwndDlg, &rcDlg );
  175.         CopyRect( &rc, &rcOwner );  
  176.         // Offset the owner and dialog box rectangle so that
  177.         // right and bottom values represent the width and 
  178.         // height, and then offset the owner again to discard 
  179.         // space taken up by the dialog
  180.         OffsetRect( &rcDlg, -rcDlg.left, -rcDlg.top );
  181.         OffsetRect( &rc, -rc.left, -rc.top );
  182.         OffsetRect( &rc, -rcDlg.right, -rcDlg.bottom );
  183.         SetWindowPos(
  184.             hwndDlg,
  185.             HWND_TOP,
  186.             rcOwner.left + ( rc.right / 2 ),
  187.             rcOwner.top + ( rc.bottom / 2 ),
  188.             rcDlg.right,
  189.             rcDlg.bottom,
  190.             SWP_NOSIZE );
  191.         
  192.         //
  193.         // set the default control to the refresh button
  194.         //
  195.         SetFocus( GetDlgItem( hwndDlg, IDC_REFRESH ) );
  196.         //
  197.         // set the listview control
  198.         //
  199.         
  200.         SetWindowPos( 
  201.             g_hListView,
  202.             HWND_TOP,
  203.             LOWORD(lParam)*0.5/10,
  204.             HIWORD(lParam)*0.5/10,
  205.             LOWORD(lParam)*9/10,
  206.             HIWORD(lParam)*8.5/10,
  207.             SWP_SHOWWINDOW
  208.             );
  209.         InitListView( g_hListView );
  210.         SetListView( g_hListView );
  211.         break;
  212.     case WM_COMMAND:
  213.         if( wParam == IDC_REFRESH )
  214.         {
  215.             // 
  216.             // indicate push refresh button
  217.             //
  218.             SetListView( g_hListView );
  219.         }
  220.         else if( wParam == IDC_EXIT )
  221.         {
  222.             // 
  223.             // indicate exit
  224.             // 
  225.             GlobalFree( g_pDrvInfo);
  226.             EndDialog( hwndDlg, NULL );
  227.         }
  228.         break;
  229.     case WM_SIZE:
  230.         // 
  231.         // reset the button control
  232.         //
  233.         SetWindowPos(
  234.             GetDlgItem( hwndDlg, IDC_REFRESH ),
  235.             HWND_TOP,
  236.             LOWORD(lParam)*1/8,
  237.             HIWORD(lParam)*9.2/10,
  238.             LOWORD(lParam)*1/8,
  239.             HIWORD(lParam)*0.5/10,
  240.             SWP_SHOWWINDOW
  241.             );
  242.         SetWindowPos(
  243.             GetDlgItem( hwndDlg, IDC_EXIT ),
  244.             HWND_TOP,
  245.             LOWORD(lParam)*5/8,
  246.             HIWORD(lParam)*9.2/10,
  247.             LOWORD(lParam)*1/8,
  248.             HIWORD(lParam)*0.5/10,
  249.             SWP_SHOWWINDOW 
  250.             );
  251.         //
  252.         // reset the listview control
  253.         //
  254.         SetWindowPos( 
  255.             g_hListView,
  256.             HWND_TOP,
  257.             LOWORD(lParam)*0.5/10,
  258.             HIWORD(lParam)*0.5/10,
  259.             LOWORD(lParam)*9/10,
  260.             HIWORD(lParam)*8.5/10,
  261.             SWP_SHOWWINDOW
  262.             );
  263.         //InitListView( g_hListView );
  264.         SetListView( g_hListView );
  265.             
  266.         return FALSE;
  267.         default:
  268.             return FALSE;
  269.     }
  270.     return TRUE;
  271.             
  272. }
  273. BOOLEAN InitListView(
  274.       IN HWND hListView
  275.       )
  276. /*++
  277.  Note: this routine totally copy from MSDN
  278. --*/
  279. {
  280.     LVCOLUMN lvc = {0};
  281.     int iCol;
  282.     WCHAR szText[256];
  283.     
  284.     lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  285.     for( iCol = 0; iCol < C_COLUMNS; iCol++ )
  286.     {
  287.         lvc.iSubItem = 0;
  288.         lvc.pszText = szText; 
  289.         
  290.         switch( iCol )
  291.         {
  292.         case 0:
  293.             lvc.cx = 60;
  294.             break;
  295.         case 1:
  296.             lvc.cx = 100;
  297.             break;
  298.         case 2:
  299.             lvc.cx = 150;
  300.             break;
  301.         case 3:
  302.             lvc.cx = 450;
  303.             break;
  304.         }
  305.         LoadString((HINSTANCE)g_hInst, IDS_FIRSTCOLUMN + iCol,
  306.             szText, sizeof(szText)/sizeof(szText[0]));
  307.         if (ListView_InsertColumn( hListView, iCol,
  308.                             &lvc) == -1 )
  309.             return FALSE;
  310.     }
  311.     return TRUE;
  312. }
  313. BOOLEAN SetListView(
  314.       IN HWND hListView
  315.       )
  316. {
  317.     
  318.     LVITEM lvi = {0};
  319.     int iItem;
  320.     WCHAR temp[13] = {0};
  321.     //
  322.     // get the driver information
  323.     //
  324.     myGetDriverInfo(
  325.         &g_pDrvInfo,
  326.         TRUE
  327.         );
  328.     //
  329.     // first clean the orignal text
  330.     //
  331.     ListView_DeleteAllItems( g_hListView );
  332.     //
  333.     // now insert the subitems
  334.     //
  335.     lvi.mask = LVIF_TEXT;
  336.     lvi.cchTextMax = MAX_PATH;
  337.     for( iItem = 0; iItem < g_pDrvInfo->cnNum; iItem++ )
  338.     {
  339.         lvi.iItem = iItem;
  340.         // insert the odrer
  341.         lvi.iSubItem = 0;
  342.         wsprintf( temp,  L"%d", iItem );
  343.         lvi.pszText = temp;
  344.         ListView_InsertItem( hListView, &lvi ); 
  345.         // insert the base addr
  346.         lvi.iSubItem = 1;
  347.         wsprintf( temp, L"0x%8x", g_pDrvInfo->DrvInfo[iItem].BaseAddr );
  348.         lvi.pszText = temp;
  349.         ListView_SetItem( hListView, &lvi );
  350.         // insert the base name
  351.         lvi.iSubItem = 2;
  352.         lvi.pszText = (LPWSTR)g_pDrvInfo->DrvInfo[iItem].BaseName;
  353.         ListView_SetItem( hListView, &lvi );
  354.         // insert the file name
  355.         lvi.iSubItem = 3;
  356.         lvi.pszText = (LPWSTR)g_pDrvInfo->DrvInfo[iItem].FileName;
  357.         ListView_SetItem( hListView, &lvi );
  358.     }
  359.     return TRUE;
  360. }

相关文件下载:http://www.live-share.com/files/368301/C1_ListDrv.rar.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值