다음을 통해 공유


Visual Basic Code Example: Creating a Queue

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a private function that creates a public or private queue based on a given queue path name. For creating transactional queues, see Visual Basic Code Example: Creating a Transactional Queue.

Note

Public queues cannot be created if there is no connection to the directory service. This restriction applies to dependent client computers, independent client computers that are working offline, and Message Queuing servers that have routing services enabled (for MSMQ 1.0, these servers are referred to as FRS servers).

The following procedure shows how the private function creates the queue.

To create a queue

  1. Declare the MSMQQueueInfo variable for the queue.

  2. Create a new MSMQQueueInfo object and assign it to the variable.

  3. Set the MSMQQueueInfo.PathName property to the UNC or DNS path name of the queue.

    The following are examples of UNC path names that can be used to create public and private queues for the local computer.

    .\testqueue                  ' Public queue path name.  
    .\PRIVATE$\testqueue         ' Private queue path name.  
    

Note

For Message Queuing dependent clients, the local computer is the client's supporting server.

  1. Set optional queue properties. This example also specifies the label of the queue using MSMQQueueInfo.Label.

  2. Call MSMQQueueInfo.Create to create the queue.

Example

The following code example contains no version-specific Message Queuing calls.

Sub CreateMyQueue( _  
                  strPathname As String _  
                  )  
  Dim qinfo As MSMQQueueInfo  
  
  Set qinfo = New MSMQQueueInfo  
  qinfo.PathName = strPathname  
  qinfo.Label = "TestQueue"  
  
  On Error GoTo ErrorHandler  
  qinfo.Create  
  
  MsgBox "Created Queue."  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
          + Chr(13) + Err.Description  
End Sub