Microsoft KB Archive/815809

= How to write to and read from Microsoft Message Queuing in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 815809

Article Last Modified on 11/8/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft Message Queue Server 1.0
 * Microsoft Windows 2000 Standard Edition
 * Microsoft Windows XP Professional
 * Microsoft Windows Server 2003, Standard Edition (32-bit x86)
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 815811.



For a Microsoft Visual Basic .NET version of this article, see 315698.

This article references the following Microsoft .NET Framework Class Library namespaces:
 * System::Messaging
 * System::Text
 * System::Collections

IN THIS TASK

 * SUMMARY
 * Requirements
 * Write to and Read from Message Queuing
 * Complete Code Listing (msmq.h)
 * Verify the Code
 * Troubleshooting
 * REFERENCES



SUMMARY
This article describes how to create a message and send the message to Microsoft Message Queuing in a Windows application. This article also describes how to read from a private queue and how to deserialize the message contents for display.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:One of the following operating systems with Message Queuing installed (Message Queuing is included with the operating systems as an option):
 * Microsoft Windows 2000 Professional or Microsoft Windows 2000 Server
 * Microsoft Windows XP Professional
 * Microsoft Windows Server 2003

This article assumes that you are familiar with the following topics:
 * Microsoft Message Queuing
 * Using tools from the command prompt

back to the top

Write to and Read from Message Queuing
The System.Messaging namespace in the .NET Framework has the classes that you must read from and write to Message Queuing. To create a small Windows application that mimics an online bill payment system, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Windows Form Application under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Form Application under Templates. Name the project MSMQ . In Solution Explorer, right-click References, and then click Add Reference.

Note In Solution Explorer, right-click MSMQ, click References, and then click Add New Reference. On the .NET tab, click System.Messaging.dll file from the list of .dll files. Click Select, and then click OK.</li> In Solution Explorer, double-click Form1.h(design).</li> Press the CTRL+ALT+X key combination to open the toolbox. Click the Windows Forms tab.</li> Add the following to the middle of Form1.h (design): <ul> 4 rows each of a Label and a Textbox (positioned to the right of each label).</li> Under the labels and text boxes, add two Button controls to Form1.h (design).</li></ul> </li> Right-click the controls, click Properties, and then set the Text property for the labels to the following (in order): <ul>  Pay To: </li>  Your Name: </li>  Amount: </li>  Due Date: </li></ul> </li> In the Properties dialog box, set the Text property of the button1 control to Send Payment, and then set the Text property of the button2 control to Process Payment .</li> This application works with a private queue that you must first create in the Computer Management console. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the desktop, right-click My Computer, and then click Manage.</li> Expand Services and Applications to find Message Queuing.

Note You may not find the Messaging Queuing, when it is not installed.</li> Expand Message Queuing, right-click Private Queues, point to New, and then click Private Queue.</li> <li>In the Queue name text box, type billpay, and then click OK.

Note Click to clear the Transactional check box. Leave the Computer Management console open for viewing messages.</li></ol> </li> <li> At the top of the code in the Form1.h file, add two USING NAMESPACE statements before the class declaration to include the additional classes that reside in the System::Messaging namespace and the System::Text namespace. (The StringBuilder class, a new .NET Framework class that it is best to use when you concatenate strings, uses the System.Text namespace.) using namespace System::Messaging; using namespace System::Text; </li> <li> Create a structure that contains variables to hold the data that defines a payment. To create the structure, add the following code above the declaration of the Form1 class:: __gc public struct Payment { System::String __gc* Payor; System::String __gc* Payee; int Amount; System::String __gc* DueDate; }; </li> <li>Add the code in the following steps to the Click event of button1: <ol style="list-style-type: lower-alpha;"> <li> Set the properties of the structure to values of the form elements as follows: Payment* myPayment = new Payment; myPayment->Payor = textBox1->Text; myPayment->Payee = textBox2->Text; myPayment->Amount = Convert::ToInt32(textBox3->Text); myPayment->DueDate = textBox4->Text; </li> <li> Create an instance of the Message class, and then set the Body property to the payment structure: System::Messaging::Message *msg = new System::Messaging::Message; msg->Body=myPayment; </li> <li> To send a message to Message Queuing, create an instance of the MessageQueue class and call the Send method that passes in the Message object. The MessageQueue class is the wrapper that manages the interaction with Message Queuing.

Notice the syntax for setting the path of the private queue that you created in the Computer Management console. Private queues take the following form:

\Private$\queuename

Local host computers are referenced with a dot or a period ( &quot;.&quot;) as follows: MessageQueue *msgQ =new MessageQueue(S&quot;.\\Private$\\billpay&quot;); msgQ->Send(msg);

