-
C#教程之使用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;
}
}
}
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式