Article ID: 198686
Article Last Modified on 2/22/2007
Option Explicit
Private Sub Form_Load()
' Message Queuing can not send messages larger than 4MB. In order to
' send larger messages, we must break the large messages
' into chunks, send the chunks as individual messages, and
' reassemble them at the destination.
Dim chunksize As Long ' Holds a value to specify the maximum number of bytes per chunk.
chunksize = 4096000 ' Defines the max number of bytes per chunk (roughly 3MB in this case)
Dim qi As New MSMQQueueInfo ' Object to hold information about the destination queue.
Dim q As MSMQQueue ' The destination queue object.
Dim msg As New MSMQMessage ' The message that will be sent to the queue (a file chunk in this case).
ReDim buff(19) As Byte ' Byte array.
Dim strfilename As String ' File name of the file to be sent.
Dim Chunks As Long ' Number of total chunks.
Dim Semichunk As Long ' Size of the last chunk (which will be smaller than chunksize -- the remainder).
Dim i As Long ' Index for the loop to send complete chunks.
qi.PathName = ".\msmqxfer" ' Change this to your destination queue.
On Error GoTo ErrorCreateQueue
qi.Create ' Create the queue if it does not exist.
On Error GoTo 0
On Error GoTo ErrorOpenQueue
Set q = qi.Open(MQ_SEND_ACCESS, 0) ' Open the queue for send access.
On Error GoTo 0
strfilename = "c:\boot.ini" ' Change this to the file that you wish to send.
Open strfilename For Binary As #1 ' Open the file to be sent to the queue.
' Determine the number of chunks that will need to be sent and their sizes.
Chunks = LOF(1) \ chunksize - 1 ' Determine the total number of complete chunks that will need to be sent for this file.
Semichunk = LOF(1) Mod chunksize - 1 ' Determine the size of the last chunk of the file (the remainder).
buff(0) = Chunks + (IIf(Semichunk > 0, 1, 0)) ' buff(0) contains the number of total (complete + any remainder) chunks.
msg.CorrelationId = buff ' The message that is being sent will contain info regarding the total number of messages.
ReDim buff(chunksize) ' buff will now be used as a temp storage space for holding a message chunk before it is sent to the queue.
' Send as many chunks of chunksize as you can.
For i = 1 To Chunks
Get #1, , buff ' Load one chunk from the source file.
msg.Label = strfilename ' All messages containing chunks of this file will have the same label.
msg.AppSpecific = i ' i is included in the message so that the receiver will understand the order to use when reassembling chunks.
msg.Body = buff ' Load the chunk from the buffer into the message body.
msg.Send q ' Send this message to the queue.
Next
' Now send any leftover portion that was not large enough to fill a complete chunk.
ReDim buff(Semichunk) ' Resize the buffer to hold the last partial chunk.
Get #1, , buff ' Load the chunk from the source file.
msg.Label = strfilename ' All messages containing chunks of this file will have the same label.
msg.AppSpecific = i + 1 ' Setting the sequence number.
msg.Body = buff ' Load the chunk from the buffer into the message body.
msg.Send q ' Send the partial chunk.
' Done sending the file
q.Close ' Close the queue.
Close #1 ' Close the file.
MsgBox "Successfully sent file " & strfilename & ", " & (chunksize * Chunks) + Semichunk & " bytes."
Exit Sub
ErrorCreateQueue:
If Err.Number = -1072824315 Then
Resume Next
Else
MsgBox "Error while creating the queue! You may not have permission to create queues on this computer, or the queue could be owned by another user.\n" & Err.Number & " -- " & Err.Description
End If
Exit Sub
ErrorOpenQueue:
MsgBox "Error while opening the queue! The queue may be owned by another user.\r" & Err.Number & " -- " & Err.Description
Exit Sub
End Sub
Additional query words: kbDSupport
Keywords: kbhowto KB198686