A current project involves writing a Windows Service. Due to my lack of experience in C++, I've read an article on creating very basic services.
I've understood everything that was said; however when I try to compile the code, the compiler shows several errors.
Here is the code that I am using...
#include <windows.h>
#include <stdio.h>
#define SLEEP_TIME 5000;
#define LOGFILE "C:\\log.txt";
int WriteToLog(char* str){
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL){
return -1;
}
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
void ServiceMain(int charc, char** argv);
void ControlHandler(DWORD request);
int InitService();
void main(){
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceN
ame = "MemoryStatusTestService";
ServiceTable[0].lpServiceP
roc = (LPSERVICE_MAIN_FUNCTION) ServiceMain;
ServiceTable[1].lpServiceN
ame = NULL;
ServiceTable[1].lpServiceP
roc = NULL;
// Start the control dispatcher thread for the service
StartServiceCtrlDispatcher
(ServiceTa
ble);
}
int InitService(){
return WriteToLog("Monitoring Started");
}
void ServiceMain(int argc, char** argv){
int error;
ServiceStatus.dwServiceTyp
e = SERVICE_WIN32;
ServiceStatus.dwCurrentSta
te = SERVICE_START_PENDING;
ServiceStatus.dwControlsAc
cepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitC
ode = 0;
ServiceStatus.dwServiceSpe
cificExitC
ode = 0;
ServiceStatus.dwCheckPoint
= 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler
("MemorySt
atusServic
eTest", (LPHANDLER_FUNCTION) ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE) 0){
// Registering Control Handler failed.
return;
}
// Initialize Service
error = InitService();
if (error){
// Initizlization failed
ServiceStatus.dwCurrentSta
te = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitC
ode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// Report the status the the SCM.
ServiceStatus.dwCurrentSta
te = SERVICE_RUNNING;
SetServiceStatus(hStatus, &ServiceStatus);
MEMORYSTATUS memory;
// The worker loop of the service
while (ServiceStatus.dwCurrentSt
ate == SERVICE_RUNNING) {
char buffer[16];
GlobalMemoryStatus(&memory
);
sprintf(buffer, "%d", memory.dwAvailPhys);
int result = WriteToLog(buffer);
if (result){
ServiceStatus.dwCurrentSta
te = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitC
ode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
Sleep(SLEEP_TIME);
}
return;
}
void ControlHandler(DWORD request){
switch (request){
case SERVICE_CONTROL_STOP:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitC
ode = 0;
ServiceStatus.dwCurrentSta
te = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitC
ode = 0;
ServiceStatus.dwCurrentSta
te = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
And here are the compile errors I'm receiving...
Compiling...
Service.cpp
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(11) : error C2143: syntax error : missing ')' before ';'
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(11) : error C2660: 'fopen' : function does not take 1 parameters
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(11) : error C2143: syntax error : missing ';' before ','
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(11) : error C2059: syntax error : ')'
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(107)
: error C2143: syntax error : missing ')' before ';'
C:\Documents and Settings\Adam\Desktop\OwnK
eyLog\Serv
ice\Servic
e.cpp(107)
: error C2059: syntax error : ')'
Error executing cl.exe.
I can't see any syntax errors and this is driving me nuts so I have come for help.
I have very little C++ programming experience so please go easy on me.
Any help or suggestions will be greatly appreciated.
Cheers
-OBCT