⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atlmq.h

📁 Visual C++.net编成指南一书的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
		ATLASSERT(m_hQueue != NULL);
		return MQReceiveMessage(m_hQueue, 0, MQ_ACTION_PEEK_NEXT, pMsgProps, NULL, NULL, hCursor, NULL);
	}

	HRESULT ReceiveWithCallback(
		CMQMessageProps *pProps,
		PMQRECEIVECALLBACK pfnCallback,
		DWORD dwTimeout=INFINITE) throw()
	{
		ATLASSERT(m_hQueue != NULL);
		return MQReceiveMessage(m_hQueue, dwTimeout, MQ_ACTION_RECEIVE, pProps, NULL, pfnCallback, NULL, NULL);
	}


	HRESULT ReceiveOverlapped(
		CMQMessageProps *pProps,
		OVERLAPPED *pOverlapped,
		DWORD dwTimeout=INFINITE) throw()
	{
		ATLASSERT(m_hQueue != NULL);
		return MQReceiveMessage(m_hQueue, dwTimeout, MQ_ACTION_RECEIVE, pProps, pOverlapped, NULL, NULL, NULL);
	}

	HRESULT ReceiveWithIoCompletion(CMQMessageProps *pProps, DWORD dwTimeout=INFINITE) throw()
	{
		ATLASSERT(m_hQueue != NULL);

		// The CMQOverlapped structure allocated here, must be
		// freed when the request is retrieved from the
		// IoCompletion port
		CMQOverlapped *pOverlapped = NULL;
		ATLTRY(pOverlapped = new CMQOverlapped);
		if (!pOverlapped)
			return E_OUTOFMEMORY;
		memset(pOverlapped, 0x00, sizeof(CMQOverlapped));

		pOverlapped->m_pProps = pProps;
		pOverlapped->m_pQueue = this;
		return MQReceiveMessage(m_hQueue, dwTimeout, MQ_ACTION_RECEIVE, pProps, pOverlapped, NULL, NULL, NULL);
	}

	HRESULT Send(MQMSGPROPS *pMsgProps, ITransaction *pTransaction = NULL) throw()
	{
		ATLASSERT(m_hQueue != NULL);
		return MQSendMessage(GetHandle(), pMsgProps, pTransaction);
	}

	HRESULT Close() throw()
	{
		HRESULT hr = S_OK;
		if (m_hQueue)
			hr = MQCloseQueue(m_hQueue);
		m_hQueue = NULL;
		return hr;
	}
		
	~CMQQueue() throw()
	{
		if (m_hQueue)
			Close();
	}

	HRESULT Create(MQQUEUEPROPS *pQueueProps, PSECURITY_DESCRIPTOR pSecurityDescriptor=NULL) throw()
	{
		DWORD dwFormatNameLen = sizeof(m_wszFormatName)/sizeof(WCHAR);
		return MQCreateQueue(pSecurityDescriptor, pQueueProps, m_wszFormatName, &dwFormatNameLen);
	}

	HRESULT Create(
		LPCWSTR wszPathName,
		LPCWSTR wszLabel,
		BOOL bTransactional=FALSE,
		PSECURITY_DESCRIPTOR pSecurityDescriptor=NULL) throw()
	{
		CMQQueueProps qProps(CMQQueueProps::MQ_TRANSFER_DIR_PUT);
		qProps.Add(PROPID_Q_PATHNAME, (void *) wszPathName);
		qProps.Add(PROPID_Q_LABEL, (void *) wszLabel);
		if (bTransactional)
		{
			int nIndex = qProps.Add(PROPID_Q_TRANSACTION);
			qProps.m_rgProps[nIndex].bVal = MQ_TRANSACTIONAL;
		}
		return Create(&qProps, pSecurityDescriptor);
	}


	HRESULT Delete() throw()
	{
		ATLASSERT(m_wszFormatName[0]);	// you should set the format name first
		return MQDeleteQueue(m_wszFormatName);
	}

	HRESULT GetQueueProperties(MQQUEUEPROPS *pQueueProps) throw()
	{
		ATLASSERT(m_wszFormatName[0]);	// you should set the format name first
		return MQGetQueueProperties(m_wszFormatName, pQueueProps);
	}

	HRESULT SetQueueProperties(MQQUEUEPROPS *pQueueProps) throw()
	{
		ATLASSERT(m_wszFormatName[0]);	// you should set the format name first
		return MQSetQueueProperties(m_wszFormatName, pQueueProps);
	}

	HRESULT CreateCursor(HANDLE *phHandle) throw()
	{
		ATLASSERT(m_hQueue);
		ATLASSERT(phHandle);
		return MQCreateCursor(m_hQueue, phHandle);
	}

	static HRESULT CloseCursor(HANDLE hCursor) throw()
	{
		ATLASSERT(hCursor!=NULL);
		return MQCloseCursor(hCursor);
	}

	static HRESULT BeginTransaction(ITransaction **ppTransaction) throw()
	{
		ATLASSERT(ppTransaction);
		return MQBeginTransaction(ppTransaction);
	}
};


