HOWTO: Send a Binary File as MSMQ Message Using Visual Basic

ID: Q186188


The information in this article applies to:
  • Microsoft Message Queue Server version 1.0


SUMMARY

The Microsoft Message Queue Server (MSMQ) Message body supports byte arrays so you can assign a DLL or any other file to a message body by first converting the file contents into a byte array and then sending it as a message. This article contains a code example that shows how to accomplish this in Visual Basic (VB).


MORE INFORMATION

The following Visual Basic code opens a binary file, reads in its contents into a byte array, and then assigns that byte array to a message body.

Sample Code


   Dim myQueueInfo As New MSMQQueueInfo
   Dim myMessage As New MSMQMessage
   Dim myNewMessage As MSMQMessage
   Dim myQueue As MSMQQueue

   Dim barray() As Byte
   Dim barray2() As Byte
   Dim strFileName As String
   Dim strFileNameDest As String
   Dim i As Long
   Dim v As Variant

   strFileName = CStr(InputBox("src filename: "))
   Open strFileName For Binary As #1
   ReDim barray(LOF(1))            ' realloc to filesize
   '
   ' Copy the file into the byte array.
   '
   i = 0
   Do While Not EOF(1)
       Get #1, , barray(i)
       i = i + 1
   Loop
   Close #1
   myMessage.Body = barray
   '
   ' sending
     On Error Resume Next
     myQueueInfo.PathName = ".\FileQ"
     myQueueInfo.Label = "Test Queue"
     myQueueInfo.Create
    ' Open for send
     Set myQueue = myQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
     myMessage.Label = "Message"
     myMessage.Send myQueue
     MsgBox "File is sent"  ' can verify in Explorer
     myQueue.Close

   ' Open for receive.
     Set myQueue = myQueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
     Set myNewMessage = myQueue.Receive(wantBody:=True, _
                                     ReceiveTimeout:=1000)
   barray2 = myNewMessage.Body
   strFileNameDest = CStr(InputBox("destination filename: "))
   Open strFileNameDest For Binary As #1
   '
   ' Write the message buffer into the destination file.
   '
   For i = LBound(barray2) To UBound(barray2)
       Put #1, , barray2(i)
   Next 


REFERENCES

MSMQ SDK Help

Additional query words: kbDSupport kbdse kbMSMQ100


Keywords          : 
Version           : winnt:1.0
Platform          : winnt 
Issue type        : kbhowto 


Last Reviewed: October 6, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.