注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 电脑安全系统安全 → 文章内容

Install Windows Service with Interacted with Desktop

作者:佚名 来源:本站整理 发布时间:2008-11-10 20:41:49
view plaincopy to clipboardprint?
  1. Private Sub ProjectInstaller_AfterInstall(ByVal sender As ObjectByVal e As System.Configuration.Install.InstallEventArgs) Handles MyBase.AfterInstall   
  2.   
  3.     'Our code goes in this event because it is the only one that will do   
  4.   
  5.     'a proper job of letting the user know that an error has occurred,   
  6.   
  7.     'if one indeed occurs. Installation will be rolled back if an error occurs.   
  8.   
  9.   
  10.   
  11.     Dim iSCManagerHandle As Integer  
  12.   
  13.     Dim iSCManagerLockHandle As Integer  
  14.   
  15.     Dim iServiceHandle As Integer  
  16.   
  17.     Dim bChangeServiceConfig As Boolean  
  18.   
  19.     Dim bChangeServiceConfig2 As Boolean  
  20.   
  21.     Dim ServiceDescription As modAPI.SERVICE_DESCRIPTION   
  22.   
  23.     Dim ServiceFailureActions As modAPI.SERVICE_FAILURE_ACTIONS   
  24.   
  25.     Dim ScActions(2) As modAPI.SC_ACTION     'There should be one element for each   
  26.   
  27.     'action. The Services snap-in shows 3 possible actions.   
  28.   
  29.   
  30.   
  31.     Dim bCloseService As Boolean  
  32.   
  33.     Dim bUnlockSCManager As Boolean  
  34.   
  35.     Dim bCloseSCManager As Boolean  
  36.   
  37.   
  38.   
  39.     Dim iScActionsPointer As New IntPtr   
  40.   
  41.   
  42.   
  43.     Try  
  44.   
  45.         'Obtain a handle to the Service Control Manager, with appropriate rights.   
  46.   
  47.         'This handle is used to open the relevant service.   
  48.   
  49.         iSCManagerHandle = modAPI.OpenSCManager(vbNullString, vbNullString, _   
  50.   
  51.         modAPI.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS)   
  52.   
  53.   
  54.   
  55.         'Check that it's open. If not throw an exception.   
  56.   
  57.         If iSCManagerHandle < 1 Then  
  58.   
  59.             Throw New Exception("Unable to open the Services Manager.")   
  60.   
  61.         End If  
  62.   
  63.   
  64.   
  65.         'Lock the Service Control Manager database.   
  66.   
  67.         iSCManagerLockHandle = modAPI.LockServiceDatabase(iSCManagerHandle)   
  68.   
  69.   
  70.   
  71.         'Check that it's locked. If not throw an exception.   
  72.   
  73.         If iSCManagerLockHandle < 1 Then  
  74.   
  75.             Throw New Exception("Unable to lock the Services Manager.")   
  76.   
  77.         End If  
  78.   
  79.   
  80.   
  81.         'Obtain a handle to the relevant service, with appropriate rights.   
  82.   
  83.         'This handle is sent along to change the settings. The second parameter   
  84.   
  85.         'should contain the name you assign to the service.   
  86.   
  87.         iServiceHandle = modAPI.OpenService(iSCManagerHandle, "ServiceTest", _   
  88.   
  89.         modAPI.ACCESS_TYPE.SERVICE_ALL_ACCESS)   
  90.   
  91.   
  92.   
  93.         'Check that it's open. If not throw an exception.   
  94.   
  95.         If iServiceHandle < 1 Then  
  96.   
  97.             Throw New Exception("Unable to open the Service for modification.")   
  98.   
  99.         End If  
  100.   
  101.   
  102.   
  103.         'Call ChangeServiceConfig to update the ServiceType to SERVICE_INTERACTIVE_PROCESS.   
  104.   
  105.         'Very important is that you do not leave out or change the other relevant   
  106.   
  107.         'ServiceType settings. The call will return False if you do.   
  108.   
  109.         'Also, only services that use the LocalSystem account can be set to   
  110.   
  111.         'SERVICE_INTERACTIVE_PROCESS.   
  112.   
  113.         bChangeServiceConfig = modAPI.ChangeServiceConfig(iServiceHandle, _   
  114.   
  115.         modAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS + modAPI.ServiceType.SERVICE_INTERACTIVE_PROCESS, _   
  116.   
  117.         SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, vbNullString, vbNullString, _   
  118.   
  119.         vbNullString, vbNullString, vbNullString, vbNullString, vbNullString)   
  120.   
  121.   
  122.   
  123.         'If the call is unsuccessful, throw an exception.   
  124.   
  125.         If Not bChangeServiceConfig Then  
  126.   
  127.             Throw New Exception("Unable to change the Service settings.")   
  128.   
  129.         End If  
  130.   
  131.   
  132.   
  133.         'To change the description, create an instance of the SERVICE_DESCRIPTION   
  134.   
  135.         'structure and set the lpDescription member to your desired description.   
  136.   
  137.         ServiceDescription.lpDescription = "This is my custom description for my Windows Service Application!"  
  138.   
  139.   
  140.   
  141.         'Call ChangeServiceConfig2 with SERVICE_CONFIG_DESCRIPTION in the second   
  142.   
  143.         'parameter and the SERVICE_DESCRIPTION instance in the third parameter   
  144.   
  145.         'to update the description.   
  146.   
  147.         bChangeServiceConfig2 = modAPI.ChangeServiceConfig2(iServiceHandle, _   
  148.   
  149.         modAPI.InfoLevel.SERVICE_CONFIG_DESCRIPTION, ServiceDescription)   
  150.   
  151.   
  152.   
  153.         'If the update of the description is unsuccessful it is up to you to   
  154.   
  155.         'throw an exception or not. The fact that the description did not update   
  156.   
  157.         'should not impact the functionality of your service.   
  158.   
  159.         If Not bChangeServiceConfig2 Then  
  160.   
  161.             Throw New Exception("Unable to set the Service description.")   
  162.   
  163.         End If  
  164.   
  165.   
  166.   
  167.         'To change the Service Failure Actions, create an instance of the   
  168.   
  169.         'SERVICE_FAILURE_ACTIONS structure and set the members to your   
  170.   
  171.         'desired values. See MSDN for detailed descriptions.   
  172.   
  173.         ServiceFailureActions.dwResetPeriod = 600   
  174.   
  175.         ServiceFailureActions.lpRebootMsg = "Service failed to start! Rebooting..."  
  176.   
  177.         ServiceFailureActions.lpCommand = "SomeCommand.exe Param1 Param2"  
  178.   
  179.         ServiceFailureActions.cActions = ScActions.Length   
  180.   
  181.   
  182.   
  183.         'The lpsaActions member of SERVICE_FAILURE_ACTIONS is a pointer to an   
  184.   
  185.         'array of SC_ACTION structures. This complicates matters a little,   
  186.   
  187.         'and although it took me a week to figure it out, the solution   
  188.   
  189.         'is quite simple.   
  190.   
  191.   
  192.   
  193.         'First order of business is to populate our array of SC_ACTION structures   
  194.   
  195.         'with appropriate values.   
  196.   
  197.         ScActions(0).Delay = 20000   
  198.   
  199.         ScActions(0).SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_RESTART   
  200.   
  201.         ScActions(1).Delay = 20000   
  202.   
  203.         ScActions(1).SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_RUN_COMMAND   
  204.   
  205.         ScActions(2).Delay = 20000   
  206.   
  207.         ScActions(2).SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_REBOOT   
  208.   
  209.   
  210.   
  211.         'Once that's done, we need to obtain a pointer to a memory location   
  212.   
  213.         'that we can assign to lpsaActions in SERVICE_FAILURE_ACTIONS.   
  214.   
  215.         'We use 'Marshal.SizeOf(New modAPI.SC_ACTION) * 3' because we pass    
  216.   
  217.         '3 actions to our service. If you have less actions change the * 3 accordingly.   
  218.   
  219.         iScActionsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(New modAPI.SC_ACTION) * 3)   
  220.   
  221.   
  222.   
  223.         'Once we have obtained the pointer for the memory location we need to   
  224.   
  225.         'fill the memory with our structure. We use the CopyMemory API function   
  226.   
  227.         'for this. Please have a look at it's declaration in modAPI.   
  228.   
  229.         modAPI.CopyMemory(iScActionsPointer, ScActions, Marshal.SizeOf(New modAPI.SC_ACTION) * 3)   
  230.   
  231.   
  232.   
  233.         'We set the lpsaActions member of SERVICE_FAILURE_ACTIONS to the integer   
  234.   
  235.         'value of our pointer.   
  236.   
  237.         ServiceFailureActions.lpsaActions = iScActionsPointer.ToInt32   
  238.   
  239.   
  240.   
  241.         'We call bChangeServiceConfig2 with the relevant parameters.   
  242.   
  243.         bChangeServiceConfig2 = modAPI.ChangeServiceConfig2(iServiceHandle, _   
  244.   
  245.         modAPI.InfoLevel.SERVICE_CONFIG_FAILURE_ACTIONS, ServiceFailureActions)   
  246.   
  247.   
  248.   
  249.         'If the update of the failure actions are unsuccessful it is up to you to   
  250.   
  251.         'throw an exception or not. The fact that the failure actions did not update   
  252.   
  253.         'should not impact the functionality of your service.   
  254.   
  255.         If Not bChangeServiceConfig2 Then  
  256.   
  257.             Throw New Exception("Unable to set the Service failure actions.")   
  258.   
  259.         End If  
  260.   
  261.     Catch ex As Exception   
  262.   
  263.         Throw ex   
  264.   
  265.     Finally  
  266.   
  267.         'Once you are done you should close/release the handles to the service   
  268.   
  269.         'and the Service Control Manager, as well as free the memory that held   
  270.   
  271.         'the array of SC_ACTION structures.   
  272.   
  273.   
  274.   
  275.         Marshal.FreeHGlobal(iScActionsPointer)   
  276.   
  277.         If iServiceHandle > 0 Then  
  278.   
  279.             bCloseService = modAPI.CloseServiceHandle(iServiceHandle)   
  280.   
  281.         End If  
  282.   
  283.   
  284.   
  285.         bUnlockSCManager = modAPI.UnlockServiceDatabase(iSCManagerLockHandle)   
  286.   
  287.   
  288.   
  289.         If iSCManagerHandle > 0 Then  
  290.   
  291.             bCloseSCManager = modAPI.CloseServiceHandle(iSCManagerHandle)   
  292.   
  293.         End If  
  294.   
  295.     End Try  
  296.   
  297.   
  298.   
  299.     'When installation is done go check out your handy work using Computer Management!   
  300.   
  301. End Sub  

  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved