在C++ Builder中的服务应用程序6
我正在尝试使用C++ Builder 6做一个简单的服务,在那里我记录下所有服务的所有事件:OnStart,OnStop,OnCreate ...但是OnExecute,OnStart和OnStop事件从不记录。我错过了什么?在C++ Builder中的服务应用程序6
OnExecute,的OnStart和调用OnStop事件:
void __fastcall TServiceDump::ServiceExecute(TService *Sender) {
doSaveLog("ServiceExecute");
while(!Terminated)
{
ServiceThread->ProcessRequests(true);
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceDump::ServiceStart(TService *Sender, bool &Started)
{
doSaveLog("ServiceStart");
}
//---------------------------------------------------------------------------
void __fastcall TServiceDump::ServiceStop(TService *Sender, bool &Stopped)
{
doSaveLog("ServiceStop");
}
服务的更多的事件:
void __fastcall TServiceDump::ServiceAfterInstall(TService *Sender) {
doSaveLog("ServiceAfterInstall");
}
//---------------------------------------------------------------------------
void __fastcall TServiceDump::ServiceAfterUninstall(TService *Sender)
{
doSaveLog("ServiceAfterUninstall");
}
//---------------------------------------------------------------------------
void __fastcall TServiceDump::ServiceBeforeInstall(TService *Sender)
{
doSaveLog("ServiceBeforeInstall");
}
//---------------------------------------------------------------------------
void __fastcall TServiceDump::ServiceBeforeUninstall(TService *Sender)
{
doSaveLog("ServiceBeforeUninstall");
}
登录功能(伟大的工作):
void __fastcall TServiceDump::doSaveLog(String msg) {
TStrings* list = new TStringList;
try
{
if(FileExists("txt.log"))
{
list->LoadFromFile("txt.log");
}
list->Add(TimeToStr(Now()) + " : " + msg);
}
catch(Exception& e)
{
list->Add(TimeToStr(Now()) + ": ERROR " + e.Message);
}
list->SaveToFile("txt.log");
list->Free();
}
我可以安装和正确卸载服务,启动,停止,暂停和重新启动。
我的日志文件后安装:
17:14:16 : ServiceCreate 17:14:16 : ServiceBeforeInstall
17:14:16 : ServiceAfterInstall
17:14:17 : ServiceDestroy
我已经开始和停了几次,没有保存的日志。卸载后
我的日志文件:
17:15:44 : ServiceCreate 17:15:44 : ServiceBeforeUninstall
17:15:44 : ServiceAfterUninstall
17:15:45 : ServiceDestroy
回答:
我发现了错误。在安装/卸载服务时,如果我喜欢使用“txt.log”,则日志将保留在项目文件夹中。当服务执行时,它转到System32或SysWOW64(这是我的情况)。
我解决它移动日志目录为 “C:\ txt.log”:
void __fastcall TServiceDump::doSaveLog(String msg) {
TStrings* list = new TStringList;
try
{
if(FileExists("C:\\txt.log"))
{
list->LoadFromFile("C:\\txt.log");
}
list->Add(TimeToStr(Now()) + " : " + msg);
}
catch(Exception& e)
{
list->Add(TimeToStr(Now()) + ": ERROR " + e.Message);
}
list->SaveToFile("C:\\txt.log");
list->Free();
}
以上是 在C++ Builder中的服务应用程序6 的全部内容, 来源链接: utcz.com/qa/267119.html