Solved

service does not run

Posted on 2004-09-02
16
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

630 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