Return Events from DispatchWithEvents using third party COM
<p>My code prints out what I need from the print statement in Events.  But, I have no idea how to return the data because of the way the class is instantiated.  Further, the print statement only works if pythoncom.PumpWaitingMessages() is included, but it doesn't return the data that's printed, or anything.</p>

<p>I'd like to be able to use what's printed as a return value to be accessed by other functions.  </p>

<p>(If worse comes to worse, I could capture stdout (which is a last resort).)</p>

<p>Code:</p>

<pre><code># Standard Lib
import time
# Third Party
from win32com.client import DispatchWithEvents
import pythoncom
# Local Lib
import scan_var


class Events(object):
    def OnBarcodeEvent(self, eventType=pythoncom.Empty, pscanData=pythoncom.Empty):
        print pscanData
        return pscanData

zebra = DispatchWithEvents("CoreScanner.CoreScanner", Events)

# open api
open_status = zebra.Open(0, [1], 1)
print "Open status: {}".format(open_status)
# get scanners
get_scanners = zebra.GetScanners(0, [1])
print "get_scanners: {}".format(get_scanners)
# Register for events
register = zebra.ExecCommand(1001,scan_var.register_for_events)
print "register: {}".format(register)
# PEWPEWPEW (pull trigger)
fire_result = zebra.ExecCommand(2011, scan_var.pull_trigger)
print "PEWPEWPEW {}".format(fire_result)
time.sleep(5)

while True:
    time.sleep(1)
    pythoncom.PumpWaitingMessages()
</code></pre>

<p>Output:</p>

<pre><code>Open status: 0
get_scanners: (1, (1,),504&lt;/VID&gt;    &lt;PID&gt;6400&lt;/PID&gt; ...
register: (u'', 0)
PEWPEWPEW (u'', 0)
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;outArgs&gt;
    &lt;scannerID&gt;1&lt;/scannerID&gt;
    &lt;arg-xml&gt;
        &lt;scandata&gt;
            &lt;modelnumber&gt;new_hotness &lt;/modelnumber&gt;
            &lt;serialnumber&gt;1522501a0501156  &lt;/serialnumber&gt;
            &lt;GUID&gt;2A4BE99CFCEFD047837ADF0082aD51AD5&lt;/GUID&gt;
            &lt;datatype&gt;27&lt;/datatype&gt;
            &lt;datalabel&gt;0x39 0x32 0x304 ...&lt;/datalabel&gt;
            &lt;rawdata&gt;0x22 0x03 0x00 ... &lt;/rawdata&gt;
        &lt;/scandata&gt;
    &lt;/arg-xml&gt;
&lt;/outArgs&gt;
</code></pre>

<p>My solution is as follows.  This may be ugly and wrong but it got me what I need.  If anyone has a better way I'm happy to edit.</p>

<pre><code>class Events(object):

    def get_barcode(self):
        return self.pscanData

    def OnBarcodeEvent(self, eventType=1, pscanData=pythoncom.Empty):
        self.pscanData = pscanData
        print self.pscanData



def save_serial_to_cache():
    zebra = DispatchWithEvents("CoreScanner.CoreScanner", Events)
    # open api
    open_status = zebra.Open(0, [1], 1)
    print "Open status: {}".format(open_status)
    #get scanners
    get_scanners = zebra.GetScanners(0, [1])
    print "get_scanners: {}".format(get_scanners)
    # Register for events
    register = zebra.ExecCommand(1001,scan_var.register_for_events)
    print "register: {}".format(register)
    # PEWPEWPEW (pull trigger)
    fire_result = zebra.ExecCommand(2011, scan_var.pull_trigger)
    print "PEWPEWPEW {}".format(fire_result)
    for counter in xrange(0, 5):
        time.sleep(1)
        pythoncom.PumpWaitingMessages()
    return zebra._obj_.get_barcode.__self__.pscanData
</code></pre>

python python-2.7 com win32com pythoncom