Callbacks in COM objects

I have an ATL project which exposes a COM object. I would like my COM object to call a function specified by the consumer from time to time, or via some other means of notifying the consumer from time to time.

I have tried adding a method in CerberusNative.idl in order to take void function pointers which I would like to call from the COM instance side of things so that the consumer would just need to tell the COM object upon initialization what its callbacks are:

interface ICerberusSession : IDispatch {
    [id(5)] HRESULT SetCallbacks([in] void(*userExit)(int, char *), [in] void(*userAttemptingReconnection)(), [in] void(*userReconnected)());
};

However, there are some problems with my void function pointers:

Severity Code Description Project File Line Suppression State Error MIDL2269 procedures in an object interface must return an HRESULT : [( Parameter 'userExit' ) ] Error MIDL2131 parameter must not be a function : [ Parameter 'userExit' of Procedure 'SetCallbacks' ( Interface 'ICerberusSession' ) ]

In what ways can I get a client application to subscribe to callback functions in my COM object?


做到这一点的正确方法是定义一个独立的接口和其上的方法,并将该接口传递给我的setter方法:

[
    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);
};
链接地址: http://www.djcxy.com/p/81316.html

上一篇: 用Python包装C库:C,Cython还是ctypes?

下一篇: COM对象中的回调