使用winapi安裝Windows服務(wù)示例程序
更新時(shí)間:2014年01月11日 11:15:06 作者:
這篇文章主要介紹了使用winapi安裝Windows服務(wù)示例,大家參考使用吧
復(fù)制代碼 代碼如下:
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>
/// 應(yīng)用程序入口.
/// </summary>
[stathread]
static void main(string[] args)
{
string svcpath;
string svcname;
string svcdispname;
//服務(wù)程序的路徑
svcpath = @"c:\myservice.exe";
svcdispname = "myservice";
svcname = "myservice";
serviceinstaller c = new serviceinstaller();
c.installservice(svcpath, svcname, svcdispname);
console.read();
}
/// <summary>
/// 安裝和運(yùn)行
/// </summary>
/// <param name="svcpath">程序路徑.</param>
/// <param name="svcname">服務(wù)名</param>
/// <param name="svcdispname">服務(wù)顯示名稱.</param>
/// <returns>服務(wù)安裝是否成功.</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
{
//試嘗啟動(dòng)服務(wù)
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>
/// 反安裝服務(wù).
/// </summary>
/// <param name="svcname">服務(wù)名.</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;
}
}
}
您可能感興趣的文章:
- 使用WindowsAPI獲取錄音音頻的方法
- python調(diào)用windows api鎖定計(jì)算機(jī)示例
- C#中調(diào)用Windows API的技術(shù)要點(diǎn)說明
- 基于Windows API分解路徑問題的詳解
- Windows API函數(shù)大全(完整)
- WINDOWS 2000下使用ISAPI方式安裝PHP
- Windows程序內(nèi)部運(yùn)行機(jī)制實(shí)例詳解
- Windows中使用計(jì)劃任務(wù)自動(dòng)執(zhí)行PHP程序?qū)嵗?/a>
- 深入詳解C編寫Windows服務(wù)程序的五個(gè)步驟
- 使用WindowsAPI實(shí)現(xiàn)播放PCM音頻的方法
相關(guān)文章
C#添加Windows服務(wù) 定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
c#使用UTF-8編碼實(shí)現(xiàn)處理多語言文本
UTF-8編碼是現(xiàn)代應(yīng)用中處理多語言文本的首選,所以本文為大家詳細(xì)介紹了C#如何使用UTF-8編碼實(shí)現(xiàn)處理多語言文本,感興趣的小伙伴可以了解下2024-01-01
C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的示例代碼
SignalR 是 ASP.NET Core 的一個(gè)庫,它簡(jiǎn)化了在應(yīng)用程序中添加實(shí)時(shí)通信的過程,無論是聊天應(yīng)用、實(shí)時(shí)游戲還是協(xié)作工具,SignalR 都能提供高效且易于實(shí)現(xiàn)的解決方案,本文給大家介紹了C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的實(shí)現(xiàn),需要的朋友可以參考下2024-10-10
Unity3D實(shí)現(xiàn)鼠標(biāo)控制旋轉(zhuǎn)轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)鼠標(biāo)控制旋轉(zhuǎn)轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
WPF實(shí)現(xiàn)帶篩選功能的DataGrid
在默認(rèn)情況下,WPF提供的DataGrid僅擁有數(shù)據(jù)展示等簡(jiǎn)單功能,如果要實(shí)現(xiàn)像Excel一樣復(fù)雜的篩選過濾功能,則相對(duì)比較麻煩。本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過WPF實(shí)現(xiàn)DataGrid的篩選功能,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正2023-03-03
使用C#實(shí)現(xiàn)網(wǎng)頁內(nèi)容保存為圖片并生成壓縮包
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)網(wǎng)頁內(nèi)容保存為圖片并生成壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02

