我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C++服务程序代码怎么写

这篇“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("[|] [|]/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
#include

#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++服务程序代码怎么写”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


网站名称:C++服务程序代码怎么写
当前链接:http://mswzjz.cn/article/pojchc.html

其他资讯