如题,zebra官网给出的扫码枪SDK示例是基于MFC的,自己用的是QT,加大了开发难度(悔不当初用MFC),同时也对com组件不熟悉,导致折腾了好几天才搞定。也不知道这么做能不能满足QT跨平台的初衷,Windows是能用的,给大家参考一下。
1、下面是代码,命令发送这部分很容易解决,稍微改下就行,主要是接收com组件的事件,我这边采用了回调函数的方式接收信息。
1 private: 2 DWORD m_dwRefCount; 3 Callback //自己定义的回调函数 4 void * user; 5 public: 6 void SetCallbackFunc(CallbackGetScannerData cbk, void* user) 7 { 8 this->cbkGetData = cbk; 9 this->user = user; 10 } 11 12 CEventSink() { m_dwRefCount = 0; } 13 virtual ~CEventSink(void) {} 14 15 STDMETHODIMP GetTypeInfoCount(UINT *pctinfo) { return E_NOTIMPL; } 16 STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) { return E_NOTIMPL; } 17 STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_NOTIMPL; } 18 19 STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) 20 { 21 LONG *lValue = pVarResult->plVal; 22 switch (dispIdMember) 23 { 24 case 3: //能获取到扫描枪扫的条码信息 25 { 26 VARIANT varlValue; 27 VARIANT varlValue1; 28 short lValue = 0; 29 BSTR lValue1; 30 varlValue = (pDispParams->rgvarg)[1]; 31 varlValue1 = (pDispParams->rgvarg)[0]; 32 //varlValue1.pbstrVal 33 lValue = V_I2(&varlValue); 34 lValue1 = SysAllocString(*(varlValue1.pbstrVal)); 35 if(cbkGetData) 36 //放自己定义的回调函数 37 break; 38 } 39 default: 40 break; 41 } 42 //printf(" ---------the result %d\n", *lValue); 43 //OnReceiver(lValue); 44 return S_OK; 45 } 46 STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppv) 47 { 48 if ((iid == __uuidof(_ICoreScannerEvents)) || //(iid == __uuidof(IMarshal)) || 49 (iid == __uuidof(IDispatch)) || 50 (iid == __uuidof(IUnknown))) 51 { 52 m_dwRefCount++; 53 *ppv = this; 54 } 55 else 56 { 57 *ppv = NULL; 58 return E_NOINTERFACE; 59 } 60 return S_OK; 61 } 62 STDMETHOD_(ULONG, AddRef)() 63 { 64 return InterlockedIncrement(&m_dwRefCount); 65 } 66 STDMETHOD_(ULONG, Release)() 67 { 68 InterlockedDecrement(&m_dwRefCount); 69 if (m_dwRefCount != 0) 70 return m_dwRefCount; 71 delete this; 72 return 0; 73 } 74 };
2、下面是调用的这个类的代码
1 if(ScannerInterface) 2 { 3 HRESULT hr = S_FALSE; 4 //Create the Scanner COM object 5 IConnectionPointContainer *pCPC; 6 hr = ScannerInterface->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC); 7 if (!SUCCEEDED(hr)) 8 { 9 return -1; 10 } 11 12 //获取连接点 13 hr = pCPC->FindConnectionPoint(DIID__ICoreScannerEvents, &pCP); 14 if (!SUCCEEDED(hr)) 15 { 16 return -1; 17 } 18 pCPC->Release(); 19 20 IUnknown *pSinkUnk; 21 ScannerEventSink = new CEventSink(); 22 ScannerEventSink->SetCallbackFunc(//自定义的回调函数, user); 23 24 hr = ScannerEventSink->QueryInterface(DIID__ICoreScannerEvents, (void **)&pSinkUnk); 25 hr = pCP->Advise(pSinkUnk, &dwAdvise);//注册其接收接口指针 26 if (!SUCCEEDED(hr)) 27 { 28 return -1; 29 } 30 }