簡體   English   中英

創建Windows服務和批處理文件

[英]Creating Windows Service and batch file

我想創建Windows服務,它將在啟動時運行批處理文件。 我知道API類似於createservice ,但是我想要的是當我說從Service Control Manager Start service ,我想用參數start調用我的批處理文件,當我說停止時,我想用stop參數調用同一個批處理文件。

每當您執行任何Windows服務操作(例如啟動/停止/暫停服務)時,您通過RegisterServiceCtrlHandler()注冊的服務主控制器處理程序函數都會收到諸如SERVICE_CONTROL_PAUSE,SERVICE_CONTROL_CONTINUE,SERVICE_CONTROL_STOP等消息。

您可以創建單獨的函數以使用不同的輸入參數來調用bat文件,並且當接收到適當的服務消息時可以調用這些函數。

void ServiceMainCntrlHandler(未簽名的長請求){開關(請求){/ *接收到的服務暫停信號* / case SERVICE_CONTROL_PAUSE://將服務當前狀態更改為暫停ServiceStatus.dwCurrentState = SERVICE_PAUSED; SetServiceStatus(hStatus,&ServiceStatus);

  // TODO: Call appropriate function
  break;

/* Received service continue signal */
case SERVICE_CONTROL_CONTINUE:
  // Change the service current status to started
  ServiceStatus.dwCurrentState  = SERVICE_RUNNING;
  SetServiceStatus (hStatus, &ServiceStatus);

  // TODO: Call appropriate function
  break;

/* Received service stop signal */
case SERVICE_CONTROL_STOP:
  // Change the service current status to stopped
  ServiceStatus.dwWin32ExitCode = 0;
  ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
  ServiceStatus.dwCheckPoint        = 0;
  ServiceStatus.dwWaitHint      = 0;
  SetServiceStatus (hStatus, &ServiceStatus);

  // TODO: Call the function which will invoke the bat file with input parameter as "stop"
  break;         
default:  break;

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM