简体   繁体   English

将 UNIX 守护进程移植到 Windows 服务

[英]Porting a UNIX daemon to a Windows Service

I wrote a UNIX daemon, in C, which I want to port to Windows.我在 C 中编写了一个 UNIX 守护程序,我想将它移植到 Windows。 My target is Windows 10.我的目标是 Windows 10。

When I search on how to create a Windows service, I am met by an approach using .NET and C# which I both want to avoid at all cost.当我搜索如何创建 Windows 服务时,我遇到了一种使用 .NET 和 C#的方法,我都想不惜一切代价避免。

How can I make a simple straightforward service in C, without kitchen sinks that Microsoft tries to unload on me?如何在 C 中进行简单直接的服务,而无需 Microsoft 试图卸载我的厨房水槽? If I really have to, I would consider C++, but C# and.Net are simply taking it too far.如果我真的需要,我会考虑 C++,但 C# 和 .Net 只是太过分了。

I'm fine with switching to a different compiler too, if this is easier outside of VisualStudio.如果这在 VisualStudio 之外更容易,我也可以切换到不同的编译器。 (Currently I am using Visual Studio 2019, latest update.) (目前我使用的是 Visual Studio 2019,最新更新。)

NOTE: My Linux daemon just has one dependency: libhidapi which is available for Windows.注意:我的 Linux 守护程序只有一个依赖项:libhidapi,它可用于 Windows。

UPDATE更新

No C++ templates available.没有可用的 C++ 模板。

在此处输入图像描述

This comes down to creating two different applications (.exe)这归结为创建两个不同的应用程序 (.exe)

One to run the service using StartServiceCtrlDispatcher() where the dispatching function calls RegisterServiceCtrlHandlerEx() to register a controller.使用StartServiceCtrlDispatcher()运行服务,其中调度 function 调用RegisterServiceCtrlHandlerEx()来注册 controller。

One to install the service using CreateService()一种使用CreateService()安装服务

It is possible to skip on the installer, and use the sc command line utility for that, which is part of the OS.可以跳过安装程序,并为此使用sc命令行实用程序,它是操作系统的一部分。

See full example .查看完整示例

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

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