C ++ ATL COM:函数CallBack

时间:2018-06-01 11:39:19

标签: c++ com atl

我正在尝试在ATL COM项目中创建一个具有以下声明的函数Callback:

int Dll16Exp NSetNewPartGeomHandleCB proto ((N_GET_NEW_PART_GEOM_CB));
typedef int (*N_GET_NEW_PART_GEOM_CB) proto((LPSTR,int(*)proto((int,int,int,LPSTR))));

我能够为其他具有基本数据类型的函数创建一个Callback函数:

int Dll16Exp NSetErrorHandlerCB proto ((N_SHOW_ERROR_CB));
typedef int (*N_SHOW_ERROR_CB) proto((int,int,int, LPSTR ));

以下实施:

COM服务器:

IDL文件:

interface ICallBacks : IUnknown {
[id(1)] HRESULT ErrorLogCallBack([in] INT ID, [in] INT ErrorType, [in] INT Who, [in] LPSTR ErrStr);
};
interface IN32WrapperProxy : IDispatch{
[id(1)] HRESULT SetFctCB_N32Wrapper_ErrorLog(ICallBacks* CallBack, [out, retval] INT* Result);
};

CallBacks.h:

public:
    STDMETHOD(ErrorLogCallBack)(INT ID, INT ErrorType, INT Who, LPSTR ErrStr);

N32WrapperProxy.h:

public:
    STDMETHOD(SetFctCB_N32Wrapper_SetNewSheetGeomHandle)(ICallBacks* CallBack, INT* Result);

N32WrapperProxy.cpp:

typedef HRESULT(*ErrorLogCB)(int ID, int ErrorType, int Who, LPSTR ErrStr);
int ErrorLogLocal(int ID, int ErrorType, int Who, LPSTR ErrStr)
{
    g_cb1->ErrorLogCallBack(ID, ErrorType, Who, ErrStr);
    return 1;
}
STDMETHODIMP CN32WrapperProxy::SetFctCB_N32Wrapper_ErrorLog(ICallBacks* CallBack, INT* Result)
{
    g_cb1 = CallBack;
    *Result = NSetErrorHandlerCB(ErrorLogLocal);
    return S_OK;
}

客户:

class CCallbacksImpl : public ICallBacks
{
public:

    STDMETHODIMP_(ULONG) AddRef() { return 2; }
    STDMETHODIMP_(ULONG) Release() { return 1; }

    STDMETHODIMP QueryInterface(REFIID riid, void ** ppv)
    {
        if (riid == IID_ICallBacks || riid == IID_IUnknown)
        {
            *ppv = (void *) static_cast<ICallBacks *>(this);
            return NOERROR;
        }
        return E_NOINTERFACE;
    }

    STDMETHODIMP ErrorLogCallBack(int ID, int ErrorType, int Who, LPSTR ErrStr)
    {
        ErrorLog(ID, ErrorType,Who,ErrStr);
        return NOERROR;
    }

    STDMETHODIMP raw_ErrorLogCallBack(
        /*[in]*/ int ID,
        /*[in]*/ int ErrorType,
        /*[in]*/ int who,
        /*[in]*/ LPSTR ErrStr)
    {
        return NOERROR;
    }
} ;
CCallbacksImpl callbackF;

int CTestApp::Init()
{
    CoInitialize(NULL);
    int retVal;
    static char BufferA[1024];
    GetTempPathA(1024, BufferA);
   _bstr_t bstrt(BufferA);
   pN32Proxy= IN32WrapperProxyPtr(__uuidof(N32WrapperProxy));
   IN32WrapperProxyPtr pN32Proxy;
   retVal = pN32Proxy->SetFctCB_N32Wrapper_ErrorLog(&callbackF);
}

我想知道是否可以在其参数中创建一个不仅包含基本数据类型的函数回调,就像本文开头所描述的那样。如果是这样,我怎么能实现这一点。

0 个答案:

没有答案