问题描述
我知道如何编写dll,如何编写服务以及如何使用 rundll32
运行dll,但是现在我想编写一个以Windows中的服务
I know how to write a dll and how to write a service and how to run a dll with rundll32
, but now I want to write a dll that install as a service in windows
我不知道这是否可行,或者应该导出dll中的哪个函数?
I don't know if that's possible or which function in dll should be exported?
如何安装和运行DLL作为服务?
How can I install and run a dll as a service?
推荐答案
有几种不同的方法可以将DLL作为服务运行。您可以:
There are a few different ways to run a DLL as a service. You can either:
-
编写自己的.exe服务,并根据需要加载DLL。这是推荐的方法。
Write your own .exe service and have it load your DLL as needed. This is the recommended approach.
使用Microsoft的SVCHOST.EXE托管您的DLL。让您的DLL导出函数,将 ServiceDLL
值添加到服务的注册表项以指向您的DLL,将服务名称添加到SVCHOST的新组中注册表项,然后将 svchost -k< GroupName>
设置为服务的可执行文件。请参阅以下文章以了解更多详细信息:
Use Microsoft's SVCHOST.EXE to host your DLL. Have your DLL export a ServiceMain()
function, add a ServiceDLL
value to your service's Registry key to point at your DLL, add your service name to a new group in SVCHOST's Registry key, and then set svchost -k <GroupName>
as the executable for your service. See these articles for more details:
不过请注意,MSDN的文档警告了这种方法:
Note, however, that MSDN's Service Programs documentation warns against this approach:
将您的服务写为内核模式驱动程序会导出函数,并在指向DLL文件的服务的注册表项中添加 ServiceDLL
值。有关更多详细信息,请参见本文:
Write your service as a kernel-mode driver that exports a DriverEntry()
function, and add a ServiceDLL
value in your service's Registry key pointing at the DLL file. See this article for more details:
。
除非您正在设计自己的方法,否则我不建议您使用这种方法。硬件。
I would not recommend this approach, unless you are designing your own hardware.
这篇关于如何将dll作为服务运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!