COM callbacks in Python

D

Dan

I need to register for a COM callback under Windows. I am using an ADO
recordset interface like this:

import win32com.client
import time
connect = win32com.client.Dispatch("ADODB.Connection")
recordset = win32com.client.Dispatch("ADODB.Recordset")
connect.Open("Driver={SQLServer};Server=devserver;Database=VidVisitation;UID=sa;PWD=;")
datestring = time.strftime("%m/%d/%Y")
sql = "Select * from VisSchedule where SDATE='" + datestring +"'"
recordset.Open(sql,connect)

Now I want to receive events for the recordset when the recordset
changes. Here is the IDL for the event:
[id(0x0000000e)]
HRESULT RecordsetChangeComplete(
[in] EventReasonEnum adReason,
[in] Error* pError,
[in, out] EventStatusEnum* adStatus,
[in] _Recordset* pRecordset);

How do I wire up the recordset I have created to receive this event?
 
R

Roger Upole

Dan said:
I need to register for a COM callback under Windows. I am using an ADO
recordset interface like this:

import win32com.client
import time
connect = win32com.client.Dispatch("ADODB.Connection")
recordset = win32com.client.Dispatch("ADODB.Recordset")
connect.Open("Driver={SQLServer};Server=devserver;Database=VidVisitation;UID=sa;PWD=;")
datestring = time.strftime("%m/%d/%Y")
sql = "Select * from VisSchedule where SDATE='" + datestring +"'"
recordset.Open(sql,connect)

Now I want to receive events for the recordset when the recordset
changes. Here is the IDL for the event:
[id(0x0000000e)]
HRESULT RecordsetChangeComplete(
[in] EventReasonEnum adReason,
[in] Error* pError,
[in, out] EventStatusEnum* adStatus,
[in] _Recordset* pRecordset);

How do I wire up the recordset I have created to receive this event?
Use win32com.client.DispatchWithEvents, and create a class that
implements the event methods defined for the Recordset object.
You can find prototypes for the methods in the makepy-generated
file.

Roger
 
D

Dan

Looked at the makepy, code now looks like this:
import win32com.client
import win32gui
import time
import pythoncom
finished = 0
defaultNamedNotOptArg=pythoncom.Empty
class ADOEvents:
def OnRecordsetChangeComplete(self, adReason=defaultNamedNotOptArg,
pError=defaultNamedNotOptArg, adStatus=pythoncom.Missing,
pRecordset=defaultNamedNotOptArg):
print "recordset has changed"
global finished
finished=1
connect = win32com.client.Dispatch("ADODB.Connection")
recordset =
win32com.client.DispatchWithEvents("ADODB.Recordset",ADOEvents)
connect.Open("Driver={SQL
Server};Server=devserver;Database=VidVisitation;UID=sa;PWD=;")
datestring = time.strftime("%m/%d/%Y")
sql = "Select * from VisSchedule where SDATE='" + datestring +"'"
recordset.Open(sql,connect)
while not finished:
win32gui.PumpMessages()
pass

any idea why it doesn't work? Ive not really seen much on this in the
newsgroups so any help would be greatly appreciated. I came upon a post
by Paul Moore and and angusm describing code like this. angusm
suggested that all of the interfaces must be implemented within the
Events class, while Paul Moore suggested they didn't. Who is right?
Here is the link to the thread:
http://groups.google.com/group/comp...ul+moore+angusm&rnum=1&hl=en#b52f9ce9ac6403f4
 
D

Dan

Ok, seems to fire at least once now, had to implement
OnWillChangeRecordset as well. Definitely don't have to implement all
the events though. Still not sure why its not acting like it should.
Maybe I am looking at the wrong event? I want to know if the database
has changed when someone else enters, edits etc... a record from
another application. Ill investigate, if anyone knows, please help.
 
D

Dan

One final note, The code posted does work. Unfortunately, the event
only fires for the ADO connection who actually triggered the event. In
my opinion, completely useless event. So the Python worked, The ADO
does not do what its name implies. Thanks to all. Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top