简体   繁体   中英

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

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.

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?

I'd be glad for any hint!

Ben

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)

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#.

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 .

  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 service for easy access from any client anywhere through sockets.

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).

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

Best Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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