最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

相關(guān)文章

  • C#的編碼規(guī)范詳細(xì)說明

    C#的編碼規(guī)范詳細(xì)說明

    編碼規(guī)范是老生常談的問題,現(xiàn)在再看代碼規(guī)范可能不會(huì)再去在意變量,控件的命名方法等,而是更加關(guān)注代碼的實(shí)用性
    2013-08-08
  • C#中隱式運(yùn)行CMD命令行窗口的方法

    C#中隱式運(yùn)行CMD命令行窗口的方法

    下面介紹一種常用的在C#程序中調(diào)用CMD.exe程序,并且不顯示命令行窗口界面,來完成CMD中各種功能的簡(jiǎn)單方法。
    2011-04-04
  • C#添加Windows服務(wù) 定時(shí)任務(wù)

    C#添加Windows服務(wù) 定時(shí)任務(wù)

    這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • c#使用UTF-8編碼實(shí)現(xiàn)處理多語言文本

    c#使用UTF-8編碼實(shí)現(xiàn)處理多語言文本

    UTF-8編碼是現(xiàn)代應(yīng)用中處理多語言文本的首選,所以本文為大家詳細(xì)介紹了C#如何使用UTF-8編碼實(shí)現(xiàn)處理多語言文本,感興趣的小伙伴可以了解下
    2024-01-01
  • C#實(shí)現(xiàn)飛行棋(Winform)

    C#實(shí)現(xiàn)飛行棋(Winform)

    這篇文章主要為大家詳細(xì)介紹了基于Winform框架的飛行棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的示例代碼

    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)盤

    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
  • c# 代理模式

    c# 代理模式

    代理模式:為其他對(duì)象提供一種代理以控制其他對(duì)象的訪問
    2012-10-10
  • WPF實(shí)現(xiàn)帶篩選功能的DataGrid

    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)容保存為圖片并生成壓縮包

    使用C#實(shí)現(xiàn)網(wǎng)頁內(nèi)容保存為圖片并生成壓縮包

    這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)網(wǎng)頁內(nèi)容保存為圖片并生成壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02

最新評(píng)論

神池县| 琼中| 汶上县| 手游| 商南县| 旬邑县| 广西| 沙洋县| 平邑县| 阜新市| 城固县| 新邵县| 育儿| 咸宁市| 银川市| 饶平县| 棋牌| 玉屏| 邯郸县| 南丹县| 美姑县| 太谷县| 榆树市| 宝清县| 长汀县| 梅州市| 南川市| 阳春市| 昌江| 巫山县| 定日县| 贡嘎县| 邢台县| 鹤峰县| 界首市| 泾川县| 静海县| 平昌县| 柏乡县| 穆棱市| 岗巴县|