Hi, can anyone help figure out why a COM call is failing. The method I am
calling is to a Microsoft Message Queuing component (MSMQ). The details of
the Receive() method are shown later on. If my Python script calls:

ReplyMessage = ReplyQueue.Receive() - it works fine
ReplyMessage = ReplyQueue.Receive(0) - is ok as well
ReplyMessage = ReplyQueue.Receive(0,0) - no problem either

The method call fails when I try to pass in a WantBody flag, ie,
Receive(0,0,1). I've tried several alternatives, such as
ReplyQueue.Receive(0,0,-1) (which corresponds to VARIANT_TRUE) but to no
avail. If I run the ReplyQueue.Receive(0,0,1) line of code in PythonWin the
app crashes :(

I am running ActivePython v2.0 build 202 on Windows 2000 SP1. The MSMQ 1.0
type library is MakePy imported. The full Python script is listed at the
bottom if anyone wants to try and reproduce the problem. One note, the code
will wait for a reply which will never arrive since you won't have the
server running, but it should be enough to spot the issue. You will also
need to create a private queue called Authorisation on your local computer,
which can be done through MSMQ explorer.


MSMQQueue.Receive()

The Receive method of the MSMQQueue object retrieves the first message in
the queue, removing the message from the queue when the message is read.

set messageObject = queueObject.Receive( _
  [Transaction] _
  [, WantDestinationQueue] _
  [, WantBody] _
  [, ReceiveTimeout] _
)

Parameters

messageObject
Message (MSMQMessage) object that represents the message retrieved from the
queue.

queueObject
Queue (MSMQQueue) object that represents queue where the message resides.

Transaction
Optional. An MSMQTransaction object or one of the following constants:
MQ_NO_TRANSACTION: Specifies that the call is not part of a transaction.
MQ_MTS_TRANSACTION: Default. Specifies that the call is part of the current
MTS transaction.
MQ_XA_TRANSACTION: Specifies that the call is part of an externally... blah
blah blah

WantDestinationQueue
Optional (default is FALSE). If TRUE, MSMQMessage.DestinationQueueInfo is
updated when the message is read from the queue. Setting this property to
TRUE may slow down the operation of the application.

WantBody
Optional (default is TRUE). If you do not need to retrieve the body of the
message,
set this property to FALSE to optimize the speed of the application.

ReceiveTimeout
Optional (default is INFINITE). Specifies how long (in milliseconds) Message
Queuing waits for a message to arrive.



import win32com.client
import whrandom
def ConnectUserManager():
    global RequestQueueInfo,RequestQueue,ReplyQueue,ReplyQueueInfo
    print "Opening authorisation queue"
    try:
        RequestQueueInfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")
        RequestQueueInfo.PathName = ".\\private$\\Authorisation"
        RequestQueue = win32com.client.Dispatch("MSMQ.MSMQQueue")

        # Open with MQ_SEND_ACCESS and MQ_DENY_NONE
        RequestQueue = RequestQueueInfo.Open(0x2,0x0)
        print "Authorisation queue has been opened"

        ReplyQueueInfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")

        # Generate a random path name for replies
        Random = whrandom.randint(0,0x1000)
        PathName = ".\\private$\\ReplyAuthorisation_%(Random)X" % vars()
        ReplyQueueInfo.PathName = PathName

        ReplyQueueInfo.Create()

        # Open MQ_RECEIVE_ACCESS and MQ_DENY_NONE
        ReplyQueue = ReplyQueueInfo.Open(0x1,0x0)
        print "Reply queue has been opened"

    except:
        print "Exception thrown opening queue"

def DisconnectUserManager():
    print "DisconnectUserManager called"
    try:
        ReplyQueue.Close()
        ReplyQueueInfo.Delete()
        RequestQueue.Close()
    except:
        print "Exception closing queues"

def CheckAuthorised():
    print "CheckAuthorised called"
    global IsAuthorised
    IsAuthorised = "Not Authorised"
    UserName = "Lofty"

    try:
        # Construct a message to send to the authorisation server
        RequestMessage = win32com.client.Dispatch("MSMQ.MSMQMessage")
        RequestMessage.Body = UserName;
        RequestMessage.Label = ReplyQueueInfo.FormatName
        RequestMessage.Send(RequestQueue)

          # This is the problem line of code!
        ReplyMessage = ReplyQueue.Receive(0,0,1)
        CheckAuthorised = ReplyMessage.Body
        if CheckAuthorised == 1:
            IsAuthorised = "User Authorised"
        else:
            IsAuthorised = "Authorisation blocked"

    except:
        print "Exception requesting authorisation"

ConnectUserManager()
CheckAuthorised()
DisconnectUserManager()

_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to