Solved

service does not run

Posted on 2004-09-02
16
346 Views
Last Modified: 2010-04-01
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



0
Comment
Question by:leork2004
  • 5
  • 4
  • 3
  • +1
16 Comments
 

Expert Comment

by:Tonydixon
ID: 11968863
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);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 11969506
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?
0
 

Author Comment

by:leork2004
ID: 11971091
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


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11971246
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 11972733
Also, check out http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b170738 ("Debugging a Windows NT Service")
0
 

Author Comment

by:leork2004
ID: 11979610
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11980647
>>>>> 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

0
 
LVL 86

Expert Comment

by:jkr
ID: 11980781
>>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...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11982256
>>>> 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.

0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 11982741
>>LocalSystem accounts have no access to network resources.

Yup but I haven't read here that this was the case...
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 11983557
>>>> its server service and it has to listen to clients on specific ports.

0
 

Author Comment

by:leork2004
ID: 11989559
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
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11989934
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now