【发布时间】:2018-11-24 20:23:18
【问题描述】:
我有一个关于 windows 服务的非常基本的问题,我有这个 main 安装的函数,我可以用它来安装我的服务,还有一些配置数据在 main 函数中加载:
int
wmain(int argc, WCHAR* argv[])
{
// it reads config and fill a global struct.
ReadConfig();
// if command == 'install'
install_service();
}
这里是服务的主要功能:
void WINAPI ServiceMain(DWORD argc, WCHAR* argv[])
{
// this method retrieves the global config object.
auto config_data = GetConfigData();
// service stuff
}
这是另一个函数,如果它在没有任何参数的情况下运行(argc = 0 !),则会在我的 wmain 函数中调用:
bool
ServiceRunAsService()
{
static const SERVICE_TABLE_ENTRY table[] = {
{ SERVICE_NAME, ServiceMain },
{ NULL, NULL }
};
g_hStopService = CreateEvent(0, TRUE, FALSE, 0);
return StartServiceCtrlDispatcher(table) && GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT;
}
我的问题是,当 Windows 想要运行我的服务时(在 PC 关闭并再次打开后),它是调用我的 wmain 函数(因此调用 ReadConfig 函数)还是调用注册的ServiceMain 函数?
我想指出install_service 方法,通过GetModuleFileName 找到可执行文件的路径并将其传递给CreateService 和ScmManager
【问题讨论】:
-
为什么不直接检查?输出一条日志消息。
-
老实说,我发现在服务中记录消息很困难!我不知道如何
-
当然windows调用exe入口点。所以
wmain。系统对ServiceMain一无所知,直到您不致电StartServiceCtrlDispatcher
标签: c++ windows winapi windows-services