使用WINAPI安装Windows服务[转]

简介: using system; using system.runtime.interopservices; namespace myserviceinstaller { class serviceinstaller { #region private ...
using system;
using system.runtime.interopservices;
namespace myserviceinstaller
{

    class serviceinstaller
    {
        #region private variables
        private string _servicepath;
        private string _servicename;
        private string _servicedisplayname;
        #endregion private variables
        #region dllimport
        [dllimport("advapi32.dll")]
        public static extern intptr openscmanager(string lpmachinename, string lpscdb, int scparameter);
        [dllimport("advapi32.dll")]
        public static extern intptr createservice(intptr sc_handle, string lpsvcname, string lpdisplayname,
        int dwdesiredaccess, int dwservicetype, int dwstarttype, int dwerrorcontrol, string lppathname,
        string lploadordergroup, int lpdwtagid, string lpdependencies, string lpservicestartname, string lppassword);
        [dllimport("advapi32.dll")]
        public static extern void closeservicehandle(intptr schandle);
        [dllimport("advapi32.dll")]
        public static extern int startservice(intptr svhandle, int dwnumserviceargs, string lpserviceargvectors);
        [dllimport("advapi32.dll", setlasterror = true)]
        public static extern intptr openservice(intptr schandle, string lpsvcname, int dwnumserviceargs);
        [dllimport("advapi32.dll")]
        public static extern int deleteservice(intptr svhandle);
        [dllimport("kernel32.dll")]
        public static extern int getlasterror();
        #endregion dllimport
        /// <summary>
        /// 应用程序入口.
        /// </summary>

        [stathread]
        static void main(string[] args)
        {

            string svcpath;
            string svcname;
            string svcdispname;
            //服务程序的路径
            svcpath = @"c:\myservice.exe";
            svcdispname = "myservice";
            svcname = "myservice";
            serviceinstaller c = new serviceinstaller();
            c.installservice(svcpath, svcname, svcdispname);
            console.read();

        }

        /// <summary>
        /// 安装和运行
        /// </summary>
        /// <param name="svcpath">程序路径.</param>
        /// <param name="svcname">服务名</param>
        /// <param name="svcdispname">服务显示名称.</param>
        /// <returns>服务安装是否成功.</returns>
        public bool installservice(string svcpath, string svcname, string svcdispname)
        {
            #region constants declaration.
            int sc_manager_create_service = 0x0002;
            int service_win32_own_process = 0x00000010;
            //int service_demand_start = 0x00000003;
            int service_error_normal = 0x00000001;
            int standard_rights_required = 0xf0000;
            int service_query_config = 0x0001;
            int service_change_config = 0x0002;
            int service_query_status = 0x0004;
            int service_enumerate_dependents = 0x0008;
            int service_start = 0x0010;
            int service_stop = 0x0020;
            int service_pause_continue = 0x0040;
            int service_interrogate = 0x0080;
            int service_user_defined_control = 0x0100;
            int service_all_access = (standard_rights_required |
            service_query_config |
            service_change_config |
            service_query_status |
            service_enumerate_dependents |
            service_start |
            service_stop |
            service_pause_continue |
            service_interrogate |
            service_user_defined_control);
            int service_auto_start = 0x00000002;
            #endregion constants declaration.
            try
            {
                intptr sc_handle = openscmanager(null, null, sc_manager_create_service);
                if (sc_handle.toint32() != 0)
                {
                    intptr sv_handle = createservice(sc_handle, svcname, svcdispname, service_all_access, service_win32_own_process, service_auto_start, service_error_normal, svcpath, null, 0, null, null, null);
                    if (sv_handle.toint32() == 0)
                    {
                        closeservicehandle(sc_handle);
                        return false;
                    }
                    else
                    {
                        //试尝启动服务
                        int i = startservice(sv_handle, 0, null);
                        if (i == 0)
                        {

                            return false;
                        }

                        closeservicehandle(sc_handle);
                        return true;
                    }
                }
                else

                    return false;
            }
            catch (exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 反安装服务.
        /// </summary>
        /// <param name="svcname">服务名.</param>
        public bool uninstallservice(string svcname)
        {
            int generic_write = 0x40000000;
            intptr sc_hndl = openscmanager(null, null, generic_write);
            if (sc_hndl.toint32() != 0)
            {
                int delete = 0x10000;
                intptr svc_hndl = openservice(sc_hndl, svcname, delete);
                if (svc_hndl.toint32() != 0)
                {
                    int i = deleteservice(svc_hndl);
                    if (i != 0)
                    {
                        closeservicehandle(sc_hndl);
                        return true;
                    }
                    else
                    {
                        closeservicehandle(sc_hndl);
                        return false;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        }
    }
}

  

目录
相关文章
|
11天前
|
机器学习/深度学习 并行计算 异构计算
WINDOWS安装eiseg遇到的问题和解决方法
通过本文的详细步骤和问题解决方法,希望能帮助你顺利在 Windows 系统上安装和运行 EISeg。
29 2
|
19天前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
|
21天前
|
NoSQL Linux PHP
如何在不同操作系统上安装 Redis 服务器,包括 Linux 和 Windows 的具体步骤
本文介绍了如何在不同操作系统上安装 Redis 服务器,包括 Linux 和 Windows 的具体步骤。接着,对比了两种常用的 PHP Redis 客户端扩展:PhpRedis 和 Predis,详细说明了它们的安装方法及优缺点。最后,提供了使用 PhpRedis 和 Predis 在 PHP 中连接 Redis 服务器及进行字符串、列表、集合和哈希等数据类型的基本操作示例。
46 4
|
2月前
|
数据安全/隐私保护 Windows
安装 Windows Server 2019
安装 Windows Server 2019
|
2月前
|
Windows
安装 Windows Server 2003
安装 Windows Server 2003
|
2月前
|
Linux 网络安全 虚拟化
适用于Linux的Windows子系统(WSL1)的安装与使用记录
并放到启动文件夹,就可以开机自动启动了。
64 0
|
2月前
|
Windows
安装Windows XP系统
安装Windows XP系统