Link to home
Start Free TrialLog in
Avatar of leork2004
leork2004

asked on

service does not run

Hi

I have app developed in c++ and  programed it run as service on system startup but it does not work as desired on some machines that is the service does not start even though the windows services applet show it has started .its really strange but true


help please its very critical



Avatar of Tonydixon
Tonydixon

Can you send me your service code to look over.
The function that responed to the service control manager.

It sound like you have most of the code in place, but are not strating your service thread.

It should look some thing like this

#include <windows.h>

SERVICE_STATUS          MyServiceStatus;
SERVICE_STATUS_HANDLE   MyServiceStatusHandle;

VOID SvcDebugOut(LPSTR String, DWORD Status);

void WINAPI MyServiceStart (DWORD argc, LPTSTR *argv)
{
    DWORD status;
    DWORD specificError;
 
    MyServiceStatus.dwServiceType        = SERVICE_WIN32;
    MyServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
    MyServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP |
        SERVICE_ACCEPT_PAUSE_CONTINUE;
    MyServiceStatus.dwWin32ExitCode      = 0;
    MyServiceStatus.dwServiceSpecificExitCode = 0;
    MyServiceStatus.dwCheckPoint         = 0;
    MyServiceStatus.dwWaitHint           = 0;
 
    MyServiceStatusHandle = RegisterServiceCtrlHandler(
        "MyService",
        MyServiceCtrlHandler);
 
    if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
    {
        SvcDebugOut(" [MY_SERVICE] RegisterServiceCtrlHandler
            failed %d\n", GetLastError());
        return;
    }
 
    // Initialization code goes here.
    status = MyServiceInitialization(argc,argv, &specificError);
 
    // Handle error condition
    if (status != NO_ERROR)
    {
        MyServiceStatus.dwCurrentState       = SERVICE_STOPPED;
        MyServiceStatus.dwCheckPoint         = 0;
        MyServiceStatus.dwWaitHint           = 0;
        MyServiceStatus.dwWin32ExitCode      = status;
        MyServiceStatus.dwServiceSpecificExitCode = specificError;
 
        SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus);
        return;
    }
 
    // Initialization complete - report running status.
    MyServiceStatus.dwCurrentState       = SERVICE_RUNNING;
    MyServiceStatus.dwCheckPoint         = 0;
    MyServiceStatus.dwWaitHint           = 0;
 
    if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
    {
        status = GetLastError();
        SvcDebugOut(" [MY_SERVICE] SetServiceStatus error
            %ld\n",status);
    }
 
    // This is where the service does its work.
    SvcDebugOut(" [MY_SERVICE] Returning the Main Thread \n",0);
 
    return;
}
 
// Stub initialization function.
DWORD MyServiceInitialization(DWORD   argc, LPTSTR  *argv,
    DWORD *specificError)
{
    argv;
    argc;
    specificError;
// set up env for MFC and start service thread here.
    return(0);
}
Avatar of jkr
Sorry, but 'does not work' is kinda poor description for that problem. What are the error codes? Do you have any means for your service to create diagnostic output?
Avatar of leork2004

ASKER

jkr

its server service and it has to listen to clients on specific ports.In the services applet
it shows that it is running (started automatic) but does not listen to the ports when i use netstat -a command there are no ports listed for the service in listening mode.
I have a log file for the same and it says the service has started at specfic time.

leork


You have to write a log file to see what your service does.

How did you install the service? If it runs with local account it has no access to network resources. You have to install it with a user account that has appropriate rights.

Do you have a thread or message loop to respond to service manager's status request's?

Regards, Alex
Also, check out http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b170738 ("Debugging a Windows NT Service")
jkr
  code I used for creating service is as given below.
check is it correct?

SC_HANDLE   hservice;
 SC_HANDLE   hsrvmanager;

// Open the default, local Service Control Manager database
  hsrvmanager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hsrvmanager == NULL)
{
        MessageBox(NULL,"The Service Control Manager could not be contacted - the RDCManagement service was not registered","Management", MB_ICONEXCLAMATION | MB_OK);
}

// Create an entry for the  service
hservice = CreateService(
      hsrvmanager,                  // SCManager database
      CServiceModuleNAME,            // name of service defined as Management
      CServiceModuleDISPLAYNAME,            // name to display defined as Management
      SERVICE_ALL_ACCESS,            // desired access
      SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
            // service type
      SERVICE_AUTO_START,                  // start type
      SERVICE_ERROR_NORMAL,            // error control type
      servicecmd,                  // service's binary
      NULL,                        // no load ordering group
      NULL,                        // no tag identifier
      DEPENDENCIES,                  // dependencies defined as "" 
      NULL,                        // LocalSystem account
      NULL);                        // no password
if (hservice == NULL)
{
                DWORD error = GetLastError();
      if (error == ERROR_SERVICE_EXISTS) {
            MessageBox(NULL,  "The Management service is already registered",
         "Management",  MB_ICONEXCLAMATION | MB_OK);
               } else {
          MessageBox(NULL,        "The Management service could not be               registered",  " Management",  MB_ICONEXCLAMATION | MB_OK);
      }
                CloseServiceHandle(hsrvmanager);
}
CloseServiceHandle(hsrvmanager);
CloseServiceHandle(hservice);

 
leork
>>>>> NULL,                    // LocalSystem account

As i told you, you need an account other than that.

Stop the service using Service Control Manager and start it again using a priviliged local or domain account.

Regards, Alex

>>As i told you, you need an account other than that.

No, that's alright if you want to run as LocalSystem. But, creating a service is not the same as starting it...
>>>> No, that's alright if you want to run as LocalSystem.

LocalSystem accounts have no access to network resources.

>>>> But, creating a service is not the same as starting it...

That's true, you can install it like you did and start it using an account that has network access.

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr,alex

  pls tell me how to start the service  thro' code using an account that has network access.
  how i get that the current logged in user has network access or not?


-leork
The best is to stop the current running service using Service Control Manager (SCM) via GUI applet. Then start it again (second tab) giving a domain\user pair at the first prompt that has network access.

you also can give  domain\user with CreateService (however you have to hold the plain password in your source).

 // Create an entry for the  service
hservice = CreateService(
     hsrvmanager,               // SCManager database
     CServiceModuleNAME,          // name of service defined as Management
     CServiceModuleDISPLAYNAME,          // name to display defined as Management
     SERVICE_ALL_ACCESS,          // desired access
     SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
          // service type
     SERVICE_AUTO_START,               // start type
     SERVICE_ERROR_NORMAL,          // error control type
     servicecmd,               // service's binary
     NULL,                    // no load ordering group
     NULL,                    // no tag identifier
     DEPENDENCIES,               // dependencies defined as "" 
     "domain\\user",                    // network account
     "password");                    // no password


If you don't have a domain login use   ".\\user" or "\\workstationid\\user" instead.

Regards, Alex