使用winapi安装Windows服务示例程序

 更新时间:2014年01月11日 11:15:06   作者:  
这篇文章主要介绍了使用winapi安装Windows服务示例,大家参考使用吧

复制代码 代码如下:

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;
        }
    }
}

相关文章

  • C#中整理了几种字符串截取方法小结

    C#中整理了几种字符串截取方法小结

    本文给大家整理了几种字符串截取方法, (Substring);(Remove);(Replace)方法和split方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • C#控制台程序中使用官方依赖注入的实现

    C#控制台程序中使用官方依赖注入的实现

    这篇文章主要介绍了C#控制台程序中使用官方依赖注入的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Winform中如何跨线程访问UI元素

    Winform中如何跨线程访问UI元素

    这篇文章主要介绍了Winform中如何跨线程访问UI元素,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-10-10
  • C# WinForm窗体编程中处理数字的正确操作方法

    C# WinForm窗体编程中处理数字的正确操作方法

    这篇文章主要介绍了C# WinForm窗体编程中处理数字的正确操作方法,本文给出了正确示例,并解释了为什么要这么做,需要的朋友可以参考下
    2014-08-08
  • Unity实现场景加载功能

    Unity实现场景加载功能

    这篇文章主要为大家详细介绍了Unity实现场景加载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • 解答“60k”大佬的19道C#面试题(上)

    解答“60k”大佬的19道C#面试题(上)

    这篇文章主要解答了“60k”大佬的19道C#面试题中的10道,文中的面试题比较小众,作者给了不错的答案,相信对你以后的面试有所帮助,感兴趣就来了解下
    2020-06-06
  • C#学习基础概念二十五问续2

    C#学习基础概念二十五问续2

    C#学习基础概念二十五问续2...
    2007-04-04
  • C#集合之自定义集合类

    C#集合之自定义集合类

    这篇文章介绍了C#集合之自定义集合类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • ZooKeeper的安装及部署教程

    ZooKeeper的安装及部署教程

    Zookeeper是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等,这篇文章主要介绍了ZooKeeper的安装及部署,需要的朋友可以参考下
    2019-06-06
  • C#获取计算机名,IP,MAC信息实现代码

    C#获取计算机名,IP,MAC信息实现代码

    利用C#获取计算机名,IP,MAC信息如何实现,一直是网友们的头疼问题,本文整理了一些实现代码,需要的朋友可以参考下
    2012-11-11

最新评论