[python-win32] How to catch events from nested com objects

Henning Fagge I (GR/ETO) henning.i.fagge at ericsson.com
Thu Aug 7 16:00:09 EDT 2003


I have program where I dispatch a server with the command:

object=DispatchWithEvents("DebugMuxSrv.DebugMux",IDebugMuxEvents)

When I connect equipment to my PC I can se that the events specified in IDebugMuxEvents are called.

>From object I can get all connected target and dataproviders:

for Target in object.Targets:
	#Within each target there could be severeal Dataproviders
	for DataProvider in Target.DataProvider:
		#HOW should i catch events provided by DataProvider
		#Is it possible to: DispatchWithEvent(DataProvider,IDataProvidersEvents)

The problems is that when the dataproviders has some data for me they generate the event 'OnDataAvailable', but how should I connect the class IDataProviderEvents to the DataProvider object.

I have the solution for this written i C, but I don't know how to do it in python:

BOOL CComThread::ConnectToServer ( IDataProvider* pDataProvider )
{
	HRESULT hr = E_FAIL;
	BOOL bRet = FALSE;

	if ( pDataProvider != NULL )
	{
		LPUNKNOWN lpUnk = NULL;
		m_pSink = new CDataProviderSink ( m_hWndEditBox, m_hWndChildFrame );

		hr = pDataProvider->QueryInterface ( IID_IDispatch, (LPVOID*)&m_pobjDispatch );

		if ( SUCCEEDED ( hr ) )
		{
			if ( m_pSink != NULL )
			{
				lpUnk = m_pSink->GetIDispatch ( FALSE );
				if ( lpUnk != NULL )
				{
					if ( AfxConnectionAdvise ( m_pobjDispatch, DIID__IDataProviderEvents, lpUnk, FALSE, &m_dwCookie ) )
					{
						m_bIsConnected = TRUE;
						bRet = TRUE;
						DBGOUT ( 2, _T("DataProviderEvents up'n running.") );
					}
					else
						DBGOUT ( 2, _T("FAILED to setup the DataProviderEvents.") );
				}
			}
		}
	}
	return bRet;
}


****************************************'*
Full print of program:
******************************************
from win32com.client import DispatchWithEvents, Dispatch,getevents,WithEvents
import msvcrt, pythoncom
import time, sys
import types
import threading
import gendbgmux
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty
class IDataProviderEvents:
    def __init__(self):
        print 80*"I"
    def OnDisconnect(self):
        """method OnDisconnect"""
        print "****************************OnDisconnect*****************************"
    def OnDataAvailable(self, DataPacket=defaultNamedNotOptArg):
        """method OnDataAvailable"""
        print "************************OnDataAvailable******************************"
            
class ITargetEvents:
    def OnDataProviderRemoved(self, DataProvider=defaultNamedNotOptArg):
        """method OnDataProviderRemoved"""
        print "method OnDataProviderRemoved"
    def OnNewDataProvider(self, DataProvider=defaultNamedNotOptArg):
        """method OnNewDataProvider"""
        print "method OnNewDataProvider"
    def OnDataProviderUnavailable(self, DataProvider=defaultNamedNotOptArg):
        """method OnDataProviderUnavailable"""
        print "method OnDataProviderUnavailable"


class IDebugMuxEvents:
    def OnTargetConnected(self, Target=None):
        """method OnTargetConnected"""
        print "method OnTargetConnected"
    def OnTargetDisconnected(self, Target=None):
        """method OnTargetDisconnected"""
        print "method OnTargetDisconnected"


connected=0
IDB=None
print "Starting DBGMux" 
object=DispatchWithEvents("DebugMuxSrv.DebugMux",IDebugMuxEvents)
start=time.time()
while (time.time()-start)<10:
    time.sleep(2)
    pythoncom.PumpWaitingMessages()
    for Target in object.Targets:
        DataProviders=Target.DataProviders
        for DataProvider in DataProviders:
            print "Available DataProviders:",DataProvider.Name
            if str(DataProvider.Name)=="Interactive Debug":
                if DataProvider.Available and not connected:
                    print "Connect to: ",DataProvider.Name
                    if DataProvider.Connect():
                        IDB=DataProvider
                        print "Connect SUCCESS"
                        connected=1
                    else:
                        print "Connect FAILED"
                        connected=0
                if connected:
                    packet=Dispatch("DebugMuxSrv.DataPacket.1")
                    print "Sending dir command, which should cause a event(OnDataAvailable)"
                    packet.Data="dir\n"
                    IDB.Send(packet)
if connected:
    print "Disconnected to server"
    IDB.Disconnect()
print "Exit timeout"



More information about the Python-win32 mailing list