1.创建接口 用ATL Simple Object 创建的
2. 客户端调用时候创建com对象
CComPtr<IDispatch> obj; obj.CoCreateInstance(CLS_.... ) obj.CoCreateInstance("obj.objname.1")
3. 参数尽量用 VARIANT js交互认这个
4. 事件回调需要主线程处理, 创建个隐藏窗口来传递消息
5.
c++
处理事件
继承IDispEventImpl
_ATL_FUNC_INFO sfi_OnMsg = {CC_STDCALL, VT_EMPTY, 7, { VT_VARIANT, VT_VARIANT, VT_VARIANT, VT_VARIANT, VT_VARIANT, VT_VARIANT,VT_VARIANT}}; BEGIN_SINK_MAP(CSynthImageHelper) SINK_ENTRY_INFO(1, DIID__event, 1000, oncall, &sfi_OnMsg) END_SINK_MAP()
调用
DispEventAdvise 来绑定com和事件响应
6.
CComVariant
CComPtr
2个智能指针非常有用
7. js可以把js函数当回调给com, com中事件完成后可以调用
1 CComVariant callback; 2 3 假设3个参数 4 CComVariant result; 5 VARIANTARG args[3]; 6 DISPPARAMS dispParams = {0}; 7 dispParams.cNamedArgs = 0; 8 dispParams.cArgs = sizeof(args) / sizeof(VARIANTARG); 9 dispParams.rgvarg = args; 10 11 memcpy((void*) &dispParams.rgvarg[0], (void*)&arg2, sizeof(VARIANT)); 12 memcpy((void*) &dispParams.rgvarg[1], (void*)&arg1, sizeof(VARIANT)); 13 memcpy((void*) &dispParams.rgvarg[2], (void*)&arg0, sizeof(VARIANT)); 14 15 16 if (callback.vt == VT_DISPATCH && callback.pdispVal) 17 { 18 EXCEPINFO excepInfo; 19 memset(&excepInfo, 0, sizeof(EXCEPINFO)); 20 21 UINT nArgErr = (UINT)-1; // initialize to invalid arg 22 23 24 hr = callback.pdispVal->Invoke(0, IID_NULL, LOCALE_USER_DEFAULT, 25 DISPATCH_METHOD, &dispParams, &result, &excepInfo, &nArgErr); 26 }