// class CMQCursor
// This class encapsulates MSMQ cursor related methods
class CMQCursor
{
protected:
	HANDLE m_hCursor;
public:
	CMQCursor() throw() : m_hCursor(NULL)
	{

	}

	~CMQCursor() throw()
	{
		if (m_hCursor)
			Close();
	}

	void Attach(HANDLE hCursor) throw()
	{
		ATLASSERT(m_hCursor==NULL);
		m_hCursor = hCursor;
	}

	HANDLE Detach() throw()
	{
		HANDLE hCursor = m_hCursor;
		m_hCursor = NULL;
		return hCursor;
	}

	HRESULT Create(HANDLE hQueue) throw()
	{
		ATLASSERT(m_hCursor==NULL);
		return MQCreateCursor(hQueue, &m_hCursor);
	}

	HRESULT Close() throw()
	{
		ATLASSERT(m_hCursor!=NULL);
		HRESULT hr = MQCloseCursor(m_hCursor);
		m_hCursor = NULL;
		return hr;
	}

	operator HANDLE() throw()
	{
		return m_hCursor;
	}
};

// class CMQColumnSet
// Simple wrapper around an MSQM column set
class CMQColumnSet : public MQCOLUMNSET
{
protected:
	CAtlArray<PROPID> m_Ids;

public:
	CMQColumnSet() throw()
	{
		cCol = 0;
		aCol = NULL;
	}

	int Add(PROPID propId) throw()
	{
		int nIndex = (int)m_Ids.Add(propId);
		if (nIndex < 0)
			return nIndex;
		// update the MQCOLUMNSET members
		cCol = (int)m_Ids.GetCount();
		aCol = m_Ids.GetData();
		return nIndex;
	}
};

// class CMQPropVariantIn
// Simple wrapper around an MQPROPVARIANT
// Notice that this class is not suitable
// for receiving property values from MSMQ
// since it allocates memory using malloc
// CMQPropVariantRet can be used to receive
// property values from MSMQ
class CMQPropVariantIn : public MQPROPVARIANT
{
public:
	CMQPropVariantIn() throw()
	{
		vt = VT_NULL;
	}

	~CMQPropVariantIn() throw()
	{
		Clear();
	}

	CMQPropVariantIn(int n, VARTYPE vType=VT_I4) throw()
	{
		Set(n, vType);
	}

	CMQPropVariantIn(UINT n) throw()
	{
		Set((int) n, VT_UI4);
	}

	CMQPropVariantIn(unsigned char src) throw()
	{
		Set(src);
	}

	CMQPropVariantIn(short src) throw()
	{
		Set(src);
	}

	CMQPropVariantIn(USHORT src) throw()
	{
		Set(src);
	}

	CMQPropVariantIn(bool src) throw()
	{
		Set(src);
	}

	void Set(int n, VARTYPE vType=VT_I4) throw()
	{
		Clear();
		vt = vType;
		lVal = (long) n;
	}

	void Set(unsigned char b) throw()
	{
		Clear();
		vt = VT_UI1;
		bVal = b;
	}

	void Set(short s) throw()
	{
		Clear();
		vt = VT_I2;
		iVal = s;
	}

	void Set(USHORT us) throw()
	{
		Clear();
		vt = VT_UI2;
		uiVal = us;
	}

	void Set(bool b) throw()
	{
		Clear();
		vt = VT_BOOL;
		boolVal = b ? ATL_VARIANT_TRUE : ATL_VARIANT_FALSE;
	}

	BOOL Set(LPCWSTR wsz) throw()
	{
		Clear();
		DWORD dwLen = (DWORD) (wcslen(wsz)+1)*2;
		pwszVal = (LPWSTR) malloc(dwLen);
		if (!pwszVal)
			return FALSE;
		memcpy(pwszVal, wsz, dwLen);
		vt = VT_LPWSTR;
		return TRUE;
	}

