博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用WINAPI安装Windows服务[转]
阅读量:4961 次
发布时间:2019-06-12

本文共 5493 字,大约阅读时间需要 18 分钟。

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        ///         /// 应用程序入口.        ///         [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();        }        ///         /// 安装和运行        ///         /// 程序路径.        /// 服务名        /// 服务显示名称.        /// 
服务安装是否成功.
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; } } /// /// 反安装服务. /// /// 服务名. 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; } }}

  

转载于:https://www.cnblogs.com/liulun/archive/2011/10/25/2223906.html

你可能感兴趣的文章
Spring Cloud 注册中心Eureka
查看>>
py-day2-sys模块、os模块、运算符、列表、字典
查看>>
HTTP响应码
查看>>
Java中String字符串常量池
查看>>
模拟器虚拟键盘不起作用
查看>>
django-beautifulsoup的简单使用
查看>>
【Marschner模型】Light Scattering from Human Hair Fibers人类头发纤维的光照散射
查看>>
实验一作业
查看>>
设置RDLC中table控件的表头在每页显示
查看>>
time及各种cpu时间
查看>>
MFC reference
查看>>
扎马步-计算机网络和系统基础知识
查看>>
64位/32位
查看>>
浅谈对Struts2上传文件的理解
查看>>
看完这篇分析,还不懂分布式事物,请给我差评
查看>>
C#中的委托和事件(续)
查看>>
Codeforces Round #445 Div. 2 A ACM ICPC+B Vlad and Cafes
查看>>
C# Excel数据导入到数据库
查看>>
c# 去除字符串中重复字符
查看>>
POJ 1147
查看>>