win32com.client - need some help

Alex Martelli aleaxit at yahoo.com
Wed Jan 24 10:20:34 EST 2001


<zippy1984 at my-deja.com> wrote in message news:94mjj8$v3g$1 at nnrp1.deja.com...
> I am trying to port some VB code to Python, but I have a problem
> with code that passes Objects by reference....
>
> VB:
>    Dim ob as someObject
>    Dim dic as IDictionary
>    Dim i as Long
>
>    ob = CreateObject("some.object.1")
>    ob.getDictionary dic  ' set "dic" to point to the "ob" dictionary
>    i = dic.getValue("xyzzy")

Pretty unusual VB usage, but, yes, it does happen.  It's unclear
to me whether the single argument of method getDictionary is
[out] or [in,out] -- presumably just [out]...?  VB is often
not crystal-clear about such things (which yet ARE crucial --
resource leaks are possible if one gets confused) and looking
at the IDL for the type library in question is quite a good idea
(VB Professional, I think, comes with the "OLE Viewer" which,
among many other precious uses, lets one look at the IDL of any
type library; if it doesn't, then get the Platform SDK download,
THAT one surely does come with oleview.exe and it's free, though
rather big to get).


> Python:
>    import win32com.client
>    ob = win32com.client.Dispatch("some.object.1")  # ok

Do make sure makepy.py HAS been run on the typelibrary whence
some.object comes from, though.  To ensure that, you may use:

    import win32com.client.gencache
    ob = win32com.client.gencache.EnsureDispatch("some.object.1")

this will generate the 'makepy support' on-the-fly if needed
and feasible (one time only, of course; it then gets cached to
suitable disk files, and reused in any future run).


>    # I can now use "ob" to call methods that returns infomation
>    # by the return value.
>    #... then how do I get the dictionary?

Once makepy has operated on the type library, [out] arguments
are transformed into return values, so:

    dic = ob.getDictionary()

would work if [out] is indeed the argument's direction.  If
[in, out], then you will also need to pass the argument
explicitly (for the [in] part), as well as getting the resulting
new value as a return value from the method (for the [out]).


Alex






More information about the Python-list mailing list