The code now exists to send a message to Microsoft Message Queuing. The .NET Framework automatically serializes the message by using an XMLMessageFormatter object. This object is implicitly created when messages are sent. </li></ol> </li> <li>Add the following code to the Click event of the button2 control. The button2_Click event handler receives and processes the payment message that is sent in the button1 event handler. <ol style="list-style-type: lower-alpha;"> <li> The first line of code is the same as the line of code that is in the first event handler: MessageQueue *msgQ = new MessageQueue(S&quot;.\\Private$\\billpay&quot;); </li> <li> Create an array of types to pass to the XMLMessageFormatter class.

Note This class must be explicitly created when receiving messages. The constructor of the XMLMessageFormatter class takes either a string array of type names or, more preferably, a Type array of types: Payment *myPayment=new Payment; Object *o=new Object; System::Type *arrTypes[] = new System::Type* [2] ; arrTypes[0] = myPayment->GetType; arrTypes[1] = o->GetType; msgQ->Formatter = new XmlMessageFormatter(arrTypes); myPayment= static_cast<Payment*>(msgQ->Receive->Body); These types tell the XMLMessageFormatter how to deserialize the message. </li> <li> Messages are received by calling the Receive method. Access the Body property to read the message contents. The Body property returns an object. Therefore, the object is cast to the payment type to retrieve the contents in a usable form: StringBuilder *sb = new StringBuilder; sb->Append(S&quot;Payment paid to: &quot;); sb->Append(myPayment->Payor); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Paid by: &quot;); sb->Append(myPayment->Payee); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Amount: $&quot;); sb->Append(myPayment->Amount.ToString); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Due Date: &quot;); sb->Append(Convert::ToString(Convert::ToDateTime(myPayment->DueDate))); </li> <li> Create a message box to display the results: MessageBox::Show(sb->ToString, S&quot;Message Received!&quot;); </li></ol> </li></ol>

back to the top

Complete Code Listing (msmq.h)

 * 1) pragma once

using namespace System::Messaging; using namespace System::Text;

namespace MSMQ {   using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;

__gc public struct Payment {       System::String __gc* Payor; System::String __gc* Payee; int Amount; System::String __gc* DueDate; };   ///     /// Summary for Form1 ///   /// WARNING: If you change the name of this class, you must change the ///         'Resource File Name' property for the managed resource compiler tool ///         that is associated with all .resx files that this class depends on. Otherwise, ///         the designers cannot interact correctly with localized ///         resources that are associated with this form. ///    public __gc class Form1 : public System::Windows::Forms::Form {   public: Form1(void) {           InitializeComponent; }   protected: void Dispose(Boolean disposing) {           if (disposing && components) {               components->Dispose; }           __super::Dispose(disposing); }   private: System::Windows::Forms::Label *  label1; private: System::Windows::Forms::Label * label2; private: System::Windows::Forms::Label * label3; private: System::Windows::Forms::Label * label4; private: System::Windows::Forms::TextBox * textBox1; private: System::Windows::Forms::TextBox * textBox2; private: System::Windows::Forms::TextBox * textBox3; private: System::Windows::Forms::TextBox * textBox4; private: System::Windows::Forms::Button * button1; private: System::Windows::Forms::Button * button2;

private: ///        /// Required designer variable. ///        System::ComponentModel::Container * components;

///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        void InitializeComponent(void) {           this->label1 = new System::Windows::Forms::Label; this->label2 = new System::Windows::Forms::Label; this->label3 = new System::Windows::Forms::Label; this->label4 = new System::Windows::Forms::Label; this->textBox1 = new System::Windows::Forms::TextBox; this->textBox2 = new System::Windows::Forms::TextBox; this->textBox3 = new System::Windows::Forms::TextBox; this->textBox4 = new System::Windows::Forms::TextBox; this->button1 = new System::Windows::Forms::Button; this->button2 = new System::Windows::Forms::Button; this->SuspendLayout; //            // label1 //            this->label1->Location = System::Drawing::Point(16, 32); this->label1->Name = S&quot;label1&quot;; this->label1->TabIndex = 0; this->label1->Text = S&quot;Pay To:&quot;; //            // label2 //            this->label2->Location = System::Drawing::Point(16, 72); this->label2->Name = S&quot;label2&quot;; this->label2->TabIndex = 1; this->label2->Text = S&quot;Your Name:&quot;; //            // label3 //            this->label3->Location = System::Drawing::Point(16, 112); this->label3->Name = S&quot;label3&quot;; this->label3->TabIndex = 2; this->label3->Text = S&quot;Amount:&quot;; //            // label4 //            this->label4->Location = System::Drawing::Point(16, 152); this->label4->Name = S&quot;label4&quot;; this->label4->TabIndex = 3; this->label4->Text = S&quot;Due Date:&quot;; //            // textBox1 //            this->textBox1->Location = System::Drawing::Point(136, 32); this->textBox1->Name = S&quot;textBox1&quot;; this->textBox1->TabIndex = 4; this->textBox1->Text = S&quot;ABC&quot;; //            // textBox2 //            this->textBox2->Location = System::Drawing::Point(136, 72); this->textBox2->Name = S&quot;textBox2&quot;; this->textBox2->TabIndex = 5; this->textBox2->Text = S&quot;DEF&quot;; //            // textBox3 //            this->textBox3->Location = System::Drawing::Point(136, 112); this->textBox3->Name = S&quot;textBox3&quot;; this->textBox3->TabIndex = 6; this->textBox3->Text = S&quot;12&quot;; //            // textBox4 //            this->textBox4->Location = System::Drawing::Point(136, 152); this->textBox4->Name = S&quot;textBox4&quot;; this->textBox4->TabIndex = 7; this->textBox4->Text = S&quot;9/9/2003&quot;; //            // button1 //            this->button1->Location = System::Drawing::Point(24, 208); this->button1->Name = S&quot;button1&quot;; this->button1->Size = System::Drawing::Size(88, 23); this->button1->TabIndex = 8; this->button1->Text = S&quot;Send Payment&quot;; this->button1->Click += new System::EventHandler(this, button1_Click); //            // button2 //            this->button2->Location = System::Drawing::Point(152, 208); this->button2->Name = S&quot;button2&quot;; this->button2->Size = System::Drawing::Size(104, 23); this->button2->TabIndex = 9; this->button2->Text = S&quot;Process Payment&quot;; this->button2->Click += new System::EventHandler(this, button2_Click); //            // Form1 //            this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->textBox4); this->Controls->Add(this->textBox3); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->Controls->Add(this->label4); this->Controls->Add(this->label3); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Name = S&quot;Form1&quot;; this->Text = S&quot;Form1&quot;; this->ResumeLayout(false);

}  private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)             { try {                   Payment* myPayment = new Payment; myPayment->Payor = textBox1->Text; myPayment->Payee = textBox2->Text; myPayment->Amount = Convert::ToInt32(textBox3->Text); myPayment->DueDate = textBox4->Text;

System::Messaging::Message *msg = new System::Messaging::Message; msg->Body=myPayment;

MessageQueue *msgQ =new MessageQueue(S&quot;.\\Private$\\billpay&quot;); msgQ->Send(msg); }                catch(Exception *e) {                    MessageBox::Show(e->Message); }            }

