PCIStartup.cpp
上传用户:zhuqijet
上传日期:2007-01-04
资源大小:138k
文件大小:25k
源码类别:

驱动编程

开发平台:

Visual C++

  1. // PCIStartup.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include <winioctl.h>
  5. #include <winsvc.h> // cleverly excluded by Microsoft
  6. #include "PCI Explorer.h"
  7. #include "PCIStartup.h"
  8. #include "pci.h"
  9. #include "pciScanIoctl.h"
  10. #include "about.h"
  11. #include "GetDriveType.h"
  12. #include "PCI ExplorerDlg.h"
  13. #include "NumericEdit.h"
  14. #include "IDCombo.h"
  15. #include "DataDisplay.h"
  16. #include "pcidata.h"
  17. #include "RegVars.h"
  18. #include "index.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. #define PCISCAN _T("PCISCAN")
  25. static WORD UWM_RUN = ::RegisterWindowMessage(_T("UWM_RUN-{E7844BB0-105D-11d2-8379-00AA005C0507}"));
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CPCIStartup dialog
  28. CPCIStartup::CPCIStartup(CWnd* pParent /*=NULL*/)
  29. : CDialog(CPCIStartup::IDD, pParent)
  30. {
  31. //{{AFX_DATA_INIT(CPCIStartup)
  32. // NOTE: the ClassWizard will add member initialization here
  33. //}}AFX_DATA_INIT
  34. }
  35. void CPCIStartup::DoDataExchange(CDataExchange* pDX)
  36. {
  37. CDialog::DoDataExchange(pDX);
  38. //{{AFX_DATA_MAP(CPCIStartup)
  39. DDX_Control(pDX, IDC_DRIVER_CAPTION, c_c_Driver);
  40. DDX_Control(pDX, IDC_DRIVER, c_Driver);
  41. DDX_Control(pDX, IDC_CHANGE, c_Change);
  42. DDX_Control(pDX, IDC_BROWSE, c_Browse);
  43. DDX_Control(pDX, IDC_COMPLETED, c_Completed);
  44. DDX_Control(pDX, IDC_OPENING, c_Opening);
  45. DDX_Control(pDX, IDC_PROGRESS, c_Progress);
  46. DDX_Control(pDX, IDOK, c_OK);
  47. DDX_Control(pDX, IDC_UNINSTALLING, c_Uninstalling);
  48. DDX_Control(pDX, IDC_STOPPING, c_Stopping);
  49. DDX_Control(pDX, IDC_STATUS, c_Status);
  50. DDX_Control(pDX, IDC_STARTING, c_Starting);
  51. DDX_Control(pDX, IDC_SCM_OPEN, c_OpeningSCM);
  52. DDX_Control(pDX, IDC_SCANNING, c_Scanning);
  53. DDX_Control(pDX, IDC_INSTALLING, c_Installing);
  54. //}}AFX_DATA_MAP
  55. }
  56. BEGIN_MESSAGE_MAP(CPCIStartup, CDialog)
  57. //{{AFX_MSG_MAP(CPCIStartup)
  58. ON_WM_SYSCOMMAND()
  59. ON_REGISTERED_MESSAGE(UWM_RUN, OnRunPCI)
  60. ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  61. ON_BN_CLICKED(IDC_CHANGE, OnChange)
  62. ON_EN_CHANGE(IDC_DRIVER, OnChangeDriver)
  63. //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65. /****************************************************************************
  66. *                       CPCIStartup::InstallDriver
  67. * Inputs:
  68. *       IN SC_HANDLE scm: Service Control Manager handle
  69. * IN LPCTSTR DriverName: Driver name
  70. * IN LPCTSTR executable: Place where it is found
  71. * Result: BOOL
  72. *       TRUE if successful
  73. * FALSE if error
  74. * Effect: 
  75. *       Installs the driver
  76. ****************************************************************************/
  77. BOOL CPCIStartup::InstallDriver(SC_HANDLE  scm, LPCTSTR DriverName, LPCTSTR ServiceExe)
  78.     {
  79.      SC_HANDLE  Service;
  80.      DWORD      err;
  81.      //
  82.      // NOTE: This creates an entry for a standalone driver. If this
  83.      //       is modified for use with a driver that requires a Tag,
  84.      //       Group, and/or Dependencies, it may be necessary to
  85.      //       query the registry for existing driver information
  86.      //       (in order to determine a unique Tag, etc.).
  87.      //
  88.      Service = CreateService (scm,          // SCManager database
  89.             DriverName,           // name of service
  90.       DriverName,           // name to display
  91.       SERVICE_ALL_ACCESS,    // desired access
  92.       SERVICE_KERNEL_DRIVER, // service type
  93.       SERVICE_DEMAND_START,  // start type
  94.       SERVICE_ERROR_NORMAL,  // error control type
  95.       ServiceExe,            // full path to service's binary
  96.       NULL,                  // no load ordering group
  97.       NULL,                  // no tag identifier
  98.       NULL,                  // no dependencies
  99.       NULL,                  // LocalSystem account
  100.       NULL                   // no password
  101.       );
  102.      if (Service == NULL)
  103. { /* failed */
  104.  err = GetLastError();
  105.  if (err == ERROR_SERVICE_EXISTS)
  106.     { /* already installed */
  107.     } /* already installed */
  108.  else
  109.     {
  110.      printf ("failure: CreateService (0x%02x)n",
  111.      err
  112.      );
  113.     }
  114.  return FALSE;
  115. }
  116.      else
  117. {
  118.  printf ("CreateService SUCCESSn");
  119. }
  120.      CloseServiceHandle (Service);
  121.      return TRUE;
  122.     }
  123. /****************************************************************************
  124. *                        CPCIStartup::RemoveDriver
  125. * Inputs:
  126. *       SC_HANDLE scm: Service control manager handle
  127. * Result: BOOL
  128. *       TRUE if successful
  129. * FALSE if failure
  130. * Effect: 
  131. *       Uninstalles the PCI driver
  132. ****************************************************************************/
  133. BOOL CPCIStartup::RemoveDriver(SC_HANDLE scm, LPCTSTR DriverName)
  134.     {
  135.      SC_HANDLE  Service;
  136.      BOOL       ret;
  137.      Service = OpenService (scm,
  138.                             DriverName,
  139.     SERVICE_ALL_ACCESS
  140.     );
  141.      if (Service == NULL)
  142. {
  143.  return FALSE;
  144. }
  145.      ret = DeleteService (Service);
  146.      if (!ret)
  147. { /* failed to remove it */
  148. } /* failed to remove it */
  149.      CloseServiceHandle (Service);
  150.      return ret;
  151.     }
  152. /****************************************************************************
  153. *                        CPCIStartup::getErrorMessage
  154. * Inputs:
  155. *       DWORD err: Error code to be displayed
  156. * Result: CString
  157. *       Displayable string with superfluous rn removed
  158. * Effect: 
  159. *