Thread: Peeking at MSMQ queues

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    98

    Peeking at MSMQ queues

    Hi. I'm trying out the MSMQ API using VC++ v6 on NT4. I have written the following code, and I seem to be managing to both open and peek at the queue, but I don't get intelligible responses when trying to display the message label and body...

    Code:
    void CReadMSMQDlg::OnRead() 
    {
    	HRESULT hr = S_OK;
    	IMSMQQueuePtr pQueue;			// The pointer to the queue (contents level)
    	CString msg;
    
    	try
    	{
    		IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo"); // The pointer to the queue itself
    
    		pInfo->FormatName = "DIRECT=TCP:IPAddress\\QName";
    
    		// Open the queue for receive access
    		pQueue = pInfo->Open( MQ_PEEK_ACCESS, MQ_DENY_NONE );
    
    		IMSMQMessagePtr pMsg("MSMQ.MSMQMessage");
    
    		_variant_t vtTimeOut((long)1000);  // 1 second until receive time-out
    		_variant_t bvtWantBody((bool)true); // Set to retrieve message body
    
    		// Read all the messages in queue until 1 second time-out
    		for( ;; )
    		{
    			pMsg = pQueue->Peek( &vtMissing, &bvtWantBody, &vtTimeOut );
    
    			if( pMsg == NULL )
    			{
    				msg.Format( "No messages on %s", pInfo->FormatName );
    				MessageBox( msg, NULL, MB_OK );
    				pQueue->Close();
    				pQueue = NULL;
    				break;
    			}
    
    			// Display the message label and body
    			msg.Format( "%s", pMsg->Label );
    			//msg.Format( "%s", pMsg->Body );
    
    			MessageBox( "DEBUG Label", msg, MB_OK );
    		}
    	}
    	catch( const _com_error& comerr )
    	{
    		hr = comerr.Error();
    		msg.Format( "Error Code = 0x%X", hr );
    		MessageBox( msg, "COM ERR", MB_OK );
    		
    		msg.Format( "Error Description = %s", (WCHAR *)comerr.Description() );
    		MessageBox( msg, "COM ERR", MB_OK );		
    
    		if( pQueue )
    			pQueue->Close();
    	}
    }
    From looking on MSDN, Label is supposed to be a string, but it doesn't display the message label I'd expect, and is just showing rubbish really. Also, MSDN says that the body variable is a 'variant', does anybody know how too handle this data type?

    Thanks.
    There is no such thing as a humble opinion

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    A COM string with an #import generated framework is represented as a _bstr_t class.
    Code:
    msg.Format( "%s", pMsg->Label );
    Passing a class or structure such as a _bstr_t to a variable args function is an unreliable operation. You need to use one of the _bstr_t cast operators to get a simple(or POD) type.
    Code:
    msg.Format( "%s", (char*) pMsg->Label );
    // or
    msg.Format( "%S", (wchar_t*) pMsg->Label );
    Similarly, a _variant_t can be cast to a _bstr_t which can then be cast to char* or wchar_t*:
    Code:
    msg.Format( "%s", (char*) (_bstr_t) pMsg->Body );
    C++ style casts would be stylistically preferable.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    All my problems solved.

    Thankyou.
    There is no such thing as a humble opinion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. posix queues
    By chaitra1604 in forum Linux Programming
    Replies: 1
    Last Post: 03-13-2009, 03:35 PM
  2. MSMQ programming
    By Opariti in forum Windows Programming
    Replies: 2
    Last Post: 10-11-2008, 12:20 AM
  3. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-18-2004, 11:19 PM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 08:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM