简体   繁体   English

如何发现DLL是否在Windows服务的上下文中运行?

[英]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.

相关问题 如何知道哪个用户帐户运行特定的Windows服务? - How to know which user account runs a specific windows service? 如何运行dll作为服务? - How to run a dll as a service? 如何在本地Windows网络上发现我的应用程序的所有其他实例? - How to discover all other instances of my application on the local Windows network? 通过串口使用Windows服务的LG TV Control在PC启动后大约30秒运行 - 如何更快地启动服务? - LG TV Control through serial using Windows service runs about 30 seconds after PC is powered on - how to start the service sooner? Windows 7中编译的ADO应用程序在Windows 7中运行,而Windows XP中没有运行msado15.dll - ADO application compiled in Windows 7 runs in Windows 7 does not run in Windows XP, msado15.dll 在Windows 7中从Windows服务运行DLL / EXE程序 - Running DLL/EXE program from windows service in Windows 7 如何在Windows上将LibXtract构建为DLL - How to build LibXtract as DLL on Windows 如何实际共享Windows DLL? - How are windows DLL actually shared? 在Windows上进行_CONTEXT结构调试:如何? - _CONTEXT structure debugging on Windows: How to? 创建与Windows Service通信的非托管DLL(在C ++中)? - Creating an unmanaged DLL (in C++) which communicates with Windows Service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM