亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++服務程序代碼怎么寫

發布時間:2022-04-06 16:54:26 來源:億速云 閱讀:331 作者:iii 欄目:編程語言

這篇“C++服務程序代碼怎么寫”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C++服務程序代碼怎么寫”文章吧。

VOID Install(char *path, char *internalName, char *descriptiveName);
VOID UnInstall(char *internalName);

int main(int argc,char *argv[])
{
  char *install="install";
  char *uninstall="uninstall";
  if(argc>1)
  {
 if (strcmp(uninstall,argv[1])==0)
  UnInstall("ywpturkey");

if (strcmp(install,argv[1])==0)
Install(argv[2], "ywpturkey", "ywpturkey");
  }
  else
  {
  printf("[<install>|<uninstall>] [<path>|<sevicename>]/n");
  }
  return 0;
}
VOID Install(char *path, char *internalName, char *descriptiveName)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
LPCTSTR lpszDisplayName;
LPCTSTR lpszBinaryPathName;

schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);

if (schSCManager == NULL)
printf("ERROR on OpenSCManager/n");

lpszDisplayName = TEXT(descriptiveName);
lpszBinaryPathName = path;

schService = CreateService(
  schSCManager,              /* SCManager database      */
  TEXT(internalName),        /* name of service         */
lpszDisplayName,          /* service name to display */
  SERVICE_ALL_ACCESS,        /* desired access          */
  SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, /* service type            */
  SERVICE_AUTO_START,      /* start type              */
  SERVICE_ERROR_NORMAL,      /* error control type      */
  lpszBinaryPathName,        /* service's binary        */
  NULL,                      /* no load ordering group  */
  NULL,                      /* no tag identifier       */
  NULL,                      /* no dependencies         */
  NULL,                      /* LocalSystem account     */
  NULL);                     /* no password             */

if (schService == NULL)
printf("ERROR on CreateService/n");
else
printf("Service Installed/n");

CloseServiceHandle(schService);

exit(1);
}

VOID UnInstall(char *internalName)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;

schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);

schService = OpenService( schSCManager, TEXT(internalName), DELETE);

if (schService == NULL)
printf("OpenService failed/n");

if (! DeleteService(schService) )
printf("DeleteService FAILED/n");
else
printf("Service Removed/n");

CloseServiceHandle(schService);

exit(1);
}
---------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>

#define SLEEP_TIME 5000
#define LOGFILE "C://ywpturkey.txt"

////////////////////////////////////////////////////////////
// Declare several global variables to share
// their values across multiple functions of your program.
////////////////////////////////////////////////////////////
SERVICE_STATUS          ServiceStatus;
SERVICE_STATUS_HANDLE   hStatus;

////////////////////////////////////////////////////////////
// Make the forward definitions of functions prototypes.
//
////////////////////////////////////////////////////////////
void  ServiceMain(int argc, char** argv);
void  ControlHandler(DWORD request);
int InitService();

int WriteToLog(char* str)
{
  FILE* log;
  log = fopen(LOGFILE, "a+");
  if (log == NULL){
  OutputDebugString("Log file open failed.");
 return -1;
  }
  fprintf(log, "%s/n", str);
  fclose(log);
  return 0;
}

// Service initialization
int InitService()
{
OutputDebugString("Monitoring started.");
int result;
result = WriteToLog("Monitoring started.");
return(result);
}

// Control Handler
void ControlHandler(DWORD request)
{
  switch(request)
  {
 case SERVICE_CONTROL_STOP:
OutputDebugString("Monitoring stopped.");
WriteToLog("Monitoring stopped.");

ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;

case SERVICE_CONTROL_SHUTDOWN:
OutputDebugString("Monitoring stopped.");
WriteToLog("Monitoring stopped.");

ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;

default:
break;
}

// Report current status
SetServiceStatus (hStatus, &ServiceStatus);

return;
}

void ServiceMain(int argc, char** argv)
{
  int error;

ServiceStatus.dwServiceType =
 SERVICE_WIN32;
  ServiceStatus.dwCurrentState =
 SERVICE_START_PENDING;
  ServiceStatus.dwControlsAccepted   =
 SERVICE_ACCEPT_STOP |
 SERVICE_ACCEPT_SHUTDOWN;
  ServiceStatus.dwWin32ExitCode = 0;
  ServiceStatus.dwServiceSpecificExitCode = 0;
  ServiceStatus.dwCheckPoint = 0;
  ServiceStatus.dwWaitHint = 0;

hStatus = RegisterServiceCtrlHandler(
 "turkey",
 (LPHANDLER_FUNCTION)ControlHandler);
  if (hStatus == (SERVICE_STATUS_HANDLE)0)
  {
 // Registering Control Handler failed
 WriteToLog("Handler failed.");
 return;
  }

// Initialize Service
  error = InitService();
  if (error)
  {
 // Initialization failed
 ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
 ServiceStatus.dwWin32ExitCode = -1;
 SetServiceStatus(hStatus, &ServiceStatus);
 WriteToLog("InitService error.");
 return;
  }
  // We report the running status to SCM.
  ServiceStatus.dwCurrentState =
 SERVICE_RUNNING;
  SetServiceStatus (hStatus, &ServiceStatus);

MEMORYSTATUS memory;
  // The worker loop of a service
  while (ServiceStatus.dwCurrentState ==
 SERVICE_RUNNING)
  {
 char buffer[16];
 GlobalMemoryStatus(&memory);
 sprintf(buffer, "%d", memory.dwAvailPhys);

OutputDebugString(buffer);
 int result = WriteToLog(buffer);
 if (result)
 {
ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode      = -1;
SetServiceStatus(hStatus,
 &ServiceStatus);
return;
 }
 Sleep(SLEEP_TIME);
  }
  return;
}
void main(int argc, char* argv[])
{
  SERVICE_TABLE_ENTRY ServiceTable[2];
  ServiceTable[0].lpServiceName = "turkey";
  ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;

// Start the control dispatcher thread for our service
  StartServiceCtrlDispatcher(ServiceTable);
}

以上就是關于“C++服務程序代碼怎么寫”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI

仙桃市| 荆州市| 行唐县| 嵊泗县| 平昌县| 新晃| 阿鲁科尔沁旗| 肇州县| 山阴县| 板桥市| 股票| 兴海县| 共和县| 兴安县| 威海市| 偃师市| 白城市| 太湖县| 伊吾县| 徐汇区| 金川县| 东辽县| 蓬安县| 冀州市| 丹阳市| 汉川市| 双江| 报价| 彰武县| 临沭县| 开化县| 丹东市| 阿瓦提县| 肃宁县| 大关县| 灵寿县| 策勒县| 平凉市| 甘孜县| 漳平市| 突泉县|