现在的位置: 首页 > 综合 > 正文

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

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

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

 

【上篇】
【下篇】

抱歉!评论已关闭.