Jump to content

  • Log In with Google      Sign In   
  • Create Account


My Service Doesn't Work


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 gandolf1212   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 May 2006 - 04:47 AM

I have been trying for two days to get this to run but it will not start.
#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; 
} 



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 :(

Sponsor:

#2 cmptrgear   Members   -  Reputation: 306

Like
0Likes
Like

Posted 19 May 2006 - 05:08 AM

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)
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson

#3 gandolf1212   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 May 2006 - 03:33 PM

Nope, I changed those things but it does the same thing :(

#4 cmptrgear   Members   -  Reputation: 306

Like
0Likes
Like

Posted 21 May 2006 - 05:50 AM

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.
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson

#5 Evil Steve   Members   -  Reputation: 1959

Like
0Likes
Like

Posted 21 May 2006 - 06:34 AM

Quote:
Original post by cmptrgear
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.
net start your_service_name




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS