#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
#define SLEEP_TIME 5000
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();
int WriteToLog(char* str)
{
FILE* log;
log = fopen("output.html", "w");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}
ofstream out;
class op{
public:
op(){
out.open("output.html");
}
};
op _;
int main()
{
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = "MemoryStatus3";
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
// Start the control dispatcher thread for our service
out<<"Starting"<<endl;
out<<"Ret Code"<<StartServiceCtrlDispatcher(ServiceTable)<<endl;
DWORD myerror = GetLastError();
out<<"Last Error"<<myerror<<endl;
if(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT == myerror){
out<<"ERROR_FAILED_SERVICE_CONTROLLER_CONNECT"<<ERROR_FAILED_SERVICE_CONTROLLER_CONNECT<<endl;
}
if(ERROR_INVALID_DATA == myerror){
out<<"ERROR_INVALID_DATA"<<ERROR_INVALID_DATA<<endl;
}
if(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT == myerror){
out<<"ERROR_SERVICE_ALREADY_RUNNING"<<ERROR_FAILED_SERVICE_CONTROLLER_CONNECT<<endl;
}
out<<"Done"<<endl;
out.close();
system("PAUSE");
}
void ServiceMain(int argc, char** argv)
{
out<<"Service Main"<<endl;
int error;
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler(
"MemoryStatus3",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
cout<<"Registering Control Handler failed"<<endl;
return;
}
// Initialize Service
error = InitService();
if (error)
{
// Initialization failed
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
cout<<"Init Failed"<<endl;
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);
int result = WriteToLog(buffer);
if (result)
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
Sleep(SLEEP_TIME);
}
return;
}
// Service initialization
int InitService()
{
out<<"Init Service"<<endl;
int result;
result = WriteToLog("Monitoring started.");
return(result);
}
// Control handler function
void ControlHandler(DWORD request)
{
out<<"ControlHandler"<<endl;
switch(request)
{
case SERVICE_CONTROL_STOP:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
My Service Doesn't Work
I have been trying for two days to get this to run but it will not start.
The start service dispatch control thread dies. Here is the output log:
Starting
Ret Code0
Last Error1063
ERROR_FAILED_SERVICE_CONTROLLER_CONNECT1063
ERROR_SERVICE_ALREADY_RUNNING1063
Done
Anyone have any advice? I just don't know what else to narrow down the problem :(
Looking at my service code, your ServiceMain function may not be declared correctly.
Mine is declared as
void WINAPI ServiceStart(DWORD argc, LPTSTR * argv)
aslo for the control handler
void WINAPI ServiceCtrlHandler(DWORD opcode)
Mine is declared as
void WINAPI ServiceStart(DWORD argc, LPTSTR * argv)
aslo for the control handler
void WINAPI ServiceCtrlHandler(DWORD opcode)
Ah right, I forgot an important question.
How are you running this serivce application?
You have to use the service control panel to start the service. You can't start the application like a normal executable by double clicking on the icon.
In order for your service to show up in the service control panel you have to add a registry entry for it.
I believe that there is also a command line for starting services although I dont know what it is off the top of my head.
How are you running this serivce application?
You have to use the service control panel to start the service. You can't start the application like a normal executable by double clicking on the icon.
In order for your service to show up in the service control panel you have to add a registry entry for it.
I believe that there is also a command line for starting services although I dont know what it is off the top of my head.
Quote:Original post by cmptrgearnet start your_service_name
I believe that there is also a command line for starting services although I dont know what it is off the top of my head.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement