如何在 Visual Studio 2010 中创建 C++ Windows 服务并与之通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6581858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:18:00  来源:igfitidea点击:

How to create and communicate with a C++ Windows Service in Visual Studio 2010?

c++windowsvisual-studio-2010service

提问by Ben

I have a (kind of) database implemented in C++. Now I want to create a Windows service for querying it, because P-invoking it is not an option, as the database would have to be loaded with every query, which takes several minutes.

我有一个用 C++ 实现的(某种)数据库。现在我想创建一个 Windows 服务来查询它,因为 P 调用它不是一个选项,因为每个查询都必须加载数据库,这需要几分钟。

But I face several problems:

但我面临几个问题:

  1. How can I create a C++ Windows Service in VS2010? The template has been removed (why???), can I use a 2008 template and convert it? If yes: where do I find such a template?

  2. Supposed I manage to create a C++ Windows service: what are my options to communicate with this sevice (from c sharp)? What are the advantages/disadvantages?

  1. 如何在 VS2010 中创建 C++ Windows 服务?模板已被删除(为什么???),我可以使用 2008 模板并转换它吗?如果是:我在哪里可以找到这样的模板?

  2. 假设我设法创建了一个 C++ Windows 服务:我有哪些与该服务通信的选项(来自 c 锐利)?有什么优点/缺点?

I'd be glad for any hint!

我很高兴有任何提示!

Ben

回答by Stephan

I don't know why, but the template is really gone.

不知道为什么,模板真的没了。

For communicating between your application and the service WCF would be an option. One advantage of WCF is, that you can easily switch the transport layer (HTTP, TCP, Shared Memory)

对于您的应用程序和服务 WCF 之间的通信将是一个选项。WCF 的优点之一是,您可以轻松切换传输层(HTTP、TCP、共享内存)

Have a look at this MSDN page. It describes exactly what you are trying to achieve: Host a WCF Service in a Windows Service. It even contains a simple Windows Service implementation at the end. Unfortunately no C++ but C#.

看看这个 MSDN 页面。它准确描述了您要实现的目标:在 Windows 服务中托管 WCF 服务。它甚至在最后包含一个简单的 Windows 服务实现。不幸的是没有 C++ 而是 C#。

回答by Naszta

You should implement the followin functions:

您应该实现以下功能:

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
VOID WINAPI ServiceHandler(DWORD fdwControl);
VOID ReportSvcStatus( DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint );

More info on MSDN. An example: link.

有关MSDN 的更多信息。一个例子:链接

回答by t.g.

  1. you might want to take a look at POCO Project,in particular this class. There are examples if you download the code.
  2. you might also want to implement a web servicefor easy access from any client anywhere through sockets.
  1. 你可能想看看POCO 项目,特别是这个类。如果您下载代码,则有示例。
  2. 您可能还想实现一个Web 服务,以便通过套接字从任何地方的任何客户端轻松访问。

回答by ur.

What I did some years ago was an COM out-of-process server which was a Windows Service. It worked fine and you can access it from C# (and many other languages ...) easily. If you have no COM experience it might become hard (depending on how complex your interface is).

几年前我所做的是一个 COM 进程外服务器,它是一个 Windows 服务。它运行良好,您可以轻松地从 C#(和许多其他语言...)访问它。如果您没有 COM 经验,这可能会变得很困难(取决于您的界面的复杂程度)。

回答by Sebasti?o Relson Reis da Luz

Maybe you can use ! ATL Project and then select Service (EXE).

也许你可以用!ATL 项目,然后选择服务 (EXE)。

Best Regards

此致