Library tutorials & articles

Get the Message - MSMQ

Sending a message

Now we start to get into the details. Before you can use the code that we will show in the rest of this article, you must include a reference to "Microsoft Message Queue Object Library" in your project. We will start by showing you how to send a message. The code shown below illustrates how to post a message to a queue.

Private Sub PostMessage()

Dim QI as New MSMQQueueInfo
Dim RequestQ As MSMQQueue
Dim msg As New MSMQMessage

QI.PathName = "MyServer\QueueName"
Set RequestQ = QI.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)

Msg.Label = "Sales Order 1"
Msg.Body = "Customer=Bruce;Product=Pokemon;" & _
"Quantity=1000000"
msg.Send RequestQ

RequestQ.Close

End Sub


First of all, lets' discuss the objects that are used in this code snippet. The MSMQQueueInfo object represents the entire message queue environment. By setting the path name, you are indicating the server on which the queue resides ('MyServer') and the name of the queue ('QueueName'). It is quite possible to have more than one queue on a server. And when you define a message queue, you should try to make the names as meaningful as possible to help with the development process as your use for message queues increases.

By using the Open method, you create an MSMQQueue object that represents the queue specified in PathName. The parameters for Open include the access level for the queue (typically either send or receive) and a locking value that determines if others can have the queue open at the same time (typically, you don't need to open a queue for exclusive access).

The MSMQMessage object represents the message itself. Again, there are a number of properties available, but the most frequently used ones are Label and Body. Label associates a text string with the message. This string gets displayed if you look at the queue details through the MSMQ Explorer. The Body property contains the text that makes up the message. As you will see shortly, this is a simplistic view of the message body, but it will do for now.
Finally, the Send method on the MSMQMessage object is used to post the message to a queue. The MSMQQueue object that is to receive the message is passed as a parameter. At this point, the message has been placed in the specified queue.

Comments

  1. 29 Jul 2008 at 10:52

    I am using MSMQ-3 on my local machine. I am using enableNotification method to notify me when a message comes in the queue. But i am getting error message while using enableNotification. The message comes is "Access is denied" (error no. -1072824283). I am getting the message in the following line

     Dim evnt As MSMQ.MSMQEvent
                evnt = New MSMQ.MSMQEvent
                vMyQSend.EnableNotification(evnt)

     

    Please anybody help me.

  2. 08 Dec 2004 at 19:26

    Ummm, there's a lot of other stuff around the code that I have (which was in a VB.DLL used within ASP pages), but I tried to pull out the working code that would get to the essence of what you need. Essentially, this code opens a queue, creates a message on the queue, sets the body of the message, then sends the message. We developed another module which gets back the Id from the message sent so we use that to track the status of the send. Not sure if it works, but it's pretty close ... maybe I left something off, but this is code that has been working for the last several years ... having generated hundreds of thousands of messages. If you have any questions, or want some ideas on some more complicated stuff we put around it for sending and checking results, let me know at henry @ dcbiking . com


    >> Henry


       Dim oDOM As MSXML2.DOMDocument          'Holds XML to be sent
       Dim oQueueInfo As MSMQ.MSMQQueueInfo    'Used to return the queue info for formatting message props.
       Dim oQueue As MSMQ.MSMQQueue            'Queue
       Dim oMsg As MSMQ.MSMQMessage            'Message to be placed on queue
       
       Set oQueueInfo = CreateObject("MSMQ.MSMQQueueInfo")
       oQueueInfo.Pathname = "MACHINENAME\PATHNAMEBLAHBLAH"
       Set oQueue = oQueueInfo.Open(MSMQ.MQSENDACCESS, MSMQ.MQDENYNONE)


       Set oDOM = Server.CreateObject("MSXML2.DOMDocument")
       oDOM.loadXML = "<call><sendTo>user@user.com</sendTo><method>Sum</method><parms><parm>1</parm><parm>2</parm></parms></call>"
       
       ' Create message
       
       If oQueue.IsOpen Then
           Set oMsg = CreateObject("MSMQ.MSMQMessage")
           oMsg.Body = oDOM.xml
       Else
           Err.Raise 203, "sendRequest", "MSMQ queue could not be opened to send the message."
       End If
       
       ' Send message
       'Message sending will accommodate both transactional and non-transactional queues.
       'If the queue is transactional, send the message with the single message option.
       'If the queue is not transactional, send the message as non-transactional.
       If oQueueInfo.IsTransactional Then
           oMsg.Send oQueue, MQSINGLEMESSAGE
       Else
           oMsg.Send oQueue, MQNOTRANSACTION
       End If
           
       'Close queue; Null all objects
       oQueue.Close
       Set oMsg = Nothing
       Set oQueue = Nothing
       Set oQueueInfo = Nothing
       Set oDOM = Nothing

  3. 08 Dec 2004 at 06:33

    Hi,


    Could you please let me know, how to send an xml file as an object to a message queue? If possible can you provide me any code snippet for this as I'm novice to this technology.


    Also, I want to receive this message as an XML file only.


    Thanks in advance.
    -CyberLotus

  4. 21 Sep 2004 at 00:23

    Hi,



    I have similar error "The operation is not supported on a workgoup installation of computer" while testing MSMQ program. I am using VB6 to write the MSMQ program. Do you have any advise on how can I overcome it?


    I also have problem determining the MSMQ server name and Queue name. Any idea where can I find these info. I have installed MSMQ and added reference to MSMQ 2.0 Object Library.


    The syntax which you have posted seems to be for .NET version. Thanks in advance.


    Regards
    Thongfam

  5. 25 May 2004 at 16:05

    I need to Know hoy many messages are in a MSMQ Queue using API, WMI or OCX component using Visual Basic
    Any Idea?


    Gabriel

  6. 13 Apr 2004 at 11:13
    You need to use this pathname:
    Dim q As New Messaging.MessageQueue("FormatNameIRECT=OS:RMPOSC-714\private$\testq")

    A good example on how to get the Queues real path would be this

    For Each queue As Messaging.MessageQueue In Messaging.MessageQueue.GetPrivateQueuesByMachine("RMPOSC-714")
               messagebox.show(queue.Path)
    Next
  7. 11 Mar 2004 at 16:22

    I have installed MSMQ on 2 Win2000 machines and have written a simple program to send a message from one to the other. I can successfully send a message from VB to my local machine with a pathname of ".\Private$\testq". However, when I attempt to cross the network to a known computer called RMPOSC-714 it fails the the error message below. This time I am setting pathname to "RMPOSC-714\testq".


    The error is: -1072824214: The operation is not supported for a WORKGROUP installation.


    Both PCs are on the network and in the domain.


    What am I missing?

  8. 07 Jan 2004 at 22:17

    OK I've got the windows forms idea down. How can I send and receive from an ASP page. What I would like to be able to do is the following:


    1. A user modifies data.
    2. post the user change to the queue.
    3. Have a listener pick up and process the user change.
    4. Post to the queue a sucess.
    5. return that sucuss to the user.


    I have a problem with sept 2 and how to impliment.


    Step 5 I have no idea on how to do this. It seems that I would need an additional listener that would pick up the return sucess message and would know what user to return it it. I am thinking that I will need some type of block on the ASP page that will wait until it receives an return....but how??


  9. 08 Jul 2003 at 09:52

    Best thing to do is byte[] .
    Putting it into a string : look on your networksniffer : a lot of formatting data
    (20h) gets included ! No ideal when the network is on heavy duty .

  10. 11 Jun 2003 at 05:55

    using SYSTEM.messaging, one could use the Body.bodystream to pump a filestream into the body of the message . Is it still available usder MSMQ API ? I cannot find it .
    Furthermore, the explanation given below is very valuable for me . I have still one question, I do have to get rid of the unicode property : I cannot effort doubling the data on small radiolinks . In order to compare performance of MSMQ3.0 with other products (TIBCO RendezVous), I have to get UTB8 , ASCII ...
    But the data coming from the layers above me is always XML-document . How can I accomplish ?


    Perhaps bytestream ? But then again : no streamproperty for the messageBody ...

  11. 03 Jun 2003 at 02:27

    Documentation states that anything could be referred as the body of the MSMQMessage , as f.ex. an XML document . Would it be possible to give me a glimp of how you reference this object to enpack it in the body ?
    I'm having problems with it . Thnks in advance .

  12. 15 Apr 2003 at 16:46

    We have an app working this way -
       Web/MSMQ server creates a message packet in XML/DB2 format, wraps it using base64 algorithm and keeps it in outboundq of the web/msmq server. A trigger (MSMQ trigger) fires which will post (HTTPS) this data to supplier queues in a server around our premises which will unwrap it and insert into our AS/400 (using HitODBC). [We use this mechanism since a third party is hosting our server]
       CRON services working on our local server picks data from the AS/400 and will send across to our web/msmq server using the same mechanism.


    -Binoy

  13. 01 Apr 2003 at 11:47

    1) Set the Message's Body to the DOM's xml property
             oMsg.Body = oDOM.xml
    2) Send the message
    3) After receiving/peek the message, loadXML from the oMsg.Body
             oDOM.loadXML oMsg.Body


    NOTE: The message body will be stored as Unicode, 2 bytes per character. So, if the msg is limited to 4MB in size, then effectively only 2MB of text (the oDOM.xml property) can be sent.


    >> Henry


    Quote:
    [1]Posted by vg on 14 Sep 2002 04:32 PM[/1]
    successfully sent as I could see the xml text in the body


  14. 16 Mar 2003 at 13:14
    Is there a similar technology like in Java sub/sub?
  15. 26 Feb 2003 at 16:52

    If I knew that the body of the message was an XML document, I would receive the message as a string and then use the LoadXML method of the DOMDocument object to load it into the appropriate structure.

  16. 26 Feb 2003 at 16:45

    Your problem can be solved using datasets (.net).  Check out this page from MSDN, "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetlpMSDN.asp?frame=true", I think it will help you out with your problem as long as you are using .NET.

  17. 18 Oct 2002 at 12:33

    The required environment is MSMQ Server installation on Windows Server machine, that's it. The advantage of this technology is to give a guarnateed delivery to the client even if the computer fail or get shut down because of some reason. The message keeps on sitting in the queue unless retreived successfully by the client, you can set various options like recoverable messages, acknowledgements etc. overall cool technology. Very easy to use with Visual Basic 6.0.


    Sandeep

  18. 18 Oct 2002 at 12:29

    Do you have MSMQ Client installed on your machine? You need either MSMQ Client or Server on your machine to get this going, hope you would have set the reference to Microsoft MSMQ 2.0. Also you can connect to only a Public Queue from a client machine.


    Good Luck
    Sandeep

  19. 01 Oct 2002 at 01:17

    Hi,
      I have tried with u r example.I have got Microsoft Message Queue 2.0 object library and i am getting an error "Active-x Component Can't Create Object" on executing "QI.PathName = "MyServer\QueueName"".What shall i do??


    Bye
     

  20. 01 Oct 2002 at 01:02

    Hi,
      I would like to know what can be achieved with MSMQ and its required environment.Can u pls help me in this regard??


    Bye
    Prakash

  21. 27 Sep 2002 at 11:53

    I am having a similar problem. Did you get any response from other people yet? I am quite interested.


    Thanks.

  22. 14 Sep 2002 at 16:32

    Hi, I am developing an application using MSMQ. I tried sending an xml file as an object.  It was successfully sent as I could see the xml text in the body of the message in the queue. However I am having problem in reading the body(Which has the xml file) once I peek or receive the message through VB code. I do not know what should be the datatype of the variable of the body at the receiving end. Any help or suggestions please?


    Thanks in advance,
    - VG

  23. 02 Aug 2002 at 11:24

    Hi,


    Am trying to develop a web based application interacting with AS400. Would you please let me know how do I interact with DB2 on AS400 from my VB DLL/ ASP application?


    Thank you

  24. 17 Jul 2002 at 03:24

    I have developed an MSMQ application that reads a file from the client end and sends it across onto the MSMQ server .The issue is that if the file size(bytes) is more than 2MB i am splitting them and sending it across.


    My Question is
    1) On the server end how do i collate these files and process them and send it back to the same client from where the splitted files were sent


    2) On the client how do collate the files back and put it in a single file after the files processed are sent from the server.


    Please help me out on the above lines as i am getting stuck on the same.
    Also wanted to know if we can also send files other than text files.If so how???


    Regards
    Ganesh

  25. 30 May 2002 at 14:05

    Hello sir,
    Could you please help me with following question:


           Can private queues running on Windows 2000 Professional exchange messages OR is     it   necessary to have a MSMQ Server in the middle?



    Thank you
    Marina


  26. 13 May 2002 at 03:23

    Hello,Sir
    My ultimate aim is that to provide User to save my application/form in as it is format
    I've developed a project which i want it to be used by user as a software,which enables any user to
    download on to their hard-drive whether they have a VB or not.My question is  that my application which
    i'd made to be saved on drive ( whole form/app. including backgroung color,textbx color....) It's exe.file
    o.k now when i write any input or fill the content in my text-box it displays in plain format only &
    i don't want this to happen;what i require is my exe.file should be saved as it is as i 've made. For
    example you make a software in Vb regarding your personal diet chart withbackground-col, color txt-bx,col-labels,col-combobx
    with picture bx,command button......fine then you convert this to exe.file;now if you want to launch
    this software how can any user download this with all the details including the above mention list.Only
    the thing where i've got stuck is saving my software/application;The important thing after converting
    my application to exe.file it's in a same format with all those colors & other properties;but when i
    write something in the textbx or combo-bx its displaying in plain format.This is where i need to explore
    more & i expect that you'll provide me all the needed code function  furthur required.Hope you'll guide
    me.



    actually i'd made a project regarding my teacher's profile where i've 1-form containing :-


    This Form has background color which insert from properties->picture;also
    labels,textbox,combo box are all in different color( properties->background).
    Now when i save this form in this format which is stated above but when its saved
    it is in plain text without picture.



    Label                                       Textbox      
    1)Teachers Name                  Text1
    2)Subjects                               Text2
    3)Email-id                             Combo-box                    & one Picture box


    This above form also has 2-command buttons ( Saveas,Preview)
    I've all the codings for all of this but i want to save my form in as it is format including colors & picture.


    Below is the coding:


    Private Sub Command1_Click() 'Preview button
    Form2.Show
    End Sub


    Private Sub dir1_change()
    File1.Path = Dir1.Path
    End Sub


    Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
    End Sub


    Private Sub File1_Click()
    Dim sPath As String
    If Right$(Dir1.Path, 1) = "\" Then
      sPath = Dir1.Path & File1.FileName
    Else
      sPath = Dir1.Path & "\" & File1.FileName
    End If
    Set Picture1.Picture = LoadPicture(sPath)
    End Sub


    Private Sub Form_Load()
    File1.Pattern = ".bmp;.gif;*.jpg"
    End Sub


    Private Sub Form_Load()
    Form2.Picture1.Picture = Form1.Picture1.Picture
    Form2.Text1.Text = Form1.Text1.Text
    Form2.Text2.Text = Form1.Text2.Text
    Form2.Combo1.Text = Form1.Combo1.Text
    Form2.Combo2.Text = Form1.Combo2.Text
    Form2.Combo3.Text = Form1.Combo3.Text


    End Sub




    Option Explicit


    Private Sub Command2_Click()
    Dim Filter As String
    Dim strFileName As String 'String of file to open
    Dim strtext As String 'Contents of file
    'Dim strFilter As String 'Common Dialog filter string
    Dim strBuffer As String 'String buffer variable
    Dim FileHandle% 'Variable to hold file handle


    On Error GoTo SaveAsError


    Filter = "all files (.)|.|"
    Filter = Filter + "HTML Files(.htm)|.htm|"
    Filter = Filter + "Text Files(.txt)|.txt|"
    Filter = Filter + "Batch Files (.bat)|.bat|"
    Filter = Filter + "Document Files (.Doc)|.Doc|"
    CommonDialog1.Filter = Filter
    CommonDialog1.ShowSave
    CommonDialog1.FilterIndex = 2
    CommonDialog1.Action = 2


    If CommonDialog1.FileName <> "" Then
    'If it is not blank, open the file
    strFileName = CommonDialog1.FileName
    strtext = Text1.Text
    FileHandle% = FreeFile
    Open strFileName For Output As #FileHandle%
    MousePointer = vbHourglass
    Print #FileHandle%, strtext
    MousePointer = vbDefault
    Close #FileHandle%
    End If
    Exit Sub


    SaveAsError:
    MsgBox "you canceled the dialog box"
    Exit Sub
    End Sub



    Regards,
    jfuture.




  27. 13 May 2002 at 03:20

    Hello,Sir
    actually i'd made a project regarding my teacher's profile where i've 1-form containing :-


    This Form has background color which insert from properties->picture;also
    labels,textbox,combo box are all in different color( properties->background).
    Now when i save this form in this format which is stated above but when its saved
    it is in plain text without picture.



    Label                                       Textbox      
    1)Teachers Name                  Text1
    2)Subjects                               Text2
    3)Email-id                             Combo-box                    & one Picture box


    This above form also has 2-command buttons ( Saveas,Preview)
    I've all the codings for all of this but i want to save my form in as it is format including colors & picture.


    Below is the coding:


    Private Sub Command1_Click() 'Preview button
    Form2.Show
    End Sub


    Private Sub dir1_change()
    File1.Path = Dir1.Path
    End Sub


    Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
    End Sub


    Private Sub File1_Click()
    Dim sPath As String
    If Right$(Dir1.Path, 1) = "\" Then
      sPath = Dir1.Path & File1.FileName
    Else
      sPath = Dir1.Path & "\" & File1.FileName
    End If
    Set Picture1.Picture = LoadPicture(sPath)
    End Sub


    Private Sub Form_Load()
    File1.Pattern = ".bmp;.gif;*.jpg"
    End Sub


    Private Sub Form_Load()
    Form2.Picture1.Picture = Form1.Picture1.Picture
    Form2.Text1.Text = Form1.Text1.Text
    Form2.Text2.Text = Form1.Text2.Text
    Form2.Combo1.Text = Form1.Combo1.Text
    Form2.Combo2.Text = Form1.Combo2.Text
    Form2.Combo3.Text = Form1.Combo3.Text


    End Sub




    Option Explicit


    Private Sub Command2_Click()
    Dim Filter As String
    Dim strFileName As String 'String of file to open
    Dim strtext As String 'Contents of file
    'Dim strFilter As String 'Common Dialog filter string
    Dim strBuffer As String 'String buffer variable
    Dim FileHandle% 'Variable to hold file handle


    On Error GoTo SaveAsError


    Filter = "all files (.)|.|"
    Filter = Filter + "HTML Files(.htm)|.htm|"
    Filter = Filter + "Text Files(.txt)|.txt|"
    Filter = Filter + "Batch Files (.bat)|.bat|"
    Filter = Filter + "Document Files (.Doc)|.Doc|"
    CommonDialog1.Filter = Filter
    CommonDialog1.ShowSave
    CommonDialog1.FilterIndex = 2
    CommonDialog1.Action = 2


    If CommonDialog1.FileName <> "" Then
    'If it is not blank, open the file
    strFileName = CommonDialog1.FileName
    strtext = Text1.Text
    FileHandle% = FreeFile
    Open strFileName For Output As #FileHandle%
    MousePointer = vbHourglass
    Print #FileHandle%, strtext
    MousePointer = vbDefault
    Close #FileHandle%
    End If
    Exit Sub


    SaveAsError:
    MsgBox "you canceled the dialog box"
    Exit Sub
    End Sub


    My ultimate aim is that to provide User to save my application/form in as it is format
    I've developed a project which i want it to be used by user as a software,which enables any user to
    download on to their hard-drive whether they have a VB or not.My question is  that my application which
    i'd made to be saved on drive ( whole form/app. including backgroung color,textbx color....) It's exe.file
    o.k now when i write any input or fill the content in my text-box it displays in plain format only &
    i don't want this to happen;what i require is my exe.file should be saved as it is as i 've made. For
    example you make a software in Vb regarding your personal diet chart withbackground-col, color txt-bx,col-labels,col-combobx
    with picture bx,command button......fine then you convert this to exe.file;now if you want to launch
    this software how can any user download this with all the details including the above mention list.Only
    the thing where i've got stuck is saving my software/application;The important thing after converting
    my application to exe.file it's in a same format with all those colors & other properties;but when i
    write something in the textbx or combo-bx its displaying in plain format.This is where i need to explore
    more & i expect that you'll provide me all the needed code function  furthur required.Hope you'll guide
    me.


    Regards,
    jfuture.




  28. 01 Jan 1999 at 00:00

    This thread is for discussions of Get the Message - MSMQ.

Leave a comment

Sign in or Join us (it's free).

Bruce Johnson I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web site...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback