使用QT完成zebra扫码枪的命令发送和接收com组件的事件

如题,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;
		}
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值