[英]How discover if a DLL runs in the context of a windows service?
I am writing a DLL which may run in the context of a service and may be loaded into a standard win32 process. 我正在编写一个可以在服务上下文中运行的DLL,并且可以将其加载到标准的win32进程中。 How can I detect whether it is running in the context of a service or in the context of a standard win32 process ?
如何检测它是在服务环境中运行还是在标准Win32进程环境中运行?
According to Session 0 Isolation , introduced in Vista, Windows Services run in session 0. To determine if the process is executing in session 0 you could use ProcessIdToSessionId function: 根据Vista中引入的会话0隔离 ,Windows服务在会话0中运行。要确定进程是否在会话0中执行,可以使用ProcessIdToSessionId函数:
DWORD session_id;
if (ProcessIdToSessionId(GetCurrentProcessId(), &session_id))
{
std::cout << "session_id=" << session_id << "\n";
}
else
{
std::cout << "Failed : " << GetLastError() << "\n";
}
In relation to your comment regarding GUI, to quote directly from the linked document: 关于您对GUI的评论,直接引用链接文档中的内容:
Because Session 0 is no longer a user session, services that are running in Session 0 do not have access to the video driver.
由于会话0不再是用户会话,因此在会话0中运行的服务无法访问视频驱动程序。 This means that any attempt that a service makes to render graphics fails.
这意味着服务进行渲染图形的任何尝试都会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.