Go to the Windows Service series & mdash; and add COM and Windows interfaces to windows Services

Source: Internet
Author: User

Go to Windows Service series-add COM and Windows interfaces to windows Services

When running a Windows service, we usually choose to run it in Non-window or non-Console mode. In this way, it is just a background program, there is no interface for interaction.

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

Quick Solution for adding real-time interaction to Windows Services

Windows is a process, and the program we use for interaction is another process. Real-time interaction with Windows Services is actually a problem of inter-process communication. All inter-process communication solutions are basically applicable to real-time interaction solutions, such as Socket, shared memory, pipelines, and COM.

Among these solutions, the development of COM is the fastest, because we add COM interfaces to the ATL-based Windows service.

COM Introduction

The Component Object Model (COM) is a set of binary interface standards for Microsoft software components. This makes inter-process communication and dynamic object creation across programming languages possible. COM is the basis of a number of Microsoft technologies and frameworks, including OLE, OLE Automation, ActiveX, COM +, DCOM, Windows shell, DirectX, and Windows Runtime. For details, refer to component object model.

Add COM interface to service

For details about how to create an ATL-based Windows Service, refer to "go to the Windows Service series" to create a Windows service.

Next, add a COM interface to the service.

First, add an ATL simple object to the project, as shown below:

ServiceComTest. idl

The file content is as follows:

import "oaidl.idl";import "ocidl.idl";[    object,    uuid(4DDE5CA3-F5D7-4BC3-9045-E697297C5530),    dual,    nonextensible,    pointer_default(unique)]interface IIServiceComTest : IDispatch{};[    uuid(54A347BA-7689-4578-A346-C96D924BD637),    version(1.0),]library ServiceComTestLib{    importlib("stdole2.tlb");    [        uuid(C264868C-91E7-4BFE-8DD9-32D0804E44F6)            ]    coclass IServiceComTest    {        [default] interface IIServiceComTest;    };};

This idl file is used to define the COM interface.

Next, add a new method to the interface.

In the Class View, find the generated interface IIServiceComTest:

Interface IIServiceComTest: IDispatch {[id (1), helpstring ("adding two integers")] HRESULT add ([in] LONG x, [in] LONG y, [out, retval] LONG * result );};Implement COM interface

The method we add to the COM interface is only a declaration and description. We must implement this method before other processes can communicate with this service.

You can find this method in the IServiceComTest. cpp file:

STDMETHODIMP CIServiceComTest: add (LONG x, LONG y, LONG * result) {// TODO: add the implementation code return S_ OK here ;}

This method is implemented 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 are completed. Next, you need to call this interface through the test program for testing.

Call the COM interface

Create a basic console program and test the initialization test code. The Code is as follows:

#include "..\ServiceComTest\ServiceComTest_i.c"#include "..\ServiceComTest\ServiceComTest_i.h"#include <iostream>using namespace std;int _tmain(int argc, _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, it is just a demo program that skips code error handling.

Run the program and get the correct result. result is 3. The result is as follows:

References

Step by Step COM Tutorial

COM in C ++

COM (C ++) programming tutorials

C/C ++ COM Code Example: Reading Messages Asynchronously

Series links

Go to the Windows Service series-create a Windows Service

How to register and uninstall the Debug and Release versions of the Windows Service series

Go to the Windows Service series -- Why Windows service fails to be started without the COM interface and the Solution

Go to the Windows Service series-analysis of service running and stopping Processes

Turn to the Windows Service series-Tips for Windows Services

Go to Windows Service series-command line management for Windows Services

Fun with Windows Service series-Windows Service Startup timeout

Go to the Windows Service series-use Boost. Application to quickly build Windows Services

Go to Windows Service series-add COM interfaces to Windows Services

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.