COM callbacks in Python

Discussion in 'Python' started by Dan, Mar 23, 2006.

  1. Dan

    Dan Guest

    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?
     
    Dan, Mar 23, 2006
    #1
    1. Advertising

  2. Dan

    Roger Upole Guest

    "Dan" <> wrote in message news:...
    >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




    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Roger Upole, Mar 24, 2006
    #2
    1. Advertising

  3. Dan

    Dan Guest

    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
     
    Dan, Mar 24, 2006
    #3
  4. Dan

    Dan Guest

    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.
     
    Dan, Mar 24, 2006
    #4
  5. Dan

    Dan Guest

    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
     
    Dan, Mar 29, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikko Ohtamaa
    Replies:
    2
    Views:
    428
    John J. Lee
    Jul 4, 2003
  2. Melissa Wallis

    omniorb, python and callbacks

    Melissa Wallis, Apr 26, 2004, in forum: Python
    Replies:
    1
    Views:
    428
    Duncan Grisby
    Apr 28, 2004
  3. Marco Aschwanden

    Python, DDE and Callbacks

    Marco Aschwanden, May 21, 2004, in forum: Python
    Replies:
    0
    Views:
    5,059
    Marco Aschwanden
    May 21, 2004
  4. Randall Hopper

    Python callbacks & PyGILState_Release()

    Randall Hopper, Apr 23, 2005, in forum: Python
    Replies:
    4
    Views:
    509
    Randall Hopper
    Apr 26, 2005
  5. Replies:
    4
    Views:
    1,040
Loading...

Share This Page