home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / device.cpp < prev    next >
C/C++ Source or Header  |  2000-05-11  |  3KB  |  147 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : device.cpp                                                             //
  10. //  Description: Service control manager wrapper, Chapter 3                          //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <winioctl.h>
  19. #include <tchar.h>
  20. #include <assert.h>
  21.  
  22. #include "device.h"
  23.  
  24.  
  25. unsigned KDevice::InstallDriver(const TCHAR * DriverExe)
  26. {
  27.     m_schService = CreateService(m_schSCManager, 
  28.         m_DeviceName, m_DeviceName, 
  29.         SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, 
  30.         SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
  31.         DriverExe, NULL, NULL, NULL, NULL, NULL);
  32.  
  33.     if ( m_schService )
  34.         return 0;
  35.     else
  36.         return GetLastError();
  37. }
  38.  
  39.  
  40. unsigned KDevice::RemoveDriver(void)
  41. {
  42.     if ( m_schService == NULL )
  43.     {
  44.         m_schService = OpenService(m_schSCManager, 
  45.             m_DeviceName, SERVICE_ALL_ACCESS);
  46.  
  47.         if (m_schService == NULL)
  48.             return GetLastError();
  49.     }
  50.  
  51.     if ( DeleteService (m_schService) )
  52.     {
  53.         m_schService = NULL;
  54.         return 0;
  55.     }
  56.     else
  57.         return GetLastError();
  58. }
  59.  
  60.  
  61. unsigned KDevice::StartDriver(void)
  62. {
  63.     assert(m_schService!=NULL);
  64.     
  65.     if ( StartService (m_schService, 0, NULL) )
  66.         return 0;
  67.     else
  68.     {
  69.         DWORD ret = GetLastError();
  70.  
  71.         if ( ret == ERROR_SERVICE_ALREADY_RUNNING )
  72.             ret = 0;
  73.  
  74.         return ret;
  75.    }
  76. }
  77.  
  78.  
  79. unsigned KDevice::StopDriver(void)
  80. {
  81.    SERVICE_STATUS  serviceStatus;
  82.  
  83.     assert(m_schService!=NULL);
  84.     
  85.     if ( ControlService(m_schService, SERVICE_CONTROL_STOP,
  86.         &serviceStatus) )
  87.         return 0;
  88.    else
  89.         return GetLastError();
  90. }
  91.  
  92.  
  93. unsigned KDevice::OpenDevice(void)
  94. {
  95.     TCHAR completeDeviceName[MAX_PATH];
  96.  
  97.     // Create a \\.\XXX device name that CreateFile can use
  98.     _tcscpy(completeDeviceName, _T("\\\\.\\"));
  99.     _tcscat(completeDeviceName, m_DeviceName);
  100.  
  101.     m_hDevice = CreateFile(completeDeviceName,
  102.         GENERIC_READ | GENERIC_WRITE, 0, NULL, 
  103.         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  104.  
  105.     if ( m_hDevice == INVALID_HANDLE_VALUE )
  106.         return GetLastError();
  107.     else
  108.         return 0;
  109. }
  110.  
  111.  
  112. unsigned KDevice::CloseDevice(void)
  113. {
  114.     assert( m_hDevice);
  115.  
  116.     if ( CloseHandle(m_hDevice) )
  117.     {
  118.         m_hDevice = NULL;
  119.         return 0;
  120.     }
  121.     else
  122.     {
  123.         m_hDevice = NULL;
  124.         return GetLastError();
  125.     }
  126. }
  127.  
  128.  
  129. unsigned KDevice::Load(const TCHAR * DriverExe)
  130. {
  131.     RemoveDriver();
  132.     
  133.     InstallDriver(DriverExe);
  134.     StartDriver();
  135.     
  136.     return OpenDevice();
  137. }
  138.  
  139.  
  140. unsigned KDevice::Close(void)
  141. {
  142.     CloseDevice();
  143.     StopDriver();
  144.     
  145.     return RemoveDriver();
  146. }
  147.