环境:win7 64位,vs2008,wps2013
错误代码:
BOOL CWPSOper::ConvertToPdf(IDispatch* lpDoc,_bstr_t sTargetPath) { CComVariant varParams[15]; varParams[1].vt = VT_I4; varParams[1].iVal = 1; varParams[0].vt = VT_BYREF|*/VT_VARIANT; varParams[0].pvarVal = &(varParams[1]); CComVariant varRet, varRet2, varRet3; HRESULT hr1 = GetProperty(lpDoc, L"Application", &varRet); HRESULT hr2 = GetProperty(varRet.pdispVal, L"Documents", &varRet2); hr1 = InvokeN(varRet2.pdispVal, L"Item", varParams, 1, &varRet3); } HRESULT InvokeN(IDispatch *pDisp, LPCOLESTR lpszName, VARIANT *varParams, int nParams, VARIANT *pvarRet) { if(pDisp == NULL) return E_FAIL; HRESULT hr; DISPID dispid; hr = pDisp->GetIDsOfNames(IID_NULL, (LPOLESTR*)&lpszName, 1, LOCALE_USER_DEFAULT, &dispid); if (SUCCEEDED(hr)) { DISPPARAMS dispparams = { varParams, NULL, nParams, 0}; return pDisp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparams, pvarRet, NULL, NULL); } return hr; }
解决:
而通过vc添加typelib中类,通过wpsapi.dll生成CDocument,CDocuments,CApplication三个类,然后调试跟到oledisp2.cpp中的
SCODE sc = m_lpDispatch->Invoke(dwDispID, IID_NULL, 0, wFlags,
&dispparams, pvarResult, &excepInfo, &nArgErr);
发现dispparams的iVal值为0x00000001
而我的错误代码中iVal值为0xcccc0001
然后源代码改为varParams[1].lVal = 0x00000001;
结果就返回正确值S_OK了
原因:显而易见,variant是union类型, iVal只是lVal中的后半段内存,但是com处理invoke调用应该统一使用long,不会又是long又是int