如题,zebra官网给出的扫码枪SDK示例是基于MFC的,自己用的是QT,加大了开发难度(悔不当初用MFC),同时也对com组件不熟悉,导致折腾了好几天才搞定。也不知道这么做能不能满足QT跨平台的初衷,Windows是能用的,给大家参考一下。
感谢这几篇文章,给了我很多启发,现在列出:
这里是引用
1、实时接收com组件抛出的事件
https://blog.csdn.net/angellove156/article/details/41960069?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
2、COM连接点事件event
https://blog.csdn.net/vlily/article/details/70314817?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
1、下面是代码,命令发送这部分很容易解决,稍微改下就行,主要是接收com组件的事件,我这边采用了回调函数的方式接收信息。
private:
DWORD m_dwRefCount;
Callback //自己定义的回调函数
void * user;
public:
void SetCallbackFunc(CallbackGetScannerData cbk, void* user)
{
this->cbkGetData = cbk;
this->user = user;
}
CEventSink() { m_dwRefCount = 0; }
virtual ~CEventSink(void) {}
STDMETHODIMP GetTypeInfoCount(UINT *pctinfo) { return E_NOTIMPL; }
STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) { return E_NOTIMPL; }
STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_NOTIMPL; }
STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
LONG *lValue = pVarResult->plVal;
switch (dispIdMember)
{
case 3: //能获取到扫描枪扫的条码信息
{
VARIANT varlValue;
VARIANT varlValue1;
short lValue = 0;
BSTR lValue1;
varlValue = (pDispParams->rgvarg)[1];
varlValue1 = (pDispParams->rgvarg)[0];
//varlValue1.pbstrVal
lValue = V_I2(&varlValue);
lValue1 = SysAllocString(*(varlValue1.pbstrVal));
if(cbkGetData)
//放自己定义的回调函数
break;
}
default:
break;
}
//printf(" ---------the result %d\n", *lValue);
//OnReceiver(lValue);
return S_OK;
}
STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppv)
{
if ((iid == __uuidof(_ICoreScannerEvents)) || //(iid == __uuidof(IMarshal)) ||
(iid == __uuidof(IDispatch)) ||
(iid == __uuidof(IUnknown)))
{
m_dwRefCount++;
*ppv = this;
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
STDMETHOD_(ULONG, AddRef)()
{
return InterlockedIncrement(&m_dwRefCount);
}
STDMETHOD_(ULONG, Release)()
{
InterlockedDecrement(&m_dwRefCount);
if (m_dwRefCount != 0)
return m_dwRefCount;
delete this;
return 0;
}
};
2、下面是调用的这个类的代码
if(ScannerInterface)
{
HRESULT hr = S_FALSE;
//Create the Scanner COM object
IConnectionPointContainer *pCPC;
hr = ScannerInterface->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC);
if (!SUCCEEDED(hr))
{
return -1;
}
//获取连接点
hr = pCPC->FindConnectionPoint(DIID__ICoreScannerEvents, &pCP);
if (!SUCCEEDED(hr))
{
return -1;
}
pCPC->Release();
IUnknown *pSinkUnk;
ScannerEventSink = new CEventSink();
ScannerEventSink->SetCallbackFunc(//自定义的回调函数, user);
hr = ScannerEventSink->QueryInterface(DIID__ICoreScannerEvents, (void **)&pSinkUnk);
hr = pCP->Advise(pSinkUnk, &dwAdvise);//注册其接收接口指针
if (!SUCCEEDED(hr))
{
return -1;
}
}