You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C++Builder TService创建的Windows服务启动失败(错误1053)

问题:C++Builder TService创建的Windows服务无法启动

使用C++Builder的TService类创建Windows服务后,启动出现异常:

  • 命令行执行sc start MyService无报错,但服务始终卡在starting状态,无法切换至running
  • 服务启动过程中,ServiceStart()函数记录错误:

The service process could not connect to the service controller

  • 在系统「服务」窗口启动时,弹出错误:

Windows could not start the Folder Backup Service on Local Computer.
Error 1053: The service did not respond to the start or control signal in a timely manner.

已尝试将启停逻辑改为线程实现,甚至注释掉OnStart和OnStop事件中的所有代码,问题仍然存在。


Unit1.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)
{
    BackupThread = new TFolderBackupThread(true); 
    BackupThread->Start(); 
    Started = true;
}
//----------------------------------------------------------------------------

void __fastcall TService1::ServiceStop(TService *Sender, bool &Stopped)
{
    if (BackupThread) {
        BackupThread->Terminate(); 
        BackupThread->WaitFor();   
        delete BackupThread;
        BackupThread = nullptr;
    }
    Stopped = true;
}
//--------------------------------------------------------------------------

void __fastcall TService1::ServiceShutdown(TService *Sender)
{
    ServiceStop(Sender, *(new bool())); 
}
//--------------------------------------------------------------------------

Project1.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;
}

内容的提问来源于stack exchange,提问作者Jacek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 21:35:09