[python-win32] Implementing a python com server that is used by a python client
Ulrich Mierendorff
ulrich.mierendorff at gmx.net
Tue Mar 2 21:39:43 CET 2010
Mark Hammond wrote:
> On 2/03/2010 9:10 AM, Ulrich Mierendorff wrote:
>> Hi,
>>
>> I have implemented a simple com server called "TestServer" using the
>> following mail as an example
>> http://markmail.org/message/vheujc2lijgwhhju#query:+page:1+mid:4lgd7lzzokglsckc+state:results
>>
>>
>> (but without multi-threading support)
>>
>> Now I want to write a python client that should be able to connect to
>> "TestServer" via COM and receive events. Connecting to the server and
>> calling methods is easy: I just use win32com.client.Dispatch(..).
>>
>> As far as I know I have to use DispatchWithEvents if I would like to
>> receive events. This requires makepy registration, so I created an
>> idl-file for the server, compiled it with midl to a tlb file and
>> compiled this with makepy to a python file.
>
> You probably need to tell your server about the typelib being
> associated with your server - eg, look at the
> win32com\test\pippo_server.py - it includes the following information
> in the class:
>
> ### Link to typelib
> _typelib_guid_ = '{41059C57-975F-4B36-8FF3-C5117426647A}'
> _typelib_version_ = 1, 0
> _com_interfaces_ = ['IPippo']
>
> Obviously your ids will be different, but this should be enough to
> allow your server to provide the typeinfo when requested by Python,
> and therefore allow clients to use it.
Thank you for this excellent help! I used these modifications, changed
some other parts (to make this work with events), changed my .idl with
the help of pippo.idl, registered the typelib and simplified the client
and server code. Now it works!
Here is the output from the client:
Z:\extension\test>testclient.py
<win32com.gen_py.CAC1FA4D-46F4-4F9C-82B1-2807A80CC681x0x1x0.ITestServer
instance at 0x13566480>
<win32com.client.COMEventClass instance at 0x13566840>
client: OnMyEvent received: 123 ; arg type: <type 'int'>
client: OnMyEvent2 received: this is a str ; arg type: <type 'unicode'>
client: OnMyEvent2 received: this is an unicode string äöü ; arg type:
<type 'unicode'>
And debug output from the server (I don't know why those "None" messages
appear, but it works...)
in _Invoke_ with 1 0 1 (123,)
server: fire_event called 123 ; arg type: <type 'int'>
server: OnMyEvent fired 123 ; arg type: <type 'int'>
None
in _Invoke_ with 2 0 1 (u'this is a str',)
server: fire_event2 called this is a str ; arg type: <type 'unicode'>
server: OnMyEvent2 fired this is a str ; arg type: <type 'unicode'>
None
in _Invoke_ with 2 0 1 (u'this is an unicode string \xe4\xf6\xfc',)
server: fire_event2 called this is an unicode string {characters with
destroyed encoding}; arg type: <type 'unicode'>
server: OnMyEvent2 fired this is an unicode string {characters with
destroyed encoding} ; arg type: <type 'unicode'>
None
I've attached the testclient.py, testserver.py and testserver.idl. Maybe
it will help others if they have similar problems..
-Ulrich
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.zip
Type: application/zip
Size: 2374 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100302/59ba9188/attachment.zip>
More information about the python-win32
mailing list