[python-win32] COM server

Mark Hammond mhammond@skippinet.com.au
Tue, 4 Feb 2003 09:58:56 +1100


> Help!
> I am trying to implement a python COM server to be a
> custom test type for a commercial test tool.  I'm not
> sure how to handle in/out parameters on the server
> side (I have no trouble on the client side).
>
> For example, the doc says that the test type object
> has a method with the following syntax:
>   HRESULT GetBitmap([in] long Status, [out, retval]
> long * Value)
> Another method looks like this:
>   HRESULT CreateScriptTemplate([in] long TestKey,
> [in,out] BSTR * LocalPath, [out,retval] long * Value)
>
> How can I correctly implement methods like these in a
> COM server?

Just like a client!

>
>   Here's what I've done so far, trying to implement
> GetBitmap():
>   Value is supposed to be an HBITMAP of the 16x16 test
> type bitmap.  I used the dynamic policy for my server
> and popped up some win32ui.MessageBoxes in my object.
> The __init__ gets called, _dynamic_ gets called,
> various properties are queried, and then GetBitmap is
> called.  However I'm not sure how to return the
> HBITMAP.  I tried this:
>   def GetBitmap(self, Status):
>     f = open("c:\icon.bmp", "r")
>     b = win32ui.CreateBitmap()
>     b.LoadBitmapFile(f)
>     return 1, b.GetHandle()

Ignore the HRESULT - exceptions are used for this.  As this function is a
"void" from the POV of the IDL, there is only 1 return value - so you just
return it!
      return b.GetHandle()

If there are multiple out params, then you return a tuple, as you would need
to do for CreateScriptTemplate:

    return localPath, value

> I've tried just returning b.GetHandle() and other
> possibilities such as (0, b.GetHandle()).  However the
> test tool does not use my bitmap -- of course I don't
> get any errors and there is no relevant log.

This certainly should work, and does for almost all VB and C++ implemented
objects.  Have you registered your server with "--debug", and looked for
exceptions?

Oh - hang on - the bitmap is being deleted.  'b' is a local variable.  You
return b.GetHandle(), but as soon as the function returns, 'b' goes out of
scope.  Its descructor then frees the bitmap making the handle invalid.

Mark.