InstDrv.cpp
上传用户:ason123
上传日期:2010-03-31
资源大小:177k
文件大小:3k
源码类别:

并口编程

开发平台:

C++ Builder

  1. // ---------------------------------------------------- //
  2. //                      WinIo v2.0                      //
  3. //  Direct Hardware Access Under Windows 9x/NT/2000/XP  //
  4. //           Copyright 1998-2002 Yariv Kaplan           //
  5. //               http://www.internals.com               //
  6. // ---------------------------------------------------- //
  7. #include <windows.h>
  8. #include "winio.h"
  9. bool _stdcall InstallWinIoDriver(PSTR pszWinIoDriverPath, bool IsDemandLoaded)
  10. {
  11.   SC_HANDLE hSCManager;
  12.   SC_HANDLE hService;
  13.   // Remove any previous instance of the driver
  14.   RemoveWinIoDriver();
  15.   hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  16.   if (hSCManager)
  17.   {
  18.     // Install the driver
  19.     
  20.     hService = CreateService(hSCManager,
  21.                              "WINIO",
  22.                              "WINIO",
  23.                              SERVICE_ALL_ACCESS,
  24.                              SERVICE_KERNEL_DRIVER,
  25.                              (IsDemandLoaded == true) ? SERVICE_DEMAND_START : SERVICE_SYSTEM_START,
  26.                              SERVICE_ERROR_NORMAL,
  27.                              pszWinIoDriverPath,
  28.                              NULL,
  29.                              NULL,
  30.                              NULL,
  31.                              NULL,
  32.                              NULL);
  33.     CloseServiceHandle(hSCManager);
  34.     if (hService == NULL)
  35.       return false;
  36.   }
  37.   else
  38.     return false;
  39.   CloseServiceHandle(hService);
  40.   
  41.   return true;
  42. }
  43. bool _stdcall RemoveWinIoDriver()
  44. {
  45.   SC_HANDLE hSCManager;
  46.   SC_HANDLE hService;
  47.   bool bResult;
  48.   StopWinIoDriver();
  49.   hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  50.   if (hSCManager)
  51.   {
  52.     hService = OpenService(hSCManager, "WINIO", SERVICE_ALL_ACCESS);
  53.     CloseServiceHandle(hSCManager);
  54.     if (hService)
  55.     {
  56.       bResult = DeleteService(hService);
  57.       
  58.       CloseServiceHandle(hService);
  59.     }
  60.     else
  61.       return false;
  62.   }
  63.   else
  64.     return false;
  65.   return bResult;
  66. }
  67. bool _stdcall StartWinIoDriver()
  68. {
  69.   SC_HANDLE hSCManager;
  70.   SC_HANDLE hService;
  71.   bool bResult;
  72.   hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  73.   if (hSCManager)
  74.   {
  75.     hService = OpenService(hSCManager, "WINIO", SERVICE_ALL_ACCESS);
  76.     CloseServiceHandle(hSCManager);
  77.     if (hService)
  78.     {
  79.       bResult = StartService(hService, 0, NULL) || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING;
  80.       CloseServiceHandle(hService);
  81.     }
  82.     else
  83.       return false;
  84.   }
  85.   else
  86.     return false;
  87.   return bResult;
  88. }
  89. bool _stdcall StopWinIoDriver()
  90. {
  91.   SC_HANDLE hSCManager;
  92.   SC_HANDLE hService;
  93.   SERVICE_STATUS ServiceStatus;
  94.   bool bResult;
  95.   hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  96.   if (hSCManager)
  97.   {
  98.     hService = OpenService(hSCManager, "WINIO", SERVICE_ALL_ACCESS);
  99.     CloseServiceHandle(hSCManager);
  100.     if (hService)
  101.     {
  102.       bResult = ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus);
  103.       CloseServiceHandle(hService);
  104.     }
  105.     else
  106.       return false;
  107.   }
  108.   else
  109.     return false;
  110.   return bResult;
  111. }