zoukankan      html  css  js  c++  java
  • Windows 程序注册成服务的方法

    Windows 程序注册成服务的方法

             将windows 程序注册成服务这个是很多后台程序需要实现的功能,注册成服务后,你的程序就可以像windows 服务一样随系统启动,并且隐藏你的控制台界面。下面介绍一个网上流传最广泛的方法。就是Instsrv.exe + Srvany.exe .

       srvany.exe是什么?srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行。也就是说srvany只是其注册程序的服务外壳,这个特性对于我们来说非常实用,我们可以通过它让我们的程序以SYSTEM账户启动,或者实现随机器启动而自启动。

       第一步:编写好我们的控制台程序 .

    namespace showlog
    {
        class Program
        {
            static void Main(string[] args)
            {
                while(true)
                {
                    FileStream fs = new FileStream("E:\ak.txt", FileMode.OpenOrCreate|FileMode.Append);
                    //获得字节数组
                    byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!");
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();
                    Thread.Sleep(1000);
                }
                
            }
        }
    }

    改程序是循环在E 盘写文件。

    第二步:编辑注册表,好让系统启动我们的程序。配置的方法是,开始 - 运行 - regedit,打开注册表,定位到下面的路径。HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesServiceName

    如果该服务名下没有Parameters项目,则对服务名称项目右击新建项,名称为Parameters,然后定位到Parameters项,新建以下几个字符串值。

      名称 Application 值为你要作为服务运行的程序地址。

      名称 AppDirectory 值为你要作为服务运行的程序所在文件夹路径。

    名称 AppParameters 值为你要作为服务运行的程序启动所需要的参数。

    工程实施的时候手动添加肯定是很头疼的一件事,所以我们写一个reg 文件。

    Windows Registry Editor Version 5.00 
     
    [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesShowLogParameters] 
    "Application"="C:\Users\lining\Desktop\Debug\showlog.exe"
    "AppDirectory"="C:\Users\lining\Desktop\Debug"
    "AppParameters"=""

    这个文件双击就可以进行自动添加注册表了。

    第三步:注册服务。

    我们需要把instsrv.exe 和 srvany.exe 拷贝至system32 目录下(64位拷贝到syswow64目录下)

    安装方法:instsrv ServiceName C:WindowsSystem32srvany.exe

    卸载方法:instsrv ServiceName remove

    现在我们这这些过程写进批处理文件Install.bat,就不用我们一个一个的敲命令了。

    @echo off
    echo move file...
    set curDir=%~dp0
    set chargereg=%curDir%showlog.reg
    
    copy %curDir%instsrv.exe c:\windows\SysWOW64\instsrv.exe
    copy %curDir%srvany.exe c:\windows\SysWOW64\srvany.exe
    
    echo reg ...
    start %chargereg%
    choice /t 2 /d y /n >nul
    echo create service ...
    start c:windowsSysWOW64instsrv.exe ShowLog c:windowsSysWOW64srvany.exe
    choice /t 2 /d y /n >nul
    net start ShowLog
    echo install program will exit after three seconds
    choice /t 2 /d y /n >nul

    “choice /t 2 /d y /n >nul”这句命令的目的是让程序暂停2秒钟。

    当然卸载服务Uninstall.bat就很简单了,双击卸载该服务

    @echo off
    echo ShowLog.
    net stop ShowLog
    choice /t 2 /d y /n >nul
    start c:windowsSysWOW64instsrv.exe ShowLog remove
    echo uninstall program will exit after two second 
    choice /t 2 /d y /n >nul
  • 相关阅读:
    SAP全球企业官孙小群的生活智慧
    C++ vs Python向量运算速度评测
    C++ Error: no appropriate default constructor available
    危险的浮点数float
    Vagrant 手册之 Vagrantfile
    MySQL 服务器性能剖析
    Vagrant 手册之多个虚拟机 multi-machine
    Vagrant 手册之同步目录
    Vagrant 手册之同步目录
    MySQL 中的 information_schema 数据库
  • 原文地址:https://www.cnblogs.com/techdreaming/p/7283341.html
Copyright © 2011-2022 走看看