如何将dll作为服务运行? [英] How to run a dll as a service?

查看:655
本文介绍了如何将dll作为服务运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何编写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:


  1. 编写自己的.exe服务,并根据需要加载DLL。这是推荐的方法。

  1. Write your own .exe service and have it load your DLL as needed. This is the recommended approach.

使用Microsoft的SVCHOST.EXE托管您的DLL。让您的DLL导出 ServiceMain() 函数,将 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:

对Svchost.exe的描述

SVCHOST.EXE疑难解答入门

SVCHOST.EXE的技巧

服务主机

不过请注意,MSDN的服务程序文档警告了这种方法:

Note, however, that MSDN's Service Programs documentation warns against this approach:


使用SERVICE_WIN32_SHARE_PROCESS类型创建的服务程序包含一项服务的代码,使他们能够共享代码。可以执行此操作的服务程序示例是通用服务宿主进程Svchost.exe,该进程承载内部Windows服务。 请注意,Svchost.exe保留供操作系统使用,而不应由非Windows服务使用。相反,开发人员应实施自己的服务托管程序。


  • 将您的服务写为内核模式驱动程序会导出 DriverEntry() 函数,并在指向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:

    驱动程序开发第1部分:驱动程序简介

    除非您正在设计自己的方法,否则我不建议您使用这种方法。硬件。

    I would not recommend this approach, unless you are designing your own hardware.

    这篇关于如何将dll作为服务运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