	BOOL Set(CLSID *pclsid) throw()
	{
		Clear();
		puuid = (CLSID *) malloc(sizeof(CLSID));
		if (!puuid)
			return FALSE;
		memcpy(puuid, pclsid, sizeof(CLSID));
		vt = VT_CLSID;
		return TRUE;
	}

	// other overrides for set

	void Clear() throw()
	{
		void *pvFree = NULL;

		switch (vt)
		{
		case VT_CLSID:
			pvFree = puuid;
			break;

		case VT_LPWSTR:
			pvFree = pwszVal;
			break;

		}
		if (pvFree)
			free(pvFree);
		vt = VT_NULL;
	}
};


// class CMQPropVariantRet
// Simple wrapper around an MQPROPVARIANT
// Notice that this class is only suitable
// for receiving property values from MSMQ
class CMQPropVariantRet : public MQPROPVARIANT
{
public:
	CMQPropVariantRet() throw()
	{
		vt = VT_NULL;
	}

	~CMQPropVariantRet() throw()
	{
		Clear();
	}

	void Clear() throw()
	{
		void *pvFree = NULL;

		switch (vt)
		{
		case VT_CLSID:
			pvFree = puuid;
			break;

		case VT_LPWSTR:
			pvFree = pwszVal;
			break;

		}
		if (pvFree)
			MQFreeMemory(pvFree);
		vt = VT_NULL;
	}
};


// class CMQPropertyRestriction
// a class that encapsulates an MSMQ property restriction
class CMQPropertyRestriction : public MQPROPERTYRESTRICTION
{
public:
	CMQPropertyRestriction() throw()
	{
		rel = PREQ;
		prop = 0;
		prval.vt = VT_NULL;
	}

	CMQPropertyRestriction(ULONG rel, PROPID propId, MQPROPVARIANT *pVal) throw()
	{
		Set(rel, propId, pVal);
	}

	void Set(ULONG ulRel, PROPID propId, MQPROPVARIANT *pVal) throw()
	{
		ATLASSERT(ulRel==PRLT || ulRel==PRLE || ulRel==PRGE || ulRel==PREQ || ulRel==PRNE);
		ATLASSERT(pVal);

		// todo: figure out how to free current!
		rel = ulRel;
		prop = propId;
		prval = *pVal;
		pVal->vt = VT_NULL;
	}
};


// class CMQRestriction
// a class that encapsulates an MSMQ restriction
class CMQRestriction : public MQRESTRICTION
{
protected:
	CAtlArray<CMQPropertyRestriction, CElementTraitsBase<CMQPropertyRestriction> > m_propRestrictions;

public:
	CMQRestriction() throw()
	{
		cRes = 0;
		paPropRes = NULL;
	}


	int Add(CMQPropertyRestriction &res) throw()
	{
		int nIndex = (int)m_propRestrictions.Add(res);
		if (nIndex < 0)
			return nIndex;
		cRes = (int)m_propRestrictions.GetCount();
		paPropRes = m_propRestrictions.GetData();
		return nIndex;
	}
};

// class CMQLocator
// a class the encapsulates the MSMQ Locator APIs
class CMQLocator
{
protected:
	HANDLE m_hEnum;

public:
	CMQLocator() throw()
	{
		m_hEnum = NULL;
	}
	
	~CMQLocator() throw()
	{
		if (m_hEnum)
			End();
	}

	HRESULT Begin(MQRESTRICTION *pRestriction, MQCOLUMNSET *pColumns, MQSORTSET *pSort=NULL) throw()
	{
		ATLASSERT(m_hEnum == NULL);
		ATLASSERT(pColumns != NULL);
		return MQLocateBegin(NULL, pRestriction, pColumns, pSort, &m_hEnum);
	}
	
	HRESULT Next(DWORD *pcProps, MQPROPVARIANT *aPropVar) throw()
	{
		ATLASSERT(m_hEnum != NULL);
		ATLASSERT(pcProps);
		ATLASSERT(aPropVar);
		return MQLocateNext(m_hEnum, pcProps, aPropVar);
	}

	HRESULT End() throw()
	{
		ATLASSERT(m_hEnum != NULL);
		HRESULT hr = MQLocateEnd(m_hEnum);
		m_hEnum = NULL;
		return hr;
	}
};
} // namespace ATL

#endif // __ATLMQ_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -