简体   繁体   English

如何运行dll作为服务?

[英]How to run a dll as a service?

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以及如何编写服务以及如何使用rundll32运行一个dll,但现在我想编写一个在windows中作为服务安装的DLL

I don't know if that's possible or which function in dll should be exported? 我不知道是否可能导出dll或导出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. 有一些不同的方法可以将DLL作为服务运行。 You can either: 你可以:

  1. Write your own .exe service and have it load your DLL as needed. 编写您自己的.exe服务,并根据需要加载您的DLL。 This is the recommended approach. 这是推荐的方法。

  2. Use Microsoft's SVCHOST.EXE to host your DLL. 使用Microsoft的SVCHOST.EXE来托管您的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. 让您的DLL导出ServiceMain()函数,将ServiceDLL值添加到服务的注册表项以指向您的DLL,将您的服务名称添加到SVCHOST的注册表项中的新组,然后将svchost -k <GroupName>为可执行文件为您服务。 See these articles for more details: 有关详细信息,请参阅这些文章

    A description of Svchost.exe Svchost.exe的描述

    Getting Started with SVCHOST.EXE Troubleshooting SVCHOST.EXE入门疑难解答

    Tricks with SVCHOST.EXE 使用SVCHOST.EXE技巧

    The Service Host 服务主机

    Note, however, that MSDN's Service Programs documentation warns against this approach: 但请注意,MSDN的服务程序文档警告这种方法:

    A service program created with the type SERVICE_WIN32_SHARE_PROCESS contains code for more than one service, enabling them to share code. 使用SERVICE_WIN32_SHARE_PROCESS类型创建的服务程序包含多个服务的代码,使它们能够共享代码。 An example of a service program that does this is the generic service host process, Svchost.exe, which hosts internal Windows services. 执行此操作的服务程序示例是通用服务主机进程Svchost.exe,它承载内部Windows服务。 Note that Svchost.exe is reserved for use by the operating system and should not be used by non-Windows services. 请注意,Svchost.exe保留供操作系统使用,不应由非Windows服务使用。 Instead, developers should implement their own service hosting programs. 相反,开发人员应该实现自己的服务托管程序。

  3. 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. 将您的服务编写为导出DriverEntry()函数的内核模式驱动程序 ,并在服务的注册表项中指向DLL文件中添加ServiceDLL值。 See this article for more details: 有关详细信息,请参阅此文章:

    Driver Development Part 1: Introduction to Drivers . 驱动程序开发第1部分:驱动程序简介

    I would not recommend this approach, unless you are designing your own hardware. 除非您正在设计自己的硬件,否则我不建议使用此方法。

There's actually no inherent reason why you can't use rundll32.exe as the host executable, though use of rundll32 isn't recommended . 实际上没有固有的原因你不能使用rundll32.exe作为主机可执行文件,但不推荐使用rundll32。 (To expand on that: I gather you're trying to build a DLL service as an academic exercise, which is fine. In production, you should of course use an EXE, as you've already done.) (为了扩展它:我收集你正在尝试构建一个DLL服务作为学术练习,这很好。在生产中,你当然应该使用EXE,就像你已经做过的那样。)

Your main function should have this signature : 你的主要功能应该有这个签名

void CALLBACK MyServiceEntry(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)

and should call StartServiceCtrlDispatcher , in the same way as the main() or WinMain() function in a conventional service. 应该调用StartServiceCtrlDispatcher ,方法与传统服务中的main()WinMain()函数相同。

You would then install the service to use the following command line: 然后,您将安装该服务以使用以下命令行:

rundll32 MyService.dll,MyServiceEntry

For an academic exercise, it would also be acceptable to use svchost.exe as described in Remy's answer, but it is even more important not to use that in a production context: the use of rundll32 by third parties is supported but not recommended; 对于学术练习,使用svchost.exe的答案中描述的svchost.exe也是可以接受的,但更重要的是不要在生产环境中使用它:第三方使用rundll32但不建议使用; the use of svchost by third parties is explicitly unsupported. 明确不支持第三方使用svchost。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM