473,503 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assing a COM Interface to a Python object for callbacks

Hi,

I am quite new to Python, PythonCom and COM generally. As a former
Java programmer, I have found Python's flexible ability to access
native Win32, especially COM, very comfortable. However, there is
quite little on-line documentation and examples available about
PythonCOM. The only good sources I have found were the sample chapter
from Mark Hammond's book and some decades old PowerPoint show.

I have a following problem:

I am trying to do XML Schema validation using MSXML SAX parser through
PythonCOM API.

MSXML offers two different interfaces for SAX: One for VB and one for
C++. C++ one is named ISAXXMLReader and VB one is named
IVBSAXXMLReader. I haven't found what is the basic difference between
these interfaces though I have carefully read MSXML SDK documentation.
I assume IVBSAXXMLReader might have some IDispatch functionality which
is requirement for PythonCom(?).

The problem is that SAX Parser requires COM interface for callback.
You must assign a COM interface as a handler to the parser. When the
parsing begins, the handler calls this interface.

saxReader.errorHandler = myErrorHandler <-- COM Interface

I do not know how to turn Python class into COM interface on the fly.

I have managed to open the C++ version ISAXXMLReader of the interface.
I have NOT succeed to do this for VB versioon IVBSAXXMLReader, because
I do not know how it happens. I assume you have to use
CoCreateInstance, but it returns "Class not registered", see the
sample below.

After opening ISAXXMLReader I have not been able to pass COM error
handler for it. I have reasons not to go through full process
registering my own error handler as a COM server into Windows registry
and create it using win32com.client.Dispatch("Python.MyErrorHandler").
Is there a way to create COM server on the fly and get the COM handle
for it? I have been trying to read win32.client and win32.server
Python sources and even PythonCOM C sources, but unfortunately my
skillz are not 1337 enough to see the big picture.

How to assign a COM Interface for a Python object? It goes somehow
through policies and wrapping, right? Unfortunately every approach I
have tried end to an error saying my wrapper IPyDispatch cannot be
used as ISAXErrorHandler.

Mikko Ohtamaa
Student of Industrial Engineering & Management
Oulu, Finland

Sample code:

class MySaxErrorHandler:

# Some random stuff in stucked into this class
# hoping it would help policies & wrapping
_reg_progid_ = "Art2ComponentValidator.SaxErrorHandler"
_reg_clsid_ = "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}"
_reg_clsctx_ = "INPROC_SERVER"
_com_interfaces = [ "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}" ]
_query_interface_ = "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}"
_public_methods_ = [ "error", "fatalError", "ignorableWarning" ]

# A piece of VB sample:
#Private Sub IVBSAXErrorHandler_error(ByVal oLocator As
SXML2.IVBSAXLocator,

strErrorMessage As String, ByVal nErrorCode As Long)
# WriteErrorToResults "Error", strErrorMessage, _
# nErrorCode, oLocator.lineNumber, oLocator.columnNumber
#End Sub

def error(oLocator, strErrorMessage, nErrorCode):
print strErrorMessage

def fatalError(oLocator, strErrorMessage, nErrorCode):
print strErrorMessage

def ignorableWarning(oLocator, strErrorMessage, nErrorCode):
print strErrorMessage

def saxtest():

# SAXXmlReader MIDL: a4f96ed0-f829-476e-81c0-cdc7bd2a0802
# sax error handler: a60511c4-ccf5-479e-98a3-dc8dc545b7d0
# ivbsaxerrorhandler: d963d3fe-173c-4862-9095-b92f66995f52
# ivbsaxxmlreader: 8c033caa-6cd6-4f73-b728-4531af74945f

# test path switcher
useISAX = 0

if useISAX:
# Use c++ version ISAXXMLReader
saxReader = win32com.client.Dispatch("Msxml2.SAXXMLReader.4.0" )
errorHandler = MySaxErrorHandler()

# ISAXErrorHandler: a60511c4-ccf5-479e-98a3-dc8dc545b7d0

iid = pywintypes.IID("{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}")

# What I should do instead of this?
saxReader.errorHandler =
win32com.server.util.wrap(errorHandler, iid)

# Following error:
# File "C:\Python22\Lib\site-packages\win32com\server\util.py",
line 28, in wrap
# ob = ob.QueryInterface(iid) # Ask the PyIDispatch
if it supports it?
# pywintypes.com_error: (-2147467262, 'No such interface
supported', None, None)

