473,657 Members | 2,585 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.error Handler = 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
CoCreateInstanc e, 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("Pyth on.MyErrorHandl er").
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 ISAXErrorHandle r.

Mikko Ohtamaa
Student of Industrial Engineering & Management
Oulu, Finland

Sample code:

class MySaxErrorHandl er:

# Some random stuff in stucked into this class
# hoping it would help policies & wrapping
_reg_progid_ = "Art2ComponentV alidator.SaxErr orHandler"
_reg_clsid_ = "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}"
_reg_clsctx_ = "INPROC_SER VER"
_com_interfaces = [ "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}" ]
_query_interfac e_ = "{a60511c4-ccf5-479e-98a3-dc8dc545b7d0}"
_public_methods _ = [ "error", "fatalError ", "ignorableWarni ng" ]

# A piece of VB sample:
#Private Sub IVBSAXErrorHand ler_error(ByVal oLocator As
SXML2.IVBSAXLoc ator,

strErrorMessage As String, ByVal nErrorCode As Long)
# WriteErrorToRes ults "Error", strErrorMessage , _
# nErrorCode, oLocator.lineNu mber, oLocator.column Number
#End Sub

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

def fatalError(oLoc ator, strErrorMessage , nErrorCode):
print strErrorMessage

def ignorableWarnin g(oLocator, strErrorMessage , nErrorCode):
print strErrorMessage

def saxtest():

# SAXXmlReader MIDL: a4f96ed0-f829-476e-81c0-cdc7bd2a0802
# sax error handler: a60511c4-ccf5-479e-98a3-dc8dc545b7d0
# ivbsaxerrorhand ler: 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("Msxm l2.SAXXMLReader .4.0")
errorHandler = MySaxErrorHandl er()

# ISAXErrorHandle r: a60511c4-ccf5-479e-98a3-dc8dc545b7d0

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

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

# Following error:
# File "C:\Python22\Li b\site-packages\win32c om\server\util. py",
line 28, in wrap
# ob = ob.QueryInterfa ce(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.CLSC TX_INPROC_SERVE R
iface = win32com.client .pythoncom.CoCr eateInstance(cl sid,
None, create_as, iid)
# pywintypes.com_ error: (-2147221164, 'Class not registered',
None, None)
Jul 18 '05 #1
3 6408
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.error Handler = 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.error Handler = 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
7981
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 the same thread. If the callback arrives on a different thread, all hell break loose and the program dies horribly. looking at the C api documentation, I came upon the following block of
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 cann't be reproduced easily in my system and the call stack generated by the application is given below. Occurrence: 2005/4/22 23:24:57
8
3767
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 events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
0
1338
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 time overhead compared to the 'cast' -method. The problem with the cast method is that I don't know whether its C++ standard-compliant and/or portable? Example:
267
10676
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 http://www.artima.com/weblogs/viewpost.jsp?thread=147358
1
4879
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 'callbacks'. However it appears that the methods of my interface object are never called.
16
2591
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 previous python experiences: magic names (I felt almost as sad as when I discovered the strange pink worms that eat you in nethack, not to mention the mind flayers - I really hate them). I guess all programming languages have magic names to some...
0
1108
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 example code was taken from pg 1478 of the 3rd edition python o'reilly book. I cannot see what I'm doing wrong here to get this segfault? Please help. //-------------------python-code-------------------------// #! /usr/bin/env python...
4
2836
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 media_control.Run() , etc. I'm wondering how I should go about updating my gtk.Hscale widget as a trackbar for the avi player. In C++, I have the following callbacks that update the scrollbar and
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4158
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
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 we have to send another system
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.