国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

木馬編程天天練 進(jìn)入第3天 服務(wù)管理

2019-11-17 03:58:07
字體:
供稿:網(wǎng)友
服務(wù)函數(shù)

下面的函數(shù)用于被服務(wù)執(zhí)行或者使用

函數(shù)            描述

Handler            An application-defined callback function used with the RegisterServiceCtrlHandler function.
HandlerEx    
RegisterServiceCtrlHandler    注冊一個(gè)函數(shù)處理控制碼請求。
RegisterServiceCtrlHandlerEx    
ServiceMain    服務(wù)程序入口函數(shù)。
SetServiceBits    Registers a service type with the service control manager and the Server service.
SetServiceStatus    Updates the service control manager's status information for the calling service.
StartServiceCtrlDispatcher    Connects the main thread of a service PRocess to the service control manager.



下面的函數(shù)被用于管理和配置服務(wù)

函數(shù)            描述

ChangeServiceConfig    改變服務(wù)的開機(jī)運(yùn)行狀態(tài)。
ChangeServiceConfig2    改變服務(wù)的描述。
CloseServiceHandle    關(guān)閉服務(wù)句柄。
ControlService            在一個(gè)服務(wù)已經(jīng)被開啟的情況下,向這個(gè)服務(wù)發(fā)出控制碼。
ControlServiceEx     
CreateService    創(chuàng)建一個(gè)服務(wù)對象,并增加它到服務(wù)控制管理數(shù)據(jù)庫
DeleteService    在服務(wù)控制管理數(shù)據(jù)庫中標(biāo)示要?jiǎng)h除的服務(wù)。
EnumDependentServices    獲取服務(wù)管理數(shù)據(jù)庫中所有服務(wù)的名稱和當(dāng)前狀態(tài)。
EnumServicesStatusEx    
GetServiceDisplayName    獲取服務(wù)的描述。
GetServiceKeyName    Retrieves the service name of the specified service.
NotifyBootConfigStatus    Reports the boot status to the service control manager.
NotifyServiceStatusChange    Enables an application to receive notification when the specified service is created or

deleted or when its status changes.
OpenSCManager    和指定機(jī)器的服務(wù)控制管理器建立連接并打開服務(wù)控制管理器數(shù)據(jù)庫。
OpenService    打開一個(gè)存在的服務(wù)。
QueryServiceConfig    
QueryServiceConfig2    
QueryServiceObjectSecurity    Retrieves a copy of the security descriptor associated with a service object.
QueryServiceStatusEx    查詢服務(wù)程序現(xiàn)在的運(yùn)行狀態(tài)。
SetServiceObjectSecurity    Sets the security descriptor of a service object.
StartService    開啟一個(gè)服務(wù)。

廢棄函數(shù)

下面的函數(shù)已經(jīng)被廢棄。

    EnumServicesStatus
    LockServiceDatabase
    QueryServiceLockStatus
    QueryServiceStatus
    UnlockServiceDatabase

Build date: 12/3/2009

程序例子:

#include<windows.h>
#include<stdio.h>

bool Start_Service(wchar_t * ServiceName);
bool Stop_Service(wchar_t * ServiceName);
bool Create_Service(wchar_t * ServiceName);
bool Delete_Service(wchar_t * ServiceName);
void ReconfigureService(wchar_t * ServiceName, wchar_t * ServiceDes);
void  ChangeServiceRun(wchar_t * ServiceName);
void EnumService(void);

int main()
{
    wchar_t * ServiceDisp = L"快速緩存服務(wù),為網(wǎng)絡(luò)文件交換提供緩存,提高網(wǎng)絡(luò)連接速度。";
    //Start_Service(L"WmdmPmSN");
    //Stop_Service(L"WmdmPmSN");
    //Create_Service(L"ServiceTest");
    //Delete_Service(L"ServiceTest");
    //ReconfigureService(L"ServiceTest",ServiceDisp);
    ChangeServiceRun(L"WmdmPmSN");
    EnumService();
    return 0;
}

bool Start_Service(wchar_t * ServiceName)
{
    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_access);
    if(NULL != schSCManager)
    {
        // L"WmdmPmSN"
        SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
        if( NULL != schService)
        {
            if(StartService(schService,0,NULL))
            {
                CloseServiceHandle(schService);
                CloseServiceHandle(schSCManager);
                return 1;
            }
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            wprintf(L"Start Service failed!/n");
            return 0;
        }
        CloseServiceHandle(schSCManager);
        wprintf(L"Open Service failed!/n");
        return 0;
    }    
    wprintf(L"OpenSCManager failed!/n");
    CloseServiceHandle(schSCManager);
    return 0;
}

bool  Stop_Service(wchar_t * ServiceName)
{
    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
    if(NULL != schSCManager)
    {
        // L"WmdmPmSN"
        SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
        if( NULL != schService)
        {
            SERVICE_STATUS ServiceStatus;
            if(ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus))
            {
                CloseServiceHandle(schService);
                CloseServiceHandle(schSCManager);
                return 1;
            }
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            wprintf(L"Start Service failed!/n");
            return 0;
        }
        CloseServiceHandle(schSCManager);
        wprintf(L"Open Service failed!/n");
        return 0;
    }    
    wprintf(L"OpenSCManager failed!/n");
    CloseServiceHandle(schSCManager);
    return 0;
    
}

bool Create_Service(wchar_t * ServiceName)
{
    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
    if(NULL != schSCManager)
    {
        wchar_t * DisplayName = L"Service Program Test/n";
        wchar_t * FilePathName = L"d://cyuyan//servicetest.exe";
        SC_HANDLE schService = CreateService(
            schSCManager,
            ServiceName,
            DisplayName,
            SC_MANAGER_ALL_ACCESS,
            SERVICE_WIN32_OWN_PROCESS,
            SERVICE_AUTO_START,
            SERVICE_ERROR_IGNORE,
            FilePathName,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL);
        if(schService != NULL)
        {
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            return 1;
        }
        else
        {
            CloseServiceHandle(schSCManager);
            return 0;
        }
    }
    else
        return 0;    
}

bool Delete_Service(wchar_t * ServiceName)
{
    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
    if(NULL != schSCManager)
    {
        // L"WmdmPmSN"
        SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
        if( NULL != schService)
        {
            SERVICE_STATUS ServiceStatus;
            QueryServiceStatus(schService,&ServiceStatus);
            if(ServiceStatus.dwCurrentState != SERVICE_STOPPED)
            {
                ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus);    
            }
            DeleteService(schService);
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            return 1;
        }
        else
            wprintf(L"Open Service failed!/n");
            return 0;
    }    
    else
    {
        wprintf(L"OpenSCManager failed!/n");
        CloseServiceHandle(schSCManager);
        return 0;
    }
}



void ReconfigureService(wchar_t * ServiceName, wchar_t * ServiceDisp)
{
  SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  if (schSCManager != NULL)
  {
    // Need to acquire database lock before reconfiguring.
    SC_LOCK sclLock = LockServiceDatabase(schSCManager);
    if (sclLock != NULL)
    {
      // Open a handle to the service.
      SC_HANDLE schService = OpenService(
          schSCManager,           // SCManager database
          ServiceName,            // name of service
          SERVICE_CHANGE_CONFIG); // need CHANGE access
   
      if (schService != NULL)
      {
          SERVICE_DESCRIPTION sdBuf;
          sdBuf.lpDescription = ServiceDisp;
          if (ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sdBuf))
          {
             MessageBox(NULL,L"Change SUCCESS",L" ",MB_OK);
          }
          CloseServiceHandle(schService);
      }
      UnlockServiceDatabase(sclLock);
    }   
    CloseServiceHandle(schSCManager);
  }
}


void  ChangeServiceRun(wchar_t * ServiceName)
{
    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
    if(NULL != schSCManager)
    {
        // L"WmdmPmSN"
        SC_HANDLE schService = OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
        if( NULL != schService)
        {
            if(ChangeServiceConfig(
                schService,
                SERVICE_NO_CHANGE,
                SERVICE_AUTO_START,
                SERVICE_NO_CHANGE,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL))
            {
                wprintf(L"Change Service done!/n");
                return;
            }
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
        }
        wprintf(L"Open Service failed!/n");
    }    
    wprintf(L"OpenSCManager failed!/n");
    CloseServiceHandle(schSCManager);
}

void EnumService(void)
{
    LPENUM_SERVICE_STATUS st;
    st=(LPENUM_SERVICE_STATUS)LocalAlloc(LPTR,64*1024);
    DWord ret=0;
    DWORD size=0;
    SC_HANDLE sc=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

    EnumServicesStatus(sc,SERVICE_WIN32,SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)st,1024*64,&size,&ret,NULL);


    for(int i=0;i<ret;i++){
        wprintf(L"%-20s%-50s",st[i].lpServiceName,st[i].lpDisplayName);
        switch(st[i].ServiceStatus.dwCurrentState){
    case(SERVICE_RUNNING):
        wprintf(L"running/n");
        break;
    case(SERVICE_STOPPED):
        wprintf(L"stopped/n");
        break;

        }
    }
}



本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/armor51/archive/2009/12/17/5027209.aspx
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 淅川县| 临湘市| 五华县| 平安县| 南岸区| 沾化县| 南川市| 东海县| 田林县| 中山市| 甘孜县| 喜德县| 张家界市| 万源市| 拜城县| 芦山县| 汕尾市| 云梦县| 苏州市| 元氏县| 固始县| 麟游县| 临猗县| 三河市| 望江县| 湖南省| 平遥县| 吉木乃县| 葫芦岛市| 涿鹿县| 高尔夫| 师宗县| 淮北市| 涿州市| 垦利县| 白玉县| 兴安县| 长垣县| 平原县| 乐清市|