zoukankan      html  css  js  c++  java
  • C++ com 组件 事件 备忘

    [
        object,
        uuid(AECE8D0C-F902-4311-A374-ED3A0EBB6B49),
        dual,
        nonextensible,
        pointer_default(unique)
    ]
    interface ICallbacks : IUnknown
    {
        [id(1)] HRESULT UserExit([in] int errorCode, [in] BSTR errorMessage);
        [id(2)] HRESULT UserAttemptingReconnection();
        [id(3)] HRESULT UserReconnected();
    };
    
    [
        object,
        uuid(B98A7D3F-651A-49BE-9744-2B1D8C896E9E),
        dual,
        nonextensible,
        pointer_default(unique)
    ]
    interface ICerberusSession : IDispatch {
        ...
        [id(5)] HRESULT SetCallbacks([in] ICallbacks* callbacks);
    };

     上面只是创建了一个函数指针回调,也算是可以用了但并非标准windows事件。

    创建标准windows事件步骤如下:

    1.Create a new ATL DLL Project. I've called it ATLEventTest1.
    2.Build Project.
    3.Class View: Right clicked on ATLEventTest1, Add a new ATL Simple Object class. Call it MyObject.
    4.Create the definition with a) Apartment Threading b) Dual Interface c) Connection points and whatever else you need.
    5.Rebuild Project.
    6.Class View: Locate the ATLEventTest1Lib library, right click on _MyObjectEvents, and "Add Method".
    7.Method is of void return type, named someEvent, and takes an int testParam with parameter attribute [in].
    8.You should see "[in] int testParam" as a result of adding the parameter.
    9.On the IDL Attributes tab, change the id if necessary. Click Finish.
    10.Class View: Right click on CMyObject, go to Add... , go to Implement Connection Point.
    11.Select _IMyObjectEvents and click ">" to move it to the implemented connection points list. Click Finish.
    12.Rebuild.
    13.Now, in your CMyObject class, you should be able to call Fire_someEvent(). You will see a new method in your ATLEventTest1.idl file under the dispinterface _IMyObjectEvents; the header file _IMyObjectEvents_CP.h file will be created and will create the broadcast code for Fire_someEvent().

     Adding Connection Points to an existing class:

    The ATL Tutorial demonstrates how to create a control with support for connection points, how to add events, and then how to implement the connection point. ATL implements connection points with the IConnectionPointImpl class.
    
    To implement a connection point, you have two choices:
    
    •Implement your own outgoing event source, by adding a connection point to the control or object.
    
    
    •Reuse a connection point interface defined in another type library.
    
    
    In either case, the Implement Connection Point Wizard uses a type library to do its work.
    
    To add a connection point to a control or object
    
    1.Define a dispinterface in the library block of the .idl file. If you enabled support for connection points when you created the control with the ATL Control Wizard, the dispinterface will already be created. If you did not enable support for connection points when you created the control, you must manually add a dispinterface to the .idl file. The following is an example of a dispinterface. Outgoing interfaces are not required to be dispatch interfaces but many scripting languages such as VBScript and JScript require this, so this example uses two dispinterfaces:
     [
       uuid(3233E37D-BCC0-4871-B277-48AE6B61224A),
       helpstring("Buddy Events")
    ]
    dispinterface DBuddyEvents
    {
       properties:
       methods:
    };
    
    
    Use either the uuidgen.exe or guidgen.exe utility to generate a GUID.
    
    
    2.Add the dispinterface as the [default,source] interface in the coclass for the object in the project's .idl file. Again, if you enabled support for connection points when you created the control, the ATL Control Wizard will create the [default,source] entry. To manually add this entry, add the line in bold:
     coclass Buddy
    {
       [default] interface IBuddy;
       [default,source] dispinterface DBuddyEvents;
    };
    
    
    See the .idl file in the Circ ATL sample for an example.
    
    
    3.Use Class View to add methods and properties to the event interface. Right-click the class in Class View, point to Add on the shortcut menu, and click Add Connection Point.
    
    
    4.In the Source Interfaces list box of the Implement Connection Point Wizard, select Project's interfaces. If you choose an interface for your control and press OK, you will:
    
    •Generate a header file with an event proxy class that implements the code that will make the outgoing calls for the event.
    
    
    •Add an entry to the connection point map.
    
    
    You will also see a list of all of the type libraries on your computer. You should only use one of these other type libraries to define your connection point if you want to implement the exact same outgoing interface found in another type library.
    
    
    To reuse a connection point interface defined in another type library
    
    1.In Class View, right-click a class that implements a BEGIN_COM_MAP macro, point to Add on the shortcut menu, and click Add Connection Point.
    
    
    2.In the Implement Connection Point Wizard, select a type library and an interface in the type library and click Add.
    
    
    3.Edit the .idl file to either:
    
    •Copy the dispinterface from the .idl file for the object whose event-source is being used.
    
    
    •Use the importlib instruction on that type library.
  • 相关阅读:
    Symbol
    前端微信支付步骤
    获取url参数值(可解码中文值)
    HTML5--canvas与svg的使用
    js-图片img转base64格式
    echarts 地图
    echarts 水球图
    react长列表性能优化
    CSS Modules
    react路由
  • 原文地址:https://www.cnblogs.com/nanfei/p/11423184.html
Copyright © 2011-2022 走看看