Writing COM components in pure C + +

Source: Internet
Author: User
Tags define

This article provides an in-process (DLL) COM server that is fully implemented in C + + and does not provide any support for ATL or MFC. Writing COM objects in this way gives you insight into how COM processes the server in-process and how COM creates the class factory. With this simple framework provided in this article, you can implement very basic COM components, such as shell extensions (shell Extensions). If you find any problems during use, please feedback it to vckbase@public.hk.hi.cn.

Here are the steps you can take to write your own COM objects in the way that this article says:

The first step: Write a header file that contains the following contents:

1, include file comdef.h: #include.

2, define the GUID of the COM server.

_declspec(selectany) GUID CLSID_Mine = { 0xdc186800,
     0x657f,
     0x11d4,
     {0xb0, 0xb5, 0x0, 0x50, 0xba, 0xbf, 0xc9, 0x4}
    };

3. Gives the IID of the interface and the definition of the method to be implemented by this interface. The method by which the client will use the IID and interface of this interface.

interface __declspec(uuid("F614FB00-6702-11d4-B0B7-0050BABFC904")) ImyInterface : public IUnknown
{
  STDMETHOD(Square)(long *pVal)PURE;
  STDMETHOD(Cube)(long *pVal)PURE;
};
客户端使用此接口:
HRESULT hr;
ImyInterface *pmine=(0);
hr = CoCreateInstance(CLSID_Mine,        // COM 服务器的CLSID
           NULL,      //不支持聚合
           CLSCTX_INPROC_SERVER,   // 是个DLL
           __uuidof(ImyInterface),  // 接口的IID
           (void**)&pmine
           );

Another way to get the CLSID of a COM object from the registry is to call the CLSIDFromProgID () function, but you must pass the ProgID of the component to the function.

Step Two: You must provide an implementation for the interface you define, and this article uses the method to create a new class that inherits from the interface:

This class implements single interface IMyInterface ...

//

//

class CmyInterface : public CComBase<> ,
         public InterfaceImpl
{
public:
  CmyInterface();
  virtual ~CmyInterface();
  // 我们必须要为QueryInterface 编写代码
  STDMETHOD(QueryInterface)(REFIID riid,LPVOID *ppv);
  // ImyInterface 接口方法
  STDMETHOD(Square)(long *pVal);
  STDMETHOD(Cube)(long *pVal);
};

The template class interfaceimpl<> provides an implementation of the interface reference count. Here we can inherit from multiple interfaces, so that we can implement multiple interfaces in a COM component.

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.