Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

StartServiceCtrlDispatcher error 1063

Posted on 2006-06-04
2
Medium Priority
?
28,037 Views
Last Modified: 2010-08-05
i created a service.
installed it
i get an error 1063 when trying to start the service using StartServiceCtrlDispatcher();

what have i done wrong?

see the my code below.

thanks
efratk


// MyServiceTest.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdtctl.h>            // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>                  // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <iostream>

#include "Windows.h"
#include "Winsvc.h"
#include "time.h"

#include "MyServiceTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;


SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);

BOOL InstallService();
BOOL DeleteService();



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
      int nRetCode = 0;
      
      // initialize MFC and print and error on failure
      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      {
            //cerr << _T("Fatal Error: MFC initialization failed") << endl;
            printf("Fatal Error: MFC initialization failed!\n");
                  return 1;
      }
      
      if(argc>1)
      {
            if(strcmp(argv[1],"-i")==0)
            {
                  if(InstallService())
                        printf("\n\nService Installed Sucessfully\n");
                  else
                        printf("\n\nError Installing Service\n");
            }
            else if(strcmp(argv[1],"-r")==0)
            {
                  if(DeleteService())
                        printf("\n\nService UnInstalled Sucessfully\n");
                  else
                        printf("\n\nError UnInstalling Service\n");
            }
            else if(strcmp(argv[1],"-s")==0)
            {
                  SERVICE_TABLE_ENTRY DispatchTable[]=
                  {{"MyServiceTestService",ServiceMain},{NULL,NULL}};

                  BOOL ret;
                  ret=StartServiceCtrlDispatcher(DispatchTable);
                  if (!ret)
                  {
                        char wcError[100];
                        sprintf(wcError, "StartServiceCtrlDispatcher Failed. Error Code %d\n",GetLastError());
                        TRACE(wcError);
                        
                        //here i get error 1063

                  }
            }
            else
            {
                  printf("\n\nUnknown Usage\n\n"
                        "For Install use MyServiceTest -i\n\n"
                        "For UnInstall use MyServiceTest -d\n");
            }
      }
      else
      {
            printf("\n\nUnknown Usage\n\n"
                  "For Install use MyServiceTest -i\n\n"
                  "For UnInstall use MyServiceTest -d\n");
      }
      return 0;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
      DWORD status;
      DWORD specificError;
      m_ServiceStatus.dwServiceType = SERVICE_WIN32;
      m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
      m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
      m_ServiceStatus.dwWin32ExitCode = 0;
      m_ServiceStatus.dwServiceSpecificExitCode = 0;
      m_ServiceStatus.dwCheckPoint = 0;
      m_ServiceStatus.dwWaitHint = 0;
      
      m_ServiceStatusHandle = RegisterServiceCtrlHandler("MyServiceTestService",
            ServiceCtrlHandler);
      if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
      {
            return;
      }
      m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
      m_ServiceStatus.dwCheckPoint = 0;
      m_ServiceStatus.dwWaitHint = 0;
      if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
      {
      }
      
      bRunning=true;
      while(bRunning)
      {
            AfxMessageBox("Wait 3 seconds...");
            Sleep(3000);
            
      }
      return;
}

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
      switch(Opcode)
      {
      case SERVICE_CONTROL_PAUSE:
            m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
            break;
      case SERVICE_CONTROL_CONTINUE:
            m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
            break;
      case SERVICE_CONTROL_STOP:
            m_ServiceStatus.dwWin32ExitCode = 0;
            m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
            m_ServiceStatus.dwCheckPoint = 0;
            m_ServiceStatus.dwWaitHint = 0;
            
            SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
            bRunning=false;
            break;
      case SERVICE_CONTROL_INTERROGATE:
            break;
      }
      return;
}

BOOL InstallService()
{
      char strDir[1024];
      SC_HANDLE schSCManager,schService;
      GetCurrentDirectory(1024,strDir);
      strcat(strDir,"\\Debug\\MyServiceTest.exe");
      schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
      
      if (schSCManager == NULL)
            return false;
      LPCTSTR lpszBinaryPathName=strDir;
      
      schService = CreateService(schSCManager,"MyServiceTestService",
            "SAM Job Importer", // service name to display
            SERVICE_ALL_ACCESS, // desired access
            SERVICE_WIN32_OWN_PROCESS, // service type
            SERVICE_DEMAND_START, // start type
            SERVICE_ERROR_NORMAL, // error control type
            lpszBinaryPathName, // service's binary
            NULL, // no load ordering group
            NULL, // no tag identifier
            NULL, // no dependencies
            NULL, // LocalSystem account
            NULL); // no password
      
      if (schService == NULL)
            return false;
      
      CloseServiceHandle(schService);
      return true;
}

BOOL DeleteService()
{
      SC_HANDLE schSCManager;
      SC_HANDLE hService;
      schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
      
      if (schSCManager == NULL)
            return false;
      hService=OpenService(schSCManager,"MyServiceTestService",SERVICE_ALL_ACCESS);
      if (hService == NULL)
            return false;
      if(DeleteService(hService)==0)
            return false;
      if(CloseServiceHandle(hService)==0)
            return false;
      
      return true;
}
0
Comment
Question by:efratk
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16827091
I don't see StartService function in your code. StartService starts service using Service Manager. According to information provided in your InstallService function, Service Manager must start your program (lpszBinaryPathName) without parameters. In this case program runs as service, and not as console application. When program runs as Console application, you get ERROR_FAILED_SERVICE_CONTROLLER_CONNECT error.

This works by the wollowing way:
1) Your program (or another program) installs the service.
2) Your program (or another program) starts service using the StartService function.
3) Your program runs without parameters (according to information provided in InstallService function). This program, started by Service Manager, runs as service, and in this case StartServiceCtrlDispatcher succeeded.

If you use your own program to start service, one instance of this program, executed with -s switch, starts service and exits. Another instance of your program, executed without parameters, starts as service.

You can write another program which installs, starts, stops and uninstalls service:
http://www.codeproject.com/system/serviceskeleton.asp
0
 

Author Comment

by:efratk
ID: 16832298
i accept your answer.

i start the service not through the console but througjh the cmd

i use the cmd:
 net start MyServiceTestService

and my service was started.

thanks.






0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 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.
Suggested Courses

580 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