Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
9.2 KiB

  1. namespace WindowsApplication1
  2. {
  3. using System;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ServiceProcess;
  7. // Summary description for DriverControllerManager.
  8. // This class is used to handle the device drivers
  9. public class DriverControllerManager
  10. {
  11. //machine under test
  12. private string strMachineName;
  13. //The display name of the selected driver
  14. private string strDisplayName="";
  15. //the listBoxes used to show the drivers
  16. private ListBox lstDrvRun, lstDrvStopped, lstDrvPaused,lstCurrent=null;
  17. /// <summary>
  18. /// default (empty) constructor
  19. /// </summary>
  20. public DriverControllerManager()
  21. {
  22. }
  23. // Explicit constructor for this class
  24. // <param name="tmpDrvRun"> </param>
  25. // Pointer to the running drivers display
  26. // <param name="tmpDrvStopped"> </param>
  27. // Pointer to stopped drivers display
  28. // <param name="tmpDrvPaused"> </param>
  29. // Pointer to the paused drivers listBox
  30. // <param name="tmpMachineName"> </param>
  31. // Selected machine name
  32. //
  33. public DriverControllerManager(ListBox tmpDrvRun, ListBox tmpDrvStopped, ListBox tmpDrvPaused , string tmpMachineName)
  34. {
  35. strMachineName=tmpMachineName;
  36. //Get the pointers to the ListBoxes from the MainForm UI.
  37. //Assign the correspondant EventHandlers
  38. lstDrvPaused=tmpDrvPaused;
  39. lstDrvPaused.SelectedIndexChanged += new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  40. lstDrvPaused.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  41. lstDrvStopped=tmpDrvStopped;
  42. lstDrvStopped.SelectedIndexChanged += new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  43. lstDrvStopped.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  44. lstDrvRun=tmpDrvRun;
  45. lstDrvRun.SelectedIndexChanged += new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  46. lstDrvRun.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  47. LoadDrivers();
  48. }
  49. // Clear all the object collections
  50. public void Clear()
  51. {
  52. strMachineName="";
  53. strDisplayName="";
  54. if(lstDrvRun!=null)
  55. {
  56. lstDrvRun.Items.Clear();
  57. lstDrvRun.SelectedIndexChanged -= new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  58. lstDrvRun.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  59. }
  60. if(lstDrvStopped!=null)
  61. {
  62. lstDrvStopped.Items.Clear();
  63. lstDrvStopped.SelectedIndexChanged -= new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  64. lstDrvStopped.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  65. }
  66. if(lstDrvPaused!=null)
  67. {
  68. lstDrvPaused.Items.Clear();
  69. lstDrvPaused.SelectedIndexChanged -= new System.EventHandler(this.lstDrv_SelectedIndexChanged);
  70. lstDrvPaused.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstDrv_MouseDown);
  71. }
  72. }
  73. // event handler for all the ListBoxes
  74. // Used to catch the name of the selected Driver
  75. public void lstDrv_SelectedIndexChanged(object sender, System.EventArgs e)
  76. {
  77. lstCurrent = (ListBox)sender;
  78. strDisplayName=lstCurrent.SelectedItem.ToString();
  79. }
  80. // event corresponding to the ListBox/mouseDown
  81. // Used to PopUp the ContextMenu with its options
  82. public void lstDrv_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  83. {
  84. if(e.Button==System.Windows.Forms.MouseButtons.Right )
  85. {
  86. lstCurrent = (ListBox)sender;
  87. //Create a new context menu
  88. lstCurrent.ContextMenu=new System.Windows.Forms.ContextMenu();
  89. //Insert the needed menuItems
  90. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("&Start Service", new EventHandler(this.MenuStart)));
  91. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("S&top Service", new EventHandler(this.MenuStop)));
  92. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("&Pause Service", new EventHandler(this.MenuPause)));
  93. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("S&how Driver Info", new EventHandler(this.ShowDriverInfo)));
  94. //Enable/Disable the right MenuItems depending on the selected display
  95. if(lstCurrent.Equals(lstDrvRun) )
  96. {
  97. lstCurrent.ContextMenu.MenuItems[0].Enabled=false;
  98. }
  99. else if(lstCurrent.Equals(lstDrvStopped ))
  100. {
  101. lstCurrent.ContextMenu.MenuItems[1].Enabled=false;
  102. lstCurrent.ContextMenu.MenuItems[2].Enabled=false;
  103. }
  104. else
  105. {
  106. lstCurrent.ContextMenu.MenuItems[2].Enabled=false;
  107. }
  108. lstCurrent.ContextMenu.Show(lstCurrent ,new Point(e.X,e.Y));
  109. }
  110. }
  111. //The event assigned to Start driver MenuItem
  112. private void MenuStart(object sender, EventArgs e)
  113. {
  114. System.ServiceProcess.ServiceController tmpSC;
  115. tmpSC=new System.ServiceProcess.ServiceController();
  116. tmpSC.MachineName=strMachineName;
  117. tmpSC.DisplayName=strDisplayName;
  118. try
  119. {
  120. if(lstCurrent.Equals(lstDrvPaused))
  121. tmpSC.Continue();
  122. else
  123. tmpSC.Start();
  124. //wait for the process to restart
  125. System.Threading.Thread.Sleep(500);
  126. while(tmpSC.Status== ServiceControllerStatus.ContinuePending )
  127. {
  128. Application.DoEvents() ;
  129. }
  130. if(tmpSC.Status==ServiceControllerStatus.Running )
  131. {
  132. lstDrvRun.Items.Add(lstCurrent.SelectedItem.ToString());
  133. lstCurrent.Items.Remove(lstCurrent.SelectedItem.ToString());
  134. lstCurrent.Refresh();
  135. lstDrvRun.Refresh();
  136. }
  137. else
  138. {
  139. MessageBox.Show(tmpSC.ServiceName + " Cannot be started");
  140. }
  141. }
  142. catch
  143. {
  144. MessageBox.Show("Service: " + strDisplayName + " Could not be started ! ");
  145. }
  146. }
  147. //Try to stop a service.
  148. private void MenuStop(object sender, EventArgs e)
  149. {
  150. System.ServiceProcess.ServiceController tmpSC;
  151. tmpSC=new System.ServiceProcess.ServiceController();
  152. tmpSC.MachineName=strMachineName;
  153. tmpSC.DisplayName=strDisplayName;
  154. try
  155. {
  156. if(tmpSC.CanStop)
  157. {
  158. try
  159. {
  160. tmpSC.Stop();
  161. System.Threading.Thread.Sleep(500);
  162. //wait for the service to stop
  163. while(tmpSC.Status == ServiceControllerStatus.StopPending)
  164. {
  165. Application.DoEvents();
  166. }
  167. lstDrvStopped.Items.Add(lstCurrent.SelectedItem.ToString());
  168. lstCurrent.Items.Remove(lstCurrent.SelectedItem.ToString());
  169. lstCurrent.Refresh();
  170. lstDrvStopped.Refresh();
  171. }
  172. catch
  173. {
  174. MessageBox.Show("Device driver could not be stopped !");
  175. }
  176. }
  177. else
  178. {
  179. MessageBox.Show("The service: " + tmpSC.DisplayName + " is not allowed to be stopped !");
  180. }
  181. }
  182. catch
  183. {
  184. MessageBox.Show("Select a device driver and try again!");
  185. }
  186. }
  187. private void MenuPause(object sender, EventArgs e)
  188. {
  189. System.ServiceProcess.ServiceController tmpSC;
  190. tmpSC=new System.ServiceProcess.ServiceController();
  191. tmpSC.MachineName=strMachineName;
  192. tmpSC.DisplayName=strDisplayName;
  193. try
  194. {
  195. tmpSC.Pause();
  196. System.Threading.Thread.Sleep(500);
  197. //wait for the service to pause
  198. while(tmpSC.Status == ServiceControllerStatus.PausePending)
  199. {
  200. Application.DoEvents();
  201. }
  202. lstDrvPaused.Items.Add(lstCurrent.SelectedItem.ToString());
  203. lstCurrent.Items.Remove(lstCurrent.SelectedIndex);
  204. lstCurrent.Refresh();
  205. lstDrvPaused.Refresh();
  206. }
  207. catch
  208. {
  209. MessageBox.Show("Service: " + strDisplayName + " Could not be paused !");
  210. }
  211. }
  212. //Load all the drivers on the given machine
  213. public void LoadDrivers()
  214. {
  215. if(!strMachineName.Equals(""))
  216. {
  217. ServiceController[]arrDrvCtrl;
  218. try
  219. {
  220. //Get an array with all the devices on the machine
  221. arrDrvCtrl= ServiceController.GetDevices(strMachineName);
  222. string [] strTmp = new string[arrDrvCtrl.GetUpperBound(0)];
  223. for(int iIndex=0;iIndex<arrDrvCtrl.GetUpperBound(0);iIndex++ )
  224. strTmp[iIndex++]=arrDrvCtrl[iIndex].ServiceName;
  225. //Sort them by name
  226. System.Array.Sort(strTmp,arrDrvCtrl,0,arrDrvCtrl.GetUpperBound(0));
  227. lstDrvRun.Items.Clear();
  228. lstDrvPaused.Items.Clear();
  229. lstDrvStopped.Items.Clear();
  230. //Check the status for each service/device
  231. foreach(ServiceController tmpSC in arrDrvCtrl)
  232. {
  233. if(tmpSC.Status==ServiceControllerStatus.Running )
  234. {
  235. lstDrvRun.Items.Add (tmpSC.DisplayName );
  236. }
  237. else if(tmpSC.Status== ServiceControllerStatus.Paused )
  238. lstDrvPaused.Items.Add(tmpSC.DisplayName);
  239. else
  240. lstDrvStopped.Items.Add(tmpSC.DisplayName);
  241. }
  242. lstDrvPaused.Sorted=lstDrvRun.Sorted=lstDrvStopped.Sorted =true;
  243. }
  244. catch(Exception ex)
  245. {
  246. MessageBox.Show(ex.ToString());
  247. }
  248. }
  249. }
  250. //Used to show the device info
  251. public void ShowDriverInfo(object sender, EventArgs e)
  252. {
  253. System.ServiceProcess.ServiceController tmpSC;
  254. tmpSC=new System.ServiceProcess.ServiceController();
  255. tmpSC.MachineName=strMachineName;
  256. tmpSC.DisplayName=strDisplayName;
  257. ServiceInfo si = new ServiceInfo(tmpSC);
  258. si.Show();
  259. }
  260. }
  261. }