Go to Windows Service family--add COM interfaces to Windows services

Source: Internet
Author: User
Tags ole

When we run a Windows service, we typically choose to run in a non-windowed or non-console way, so it's just a daemon and there's no interface for us to interact with.

So what should we do when we want to interact with Windows services in real time?

Quick scenarios for adding real-time interactivity to Windows services

The Windows service is a process, and the program we use for interaction is another process. Our real-time interaction with Windows Services is actually a matter of inter-process communication. All interprocess communication scenarios are basically suitable for real-time interaction scenarios such as sockets, shared memory, pipelines, COM, and so on.

In these scenarios, the development of COM is the fastest, because we are adding COM interfaces to atl-based Windows services.

Introduction to COM

The Component Object model, the English-language Component Object model, abbreviated COM, is the binary interface standard for Microsoft's set of software components. This makes it possible to create inter-process communication and dynamic object creation across programming languages. COM is the foundation of several Microsoft technologies and frameworks, including OLE, OLE Automation, ActiveX, COM +, DCOM, Windows shell, DirectX, Windows Runtime. A detailed description of the Component Object model can be consulted.

Add a COM interface to a service

Creating an ATL-based Windows service can refer to the play to Windows service family-Creating a Windows service.

Next, add the COM interface to the service quickly.

First, an ATL simple object was added to the project, as follows:

When you create an ATL simple object by following the steps above, a file is generated:

Servicecomtest.idl

The contents of the file are as follows:

Import"Oaidl.idl"; Import"Ocidl.idl"; [    Object, uuid (4DDE5CA3-f5d7-4bc3-9045-e697297c5530), dual, nonextensible, pointer_default (unique)]Interfaceiiservicecomtest:idispatch{}; [UUID (54a347ba-7689-4578-a346-c96d924bd637), version (1.0),]library servicecomtestlib{importlib ("Stdole2.tlb"); [UUID (c264868c-91e7-4bfe-8dd9-32d0804e44f6)] coclass Iservicecomtest {[default]Interfaceiiservicecomtest; };};

This IDL file is used to define the COM interface.

Next, add a new method to the interface.

In Class View, locate the interface iiservicecomtest that you just generated:

Then right-click the menu and add the method:

This adds an Add method, X, y as input, and result as output.

You can then see the definition of the Add method in the IDL file:

Interface iiservicecomtest:idispatch{    [ID (1), helpstring (" sum of two integers ")] HRESULT Add ([in] long x, [in] long y, [out, retval] long* result);}; 
Implementing COM interfaces

The method we add to the COM interface is just a declaration, a description, and we must implement this method for other processes to communicate with the service.

This method can be found in the IServiceComTest.cpp file:

STDMETHODIMP Ciservicecomtest::add (long x, long y, long* result) {    //  TODO:  Add implementation code here     return  S_OK;}

The next step is to implement this method, as follows:

STDMETHODIMP Ciservicecomtest::add (long x, long y, long* result) {    *result = x + y;     return S_OK;}

In this way, a complete COM interface and its implementation, even if completed, will need to be tested by a test program called this interface.

Calling COM interfaces

Create a basic console program, and then initialize the test code to be tested with the following code:

#include".. \SERVICECOMTEST\SERVICECOMTEST_I.C"#include".. \servicecomtest\servicecomtest_i.h"#include<iostream>using namespacestd;int_tmain (intARGC, _tchar*argv[]) {Iiservicecomtest*test;    CoInitialize (NULL); Auto HRESULT=CoCreateInstance (Clsid_iservicecomtest, NULL, Clsctx_local_server|Clsctx_inproc_handler, Iid_iiservicecomtest, (void* *) &test); LONG x=1; LONG y=2; LONG result=0; HRESULT= Test->add (x, Y, &result); cout<<"result is"<< result <<Endl; System ("Pause");}

Here, just a demo program that omits the error handling of the code.

Run the program and get the correct result, result is 3, the results are as follows:

Resources

Step by step COM Tutorial

COM in C + +

COM (c + +) programming Tutorials

C + + COM Code example:reading Messages asynchronously

Series Links

Go to Windows Service Series--Create a Windows service

The registration and uninstallation of Windows Service series--debug, release version, and its rationale

Gaming Windows Service Family--no COM interface causes and solutions for Windows service startup failure

Go to Windows Service Series--Analysis of service operation and stop process

Go to Windows Service Series--windows Service Tips

Go to Windows service family--command-line management for Windows services

Play to Windows Service Series--windows service start time Out

Go to Windows service family--use boost.application to quickly build Windows services

Go to Windows Service family--add COM interfaces to Windows services

Go to Windows Service family--add COM interfaces to Windows services

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.