else:
# Use VB version, IVBSAXXMLreader
clsid = pywintypes.IID("{8c033caa-6cd6-4f73-b728-4531af74945f}")
iid = clsid
create_as = win32com.client.pythoncom.CLSCTX_INPROC_SERVER
iface = win32com.client.pythoncom.CoCreateInstance(clsid,
None, create_as, iid)
# pywintypes.com_error: (-2147221164, 'Class not registered',
None, None)
Jul 18 '05 #1
3 6386
Mikko Ohtamaa wrote:
I have a following problem:
I am trying to do XML Schema validation using MSXML SAX parser through
PythonCOM API.

MSXML offers two different interfaces for SAX: One for VB and one for
C++. C++ one is named ISAXXMLReader and VB one is named
IVBSAXXMLReader. I haven't found what is the basic difference between
these interfaces though I have carefully read MSXML SDK documentation.
I assume IVBSAXXMLReader might have some IDispatch functionality which
is requirement for PythonCom(?).

The problem is that SAX Parser requires COM interface for callback.
You must assign a COM interface as a handler to the parser. When the
parsing begins, the handler calls this interface.

saxReader.errorHandler = myErrorHandler <-- COM Interface

I do not know how to turn Python class into COM interface on the fly.


I'm not sure how Mark Hammond's Windows extensions handle this
situation, if at all.

But I do know that Thomas Heller's "ctypes" has a COM framework which
addresses the problem. Only thing is, I don't know how robust the
current support is, given that, in Thomas own words: "Warning: work in
progress".

http://starship.python.net/crew/thel...um_sample.html

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #2
Mikko Ohtamaa wrote:
I have a following problem:
I am trying to do XML Schema validation using MSXML SAX parser through
PythonCOM API.

MSXML offers two different interfaces for SAX: One for VB and one for
C++. C++ one is named ISAXXMLReader and VB one is named
IVBSAXXMLReader. I haven't found what is the basic difference between
these interfaces though I have carefully read MSXML SDK documentation.
I assume IVBSAXXMLReader might have some IDispatch functionality which
is requirement for PythonCom(?).

The problem is that SAX Parser requires COM interface for callback.
You must assign a COM interface as a handler to the parser. When the
parsing begins, the handler calls this interface.

saxReader.errorHandler = myErrorHandler <-- COM Interface

I do not know how to turn Python class into COM interface on the fly.


I'm not sure how Mark Hammond's Windows extensions handle this
situation, if at all.

But I do know that Thomas Heller's "ctypes" has a COM framework which
addresses the problem. Only thing is, I don't know how robust the
current support is, given that, in Thomas own words: "Warning: work in
progress".

http://starship.python.net/crew/thel...um_sample.html

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #3
Alan Kennedy <al****@hotmail.com> writes:
Mikko Ohtamaa wrote:

[...]
The problem is that SAX Parser requires COM interface for callback.
You must assign a COM interface as a handler to the parser. When the
parsing begins, the handler calls this interface.

[...]

See Mark Hammond's examples in the win32com/tests directory (IIRC)
that win32all installs in your Python directory. I can't remember
which example demonstrates this, but I'm pretty sure one does.
There's also been some discussion on c.l.py on this.
John
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7949
by: Steve Menard | last post by:
Here is my problem. I have this library thats hosts another language within python, and allows that language to call back INTO python. All is good as long as the other languages calls back on...
8
446
by: Saravanan | last post by:
Hello, Im running Python Application as a Windows Service (using windows extensions). But, sporadically the application crashes (crash in Python23.dll) and this stops the service. This problem...
8
3760
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
0
1324
by: news_mail_jpa | last post by:
Hi! I'd like to implement a private interface and I have the choices mentioned in the subject. The implementation using private inheritance is probably cleaner but it also adds some space and...
267
10507
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
1
4870
by: Divya | last post by:
Hello, I'm new to IronPython and COM so please bear with me. I have a COM interface provided to me. I need to implement this interface and pass it to a method as a way of implementing...
16
2572
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
0
1097
by: Tim Spens | last post by:
The following is a simple complete example using the c python api to generate callbacks from c to python. But when I run the c code I get a segfault in PyInt_FromLong () (see below). Most of this...
4
2827
by: Sayanan Sivaraman | last post by:
Hey all, So I've written a simple video player using directshow/COM in VC++, and I'm in the process of translating it to python. For example, when the avi starts playing, I have a call...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.