Solved

service does not run

Posted on 2004-09-02
16
355 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
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!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 147
Need some help with mailto 16 44
print bytes of an integer 6 48
maximize the sum of fractions 33 76
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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