In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to write C++ service program code" is not quite understood by most people, so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to write C++ service program code" article.
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, "% Spura", 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)
}
The above is about the content of this article on "how to write C++ service program code". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.