尝试在Windows服务中启动服务时出现错误1053

问题描述 投票:0回答:1

我无法启动使用

TService
类在 C++Builder 中创建的服务。

我编写了一个不执行任何操作的服务,我想启动它,但该服务没有启动。我注册该服务并在命令行中启动它。命令

sc start MyService
执行没有错误,但服务状态为
starting
,没有变为正在运行。服务启动过程中,
ServiceStart()
函数中的指令记录了错误:

服务进程无法连接到服务控制器

在“服务”窗口中启动服务时,出现错误:

Windows 无法启动本地计算机上的文件夹备份服务。 错误1053:服务没有响应启动或控制 及时发出信号。

这是我的服务代码:

单元1.cpp:

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TService1 *Service1;
//---------------------------------------------------------------------------

__fastcall TService1::TService1(TComponent* Owner): TService(Owner)
{
}
//--------------------------------------------------------------------------

void __stdcall ServiceController(unsigned CtrlCode)
{
    Service1->Controller(CtrlCode);
}
//-------------------------------------------------------------------------

TServiceController __fastcall TService1::GetServiceController(void)
{
    return (TServiceController) ServiceController;
}
//---------------------------------------------------------------------------

// Start serwisu
void __fastcall TService1::ServiceStart(TService *Sender, bool &Started)
{
    Started = true; // Serwis uruchomiony
    LogMessage("Service started successfully", EVENTLOG_ERROR_TYPE);
    ReportStatus(); // Informuj system, że usługa jest gotowa
}
//----------------------------------------------------------------------------

// Główna funkcja serwisu
void __fastcall TService1::ServiceExecute(TService *Sender)
{
}

项目1.cpp:

#include <System.SysUtils.hpp>
#include <Vcl.SvcMgr.hpp>
#pragma hdrstop
#include <tchar.h>
USEFORM("Unit1.cpp", Service1); /* TService: File Type */
//---------------------------------------------------------------------------
#define Application Svcmgr::Application
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    try
    {
        // Windows 2003 Server requires StartServiceCtrlDispatcher to be
        // called before CoRegisterClassObject, which can be called indirectly
        // by Application.Initialize. TServiceApplication->DelayInitialize allows
        // Application->Initialize to be called from TService->Main (after
        // StartServiceCtrlDispatcher has been called).
        //
        // Delayed initialization of the Application object may affect
        // events which then occur prior to initialization, such as
        // TService->OnCreate. It is only recommended if the ServiceApplication
        // registers a class object with OLE and is intended for use with
        // Windows 2003 Server.
        //
         Application->DelayInitialize = true;
        //
        if ((!Application->DelayInitialize) || (Application->Installing()))
        {
            Application->Initialize();
        }
        Application->CreateForm(__classid(TService1), &Service1);
        Application->Run();
    }
    catch (Exception &exception)
    {
        Sysutils::ShowException(&exception, System::ExceptAddr());
    }
    catch(...)
    {
        try
        {
            throw Exception("");
        }
        catch(Exception &exception)
        {
            Sysutils::ShowException(&exception, System::ExceptAddr());
        }
        }
    return 0;
}
c++builder
1个回答
0
投票

问题是您的

OnExecute
事件处理程序为空。 它根本不服务 SCM 请求。 如果您使用
OnExecute
事件,则必须定期调用
ServiceThread->ProcessRequests(false)
,例如:

void __fastcall TService1::ServiceExecute(TService *Sender)
{
    while (!ServiceThread->Terminated)
    {
        ServiceThread->ProcessRequests(false);
        Sleep(10);
    }
}

否则,只需完全删除

OnExecute
处理程序即可。 当未分配
OnExecute
事件处理程序时,
TService
将自动处理 SCM 请求。

© www.soinside.com 2019 - 2024. All rights reserved.