科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道高手进阶:给WINDOWS服务加上描述之三

高手进阶:给WINDOWS服务加上描述之三

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   当我们给服务增加安装包时,我们可以在ProjectInstaller里加上我们修改服务描述的代码 private void InitializeComponent() { //这里要增加代码 this.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.ProjectInstaller_AfterInstall);

作者:中国IT实验室 来源:中国IT实验室 2007年9月11日

关键字: 服务 Windows 编程

  • 评论
  • 分享微博
  • 分享邮件
¡¡¡¡

µ±ÎÒÃǸø·þÎñÔö¼Ó°²×°°üʱ£¬ÎÒÃÇ¿ÉÒÔÔÚProjectInstallerÀï¼ÓÉÏÎÒÃÇÐ޸ķþÎñÃèÊöµÄ´úÂë

private void InitializeComponent()
  {

//ÕâÀïÒªÔö¼Ó´úÂë
this.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.ProjectInstaller_AfterInstall);

}

private void ProjectInstaller_AfterInstall(object sender,System.Configuration.Install.InstallEventArgs e)
  {

   int iSCManagerHandle = 0;
   int iSCManagerLockHandle = 0;
   int iServiceHandle = 0;
   bool bChangeServiceConfig = false;
   bool bChangeServiceConfig2 = false;
   modAPI.SERVICE_DESCRIPTION ServiceDescription;
   modAPI.SERVICE_FAILURE_ACTIONS ServiceFailureActions;
   modAPI.SC_ACTION[] ScActions = new modAPI.SC_ACTION[3];

   bool bCloseService = false;
   bool bUnlockSCManager = false;
   bool bCloseSCManager = false;

   IntPtr iScActionsPointer = new IntPtr();

   try
   {
    //´ò¿ª·þÎñ¿ØÖÆ̨
    iSCManagerHandle = modAPI.OpenSCManagerA(null, null,
     modAPI.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);

   
    if (iSCManagerHandle < 1)
    {
     throw new Exception("²»ÄÜ´ò¿ª·þÎñ¹ÜÀíÆ÷.");
    }

   
    iSCManagerLockHandle = modAPI.LockServiceDatabase(iSCManagerHandle);

   
    if (iSCManagerLockHandle < 1)
    {
     throw new Exception("²»ÄÜËø¶¨·þÎñ¹ÜÀíÆ÷.");
    }

    //·þÎñÃû
    iServiceHandle = modAPI.OpenServiceA(iSCManagerHandle, "JadeWatchService",
     modAPI.ACCESS_TYPE.SERVICE_ALL_ACCESS);

   
    if (iServiceHandle < 1)
    {
     throw new Exception("²»ÄÜ´ò¿ª·þÎñ½øÐÐÐÞ¸Ä.");
    }
 
  
    bChangeServiceConfig = modAPI.ChangeServiceConfigA(iServiceHandle,
     modAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS | modAPI.ServiceType.SERVICE_INTERACTIVE_PROCESS,
     modAPI.SERVICE_NO_CHANGE, modAPI.SERVICE_NO_CHANGE, null, null,
     0, null, null, null, null);

   
    if (bChangeServiceConfig==false)
    {
     throw new Exception("²»Äܸıä·þÎñÉèÖÃ.");
    }

  
    ServiceDescription.lpDescription = "ÇàÄñÎļþ¼à¿Ø·þÎñ,Èç¹ûÍ£Ö¹¸Ã·þÎñ,Êý¾Ý½«²»ÄÜÕý³£½øÐб¸·Ý!";


    bChangeServiceConfig2 = modAPI.ChangeServiceConfig2A(iServiceHandle,
     modAPI.InfoLevel.SERVICE_CONFIG_DESCRIPTION,ref ServiceDescription);

  
    if (bChangeServiceConfig2==false)
    {
     throw new Exception("²»ÄܽøÐзþÎñÃèÊö¸ü¸Ä.");
    } 

  
    ServiceFailureActions.dwResetPeriod = 600;
    ServiceFailureActions.lpRebootMsg = "·þÎñÆô¶¯Ê§°Ü! ÖØÆôÖÐ...";
    // ServiceFailureActions.lpCommand = "SomeCommand.exe Param1 Param2";
    ServiceFailureActions.lpCommand = "";
    ServiceFailureActions.cActions = ScActions.Length;

    //¹ÊÕϻָ´ÉèÖã¬ÕâÀïûÓÐÉèÖÃ
    ScActions[0].Delay = 20000;
    ScActions[0].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE; //²»Òª¶Ôʧ°Ü²Ù×÷×öÈκδ¦Àí,Èç¹ûÖØÆô·þÎñµÈ
    ScActions[1].Delay = 20000;
    ScActions[1].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;
    ScActions[2].Delay = 20000;
    ScActions[2].SCActionType = modAPI.SC_ACTION_TYPE.SC_ACTION_NONE;

   
    iScActionsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);

   
    modAPI.CopyMemory(iScActionsPointer, ScActions, Marshal.SizeOf(new modAPI.SC_ACTION()) * 3);


 

²é¿´±¾ÎÄÀ´Ô´

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章