# include & lt; Stdlib. H>
# include & lt; Stdio. H>
# define SLEEP_TIME 5000
# define FILE_PATH "directory D: \ \ users \ \ my documents \ \ Visual Studio 2013 \ \ Projects \ \ ServiceTest \ \ ServiceTest \ \ log. TXT"
SERVICE_STATUS SERVICE_STATUS;
SERVICE_STATUS_HANDLE hService_Status;
BOOL bStatus=FALSE;
DWORD ErrorCode.
The MEMORYSTATUS * AvaliableMem={0};
Int nSize;
Char RealMem [1024]={0};
Void WINAPI ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv);
Void WINAPI ServiceCtrlHandler (DWORD flag);
Int WriteToLog (char * log, dwords ErrorCode)
{
The FILE * hFile;
HFile=fopen (FILE_PATH, "a +");
If (hFile==NULL)
{
return -1;
}
The else
{
Fprintf (hFile, "% s \ n the Error Code: % d", the log, ErrorCode);
The fclose (hFile);
}
return 0;
}
Void main (int arg c, char * argv [])
{
SERVICE_TABLE_ENTRY SERVICE_TABLE_ENTRY [2].
Service_Table_Entry [0]. LpServiceName="Demo";
Service_Table_Entry [0]. LpServiceProc=(LPSERVICE_MAIN_FUNCTIONA ServiceMain);
Service_Table_Entry [1]. The lpServiceName=NULL;
Service_Table_Entry [1]. LpServiceProc=NULL;
The StartServiceCtrlDispatcher (Service_Table_Entry);
//if the StartServiceCtrlDispatcher (Service_Table_Entry)==FALSE)
//{
//ErrorCode=GetLastError ();
//WriteToLog (" StartServiceCtrlDispatcher failed! \ n ", ErrorCode);
//}
}
Void WINAPI ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv)
{
Service_Status. DwServiceType=SERVICE_WIN32;
Service_Status. DwWaitHint=0;
Service_Status. DwWin32ExitCode=0;
Service_Status. DwControlsAccepted=SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
Service_Status. DwCurrentState=SERVICE_START_PENDING;
Service_Status. DwCheckPoint=0;
Service_Status. DwServiceSpecificExitCode=0;
HService_Status=RegisterServiceCtrlHandler (" Demo ", ServiceCtrlHandler);
If (hService_Status==NULL)
{
ErrorCode=GetLastError ();
WriteToLog (" RegisterServiceCtrlHandler failed! \ n ", 0);
}
The else
{
Service_Status. DwCurrentState=SERVICE_RUNNING;
}
SetServiceStatus (hService_Status, & amp; Service_Status);
BStatus=TRUE;
While (bStatus)
{
GlobalMemoryStatus (AvaliableMem);
NSize=AvaliableMem - & gt; DwAvailPhys/1024/1024;
Sprintf_s (RealMem, 1024, "Avaliable Is % d Mb Memory", nSize);
WriteToLog (RealMem, 0);
Sleep (SLEEP_TIME);
}
}
Void WINAPI ServiceCtrlHandler (DWORD flag)
{
The switch (flag)
{
Case SERVICE_ACCEPT_SHUTDOWN:
BStatus=FALSE;
Service_Status. DwCurrentState=SERVICE_ACCEPT_SHUTDOWN;
break;
Case SERVICE_ACCEPT_STOP:
BStatus=FALSE;
Service_Status. DwCurrentState=SERVICE_ACCEPT_STOP;
break;
Default:
break;
}
SetServiceStatus (hService_Status, & amp; Service_Status);
}
Why the start for the first time can start and then refresh: then after the restart display error 1067 process terminated unexpectedly hope teacher can help you see answer thank you ~
CodePudding user response:
The Complete Service SampleCodePudding user response:
After you start did not enter the Run cycleCodePudding user response:
FILE_PATH "directory D: \ \ users \ \ my documents \ \ Visual Studio 2013 \ \ Projects \ \ ServiceTest \ \ ServiceTest \ \ log. TXT"Look at the Windows system log, have write access to this file