private: System::Void button2_Click(System::Object * sender, System::EventArgs *  e)         { try {               MessageQueue *msgQ = new MessageQueue(S&quot;.\\Private$\\billpay&quot;);

Payment *myPayment=new Payment; Object *o=new Object; System::Type *arrTypes[] = new System::Type* [2] ; arrTypes[0] = myPayment->GetType; arrTypes[1] = o->GetType; msgQ->Formatter = new XmlMessageFormatter(arrTypes); myPayment= static_cast<Payment*>(msgQ->Receive->Body);

StringBuilder *sb = new StringBuilder; sb->Append(S&quot;Payment paid to: &quot;); sb->Append(myPayment->Payor); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Paid by: &quot;); sb->Append(myPayment->Payee); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Amount: $&quot;); sb->Append(myPayment->Amount.ToString); sb->Append(S&quot;\n&quot;); sb->Append(S&quot;Due Date: &quot;); sb->Append(Convert::ToString(Convert::ToDateTime(myPayment->DueDate)));

MessageBox::Show(sb->ToString, S&quot;Message Received!&quot;); }            catch(Exception *e) {                MessageBox::Show(e->Message); }        }    }; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

back to the top

Verify the Code

 * 1) On the Debug menu, click Start.
 * 2) Type values in each text box, and then click Send Payment.
 * 3) Return to the Computer Management console. In Private Queues under billpay, click the Queue messages folder, and then verify that Message Queuing received a message. An envelope icon indicates a new message.
 * 4) Right-click the message, click Properties, and then click the Body tab. You notice the payment message.

Note The contents of the payment message are serialized as XML.
 * 1) Return to the bill payment Windows application, and then click Process Payment. You receive a message box that confirms the receipt of a message and that displays the message.

back to the top

Troubleshooting

 * The lack of a private queue is generally an issue only on computers that are running Windows 2000 Professional or Windows XP Professional. Windows 2000 Server and Windows Server 2003 permit the use of the public queue.
 * Passing the correct arguments to XMLMessageFormatter can be tricky. In this example, exceptions are thrown if either the object or the Payment Types are not included in the Type array that is passed to the constructor.

back to the top

<div class="references_section">