Solved

service does not run

Posted on 2004-09-02
16
351 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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