I have a problem with a service, when I try to install it like this :
sc create MemoryStatus binpath= "C:\Users\rock\Documents\Visual Studio 2010\Projects\ServiceInC\Release\ServiceInC.exe"
I get a message in cmd that service is installed successfully, but when I take a look on it via Task Manager it is always in stopped state. The one more thing is that service is not even started, cause I dont see any log messages.
It seems like there is some problem with permissions or something like that, can you please help me with that?
MY main Service function is :
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(
"MemoryStatus",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
WriteToLog("Registering Control Handler failed");
return;
}
// Initialize Service
error = InitService();
if (error)
{
// Initialization failed
WriteToLog("Initialization failed");
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
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;
}
One more thing that I forgot to say, when I force run service frfom task manager it seems to work.
The command
sc create...only creates the service, it does not attempt to start it. Fromsc /?:To attempt to start the service either do it via the Services Control Applet or by running: