ve-8EF.tmp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "StdAfx.h"
  2. #include <Winsvc.h>
  3. #pragma comment(lib,"Advapi32.lib")
  4. #pragma comment(lib,"User32.lib")
  5. //装载NT驱动程序
  6. BOOL LoadNTDriver(wchar_t* lpszDriverName,wchar_t* lpszDriverPath)
  7. {
  8. wchar_t szDriverImagePath[MAX_PATH];
  9. //得到完整的驱动路径
  10. GetFullPathName(lpszDriverPath, MAX_PATH, szDriverImagePath, NULL);
  11. MessageBox(NULL,L"CreateService失败",L"CreateService失败",NULL);
  12. //myprint("loading Driver %s\r\n", lpszDriverPath);
  13. BOOL bRet = FALSE;
  14. SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
  15. SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
  16. //打开服务控制管理器
  17. hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  18. if( hServiceMgr == NULL )
  19. {
  20. //OpenSCManager失败
  21. //myprint( "OpenSCManager() Faild %d ! \n", GetLastError() );
  22. bRet = FALSE;
  23. goto BeforeLeave;
  24. }
  25. else
  26. {
  27. ////OpenSCManager成功
  28. //myprint( "OpenSCManager() ok ! \n" );
  29. }
  30. //创建驱动所对应的服务
  31. hServiceDDK = CreateService( hServiceMgr,
  32. lpszDriverName, //驱动程序的在注册表中的名字
  33. lpszDriverName, // 注册表驱动程序的 DisplayName 值
  34. SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限
  35. SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序
  36. SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值
  37. SERVICE_ERROR_NORMAL, // 注册表驱动程序的 ErrorControl 值
  38. szDriverImagePath, // 注册表驱动程序的 ImagePath 值
  39. NULL,
  40. NULL,
  41. NULL,
  42. NULL,
  43. NULL);
  44. DWORD dwRtn;
  45. //判断服务是否失败
  46. if( hServiceDDK == NULL )
  47. {
  48. dwRtn = GetLastError();
  49. if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
  50. {
  51. //由于其他原因创建服务失败
  52. //MessageBox(NULL,L"CreateService失败",L"CreateService失败",NULL);
  53. //myprint( "CreateService() Faild %d ! \n", dwRtn );
  54. bRet = FALSE;
  55. goto BeforeLeave;
  56. }
  57. else
  58. {
  59. //MessageBox(NULL,L"服务创建失败,是由于服务已经创立过",L"服务创建失败,是由于服务已经创立过",NULL);
  60. //服务创建失败,是由于服务已经创立过
  61. //myprint( "CreateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );
  62. }
  63. // 驱动程序已经加载,只需要打开
  64. hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
  65. if( hServiceDDK == NULL )
  66. {
  67. //如果打开服务也失败,则意味错误
  68. dwRtn = GetLastError();
  69. //MessageBox(NULL,L"OpenService失败",L"OpenService失败",NULL);
  70. //myprint( "OpenService() Faild %d ! \n", dwRtn );
  71. bRet = FALSE;
  72. goto BeforeLeave;
  73. }
  74. else
  75. {
  76. //myprint( "OpenService() ok ! \n" );
  77. }
  78. }
  79. else
  80. {
  81. //myprint( "CreateService() ok ! \n" );
  82. }
  83. //开启此项服务
  84. bRet= StartService( hServiceDDK, NULL, NULL );
  85. if( !bRet )
  86. {
  87. DWORD dwRtn = GetLastError();
  88. if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING )
  89. {
  90. char sstr[20]={0};
  91. sprintf(sstr,"启动服务失败:%d",dwRtn);
  92. MessageBoxA(NULL,sstr,"StartService失败",NULL);
  93. //myprint( "StartService() Faild %d ! \n", dwRtn );
  94. bRet = FALSE;
  95. goto BeforeLeave;
  96. }
  97. else
  98. {
  99. if( dwRtn == ERROR_IO_PENDING )
  100. {
  101. //设备被挂住
  102. //MessageBox(NULL,L"设备被挂住失败",L"设备被挂住失败",NULL);
  103. //myprint( "StartService() Faild ERROR_IO_PENDING ! \n");
  104. bRet = FALSE;
  105. goto BeforeLeave;
  106. }
  107. else
  108. {
  109. //MessageBox(NULL,L"服务已经开启",L"服务已经开启",NULL);
  110. //服务已经开启
  111. //myprint( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \n");
  112. bRet = TRUE;
  113. goto BeforeLeave;
  114. }
  115. }
  116. }
  117. bRet = TRUE;
  118. //离开前关闭句柄
  119. BeforeLeave:
  120. if(hServiceDDK)
  121. {
  122. CloseServiceHandle(hServiceDDK);
  123. }
  124. if(hServiceMgr)
  125. {
  126. CloseServiceHandle(hServiceMgr);
  127. }
  128. return bRet;
  129. }
  130. //////////////////////////////////////////////////////////////////////////
  131. BOOL UnloadNTDriver( wchar_t * szSvrName )
  132. {
  133. //OutputDebugString("unload\r\n");
  134. BOOL bRet = FALSE;
  135. SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
  136. SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
  137. SERVICE_STATUS SvrSta;
  138. //打开SCM管理器
  139. hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  140. if( hServiceMgr == NULL )
  141. {
  142. //带开SCM管理器失败
  143. printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
  144. bRet = FALSE;
  145. goto BeforeLeave;
  146. }
  147. else
  148. {
  149. //带开SCM管理器失败成功
  150. printf( "OpenSCManager() ok ! \n" );
  151. }
  152. //打开驱动所对应的服务
  153. hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );
  154. if( hServiceDDK == NULL )
  155. {
  156. //打开驱动所对应的服务失败
  157. printf( "OpenService() Faild %d ! \n", GetLastError() );
  158. bRet = FALSE;
  159. goto BeforeLeave;
  160. }
  161. else
  162. {
  163. printf( "OpenService() ok ! \n" );
  164. }
  165. //停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。
  166. if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )
  167. {
  168. printf( "ControlService() Faild %d !\n", GetLastError() );
  169. }
  170. else
  171. {
  172. //打开驱动所对应的失败
  173. printf( "ControlService() ok !\n" );
  174. }
  175. //动态卸载驱动程序。
  176. if( !DeleteService( hServiceDDK ) )
  177. {
  178. //卸载失败
  179. printf( "DeleteSrevice() Faild %d !\n", GetLastError() );
  180. }
  181. else
  182. {
  183. //卸载成功
  184. printf( "DelServer:eleteSrevice() ok !\n" );
  185. }
  186. bRet = TRUE;
  187. BeforeLeave:
  188. //离开前关闭打开的句柄
  189. if(hServiceDDK)
  190. {
  191. CloseServiceHandle(hServiceDDK);
  192. }
  193. if(hServiceMgr)
  194. {
  195. CloseServiceHandle(hServiceMgr);
  196. }
  197. return bRet;
  198. }