List Info

Thread: how to get the interface of an COM object?




how to get the interface of an COM object?
user name
2006-11-14 05:23:15
Hi, all

I am trying to use win32com to interact with a proprietary
COM object.
progID of the coclass is "SomeObj2.SomeObj.1", I
can create the
coclass but am not able to access other interfaces via
win32com.client.CastTo().

For example ISomeInterface is one of this COM object's
interfaces. But
no classes are generated for this interface in the file made
by
makepy.py. I can only find things like this:

ISomeInterface_vtables_dispatch_, ISomeInterface_vtables_

this is my code:

    te =
win32com.client.Dispatch("SomeObj2.SomeObj.1")
    # fail! Error message is:
    # ...
    # return mapCLSIDToClass[clsid]
    # KeyError: ''
    ISomeInterface_ = win32com.client.CastTo(te,
'ISomeInterface')


in the file generated by makepy.py, the class is very
simple:

class SomeObj(CoClassBaseClass): # A CoClass
	CLSID = IID('')
	coclass_sources = [
		_SomeObjEvents2,
		_SomeObjEvents,
		_SomeObjEvents4,
		_SomeObjEvents5,
		_SomeObjEvents3,
	]
	default_source = _SomeObjEvents5
	coclass_interfaces = [
	]

I googled the web and find some old posts in this maillist,
it seems
that only interfaces inherit from IDispatch can be used by
pythonCOM,
and interfaces here inherits from IUnknown. But I just need
to use
these interfaces, so could anybody tell me how to do it?

And this control and its interfaces are all available to
VBScript or
Visual Bacis:

   Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
   ...
   <OBJECT ID="SomeObj"
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>

Since VBScript and python are both script languages, I guess
python
can also access these interfaces.

Any help would be appreciated!

Bruce
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 05:43:11
> I googled the web and find some old posts in this
maillist, it seems
> that only interfaces inherit from IDispatch can be used
by pythonCOM,
> and interfaces here inherits from IUnknown. But I just
need to use
> these interfaces, so could anybody tell me how to do
it?

I'm afraid that what you googled is still correct - only
interfaces
implemented using IDispatch can be called by pywin32.

> And this control and its interfaces are all available
to VBScript or
> Visual Bacis:
>
>    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
>    ...
>    <OBJECT ID="SomeObj"
>
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
>
> Since VBScript and python are both script languages, I
guess python
> can also access these interfaces.

VB can use all interfaces, but I believe VBScript can not. 
Can you show me
some VBScript code that runs under the Windows Scripting
Host (WSH - ie,
wscript.exe/cscript.exe) that does what you need?

Cheers,

Mark

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 07:09:44
Bruce Who schrieb:

> I googled the web and find some old posts in this
maillist, it seems
> that only interfaces inherit from IDispatch can be used
by pythonCOM,
> and interfaces here inherits from IUnknown. But I just
need to use
> these interfaces, so could anybody tell me how to do
it?
> 
> And this control and its interfaces are all available
to VBScript or
> Visual Bacis:
> 
>    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
>    ...
>    <OBJECT ID="SomeObj"
>
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
> 
> Since VBScript and python are both script languages, I
guess python
> can also access these interfaces.

comtypes can call custom com interfaces:

http://www.python
.org/pypi/comtypes

The downside is that there are no docs, yet.

Thomas

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 08:17:53
Hi, Thomas,

Thanks, I'll give it a try.

On 11/14/06, Thomas Heller <thellerctypes.org> wrote:
> Bruce Who schrieb:
>
> > I googled the web and find some old posts in this
maillist, it seems
> > that only interfaces inherit from IDispatch can be
used by pythonCOM,
> > and interfaces here inherits from IUnknown. But I
just need to use
> > these interfaces, so could anybody tell me how to
do it?
> >
> > And this control and its interfaces are all
available to VBScript or
> > Visual Bacis:
> >
> >    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
> >    ...
> >    <OBJECT ID="SomeObj"
> >
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
> >
> > Since VBScript and python are both script
languages, I guess python
> > can also access these interfaces.
>
> comtypes can call custom com interfaces:
>
> http://www.python
.org/pypi/comtypes
>
> The downside is that there are no docs, yet.
>
> Thomas
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32python.org
> 
http://mail.python.org/mailman/listinfo/python-win32
>
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 05:43:11
> I googled the web and find some old posts in this
maillist, it seems
> that only interfaces inherit from IDispatch can be used
by pythonCOM,
> and interfaces here inherits from IUnknown. But I just
need to use
> these interfaces, so could anybody tell me how to do
it?

I'm afraid that what you googled is still correct - only
interfaces
implemented using IDispatch can be called by pywin32.

> And this control and its interfaces are all available
to VBScript or
> Visual Bacis:
>
>    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
>    ...
>    <OBJECT ID="SomeObj"
>
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
>
> Since VBScript and python are both script languages, I
guess python
> can also access these interfaces.

VB can use all interfaces, but I believe VBScript can not. 
Can you show me
some VBScript code that runs under the Windows Scripting
Host (WSH - ie,
wscript.exe/cscript.exe) that does what you need?

Cheers,

Mark

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 07:09:44
Bruce Who schrieb:

> I googled the web and find some old posts in this
maillist, it seems
> that only interfaces inherit from IDispatch can be used
by pythonCOM,
> and interfaces here inherits from IUnknown. But I just
need to use
> these interfaces, so could anybody tell me how to do
it?
> 
> And this control and its interfaces are all available
to VBScript or
> Visual Bacis:
> 
>    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
>    ...
>    <OBJECT ID="SomeObj"
>
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
> 
> Since VBScript and python are both script languages, I
guess python
> can also access these interfaces.

comtypes can call custom com interfaces:

http://www.python
.org/pypi/comtypes

The downside is that there are no docs, yet.

Thomas

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 08:17:53
Hi, Thomas,

Thanks, I'll give it a try.

On 11/14/06, Thomas Heller <thellerctypes.org> wrote:
> Bruce Who schrieb:
>
> > I googled the web and find some old posts in this
maillist, it seems
> > that only interfaces inherit from IDispatch can be
used by pythonCOM,
> > and interfaces here inherits from IUnknown. But I
just need to use
> > these interfaces, so could anybody tell me how to
do it?
> >
> > And this control and its interfaces are all
available to VBScript or
> > Visual Bacis:
> >
> >    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
> >    ...
> >    <OBJECT ID="SomeObj"
> >
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
> >
> > Since VBScript and python are both script
languages, I guess python
> > can also access these interfaces.
>
> comtypes can call custom com interfaces:
>
> http://www.python
.org/pypi/comtypes
>
> The downside is that there are no docs, yet.
>
> Thomas
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32python.org
> 
http://mail.python.org/mailman/listinfo/python-win32
>
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 10:43:27
Hi, Thomas

Finally I make it to create a COM object:

class EventSink:
    def OnLoadFinished(self):
        print 'load finished'

o =
comtypes.client.CreateObject("SomeObj2.SomeObj.1",
sink=EventSink())

but the EventSink.OnLoadFinished() is never called, and I
still donnot
know how to get an interface, :-(


On 11/14/06, Thomas Heller <thellerctypes.org> wrote:
>
> comtypes can call custom com interfaces:
>
> http://www.python
.org/pypi/comtypes
>
> The downside is that there are no docs, yet.
>
> Thomas
>

Bruce Who
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 12:19:05
Bruce Who schrieb:
> Hi, Thomas
> 
> Finally I make it to create a COM object:
> 
> class EventSink:
>     def OnLoadFinished(self):
>         print 'load finished'
> 
> o =
comtypes.client.CreateObject("SomeObj2.SomeObj.1",
sink=EventSink())
> 
> but the EventSink.OnLoadFinished() is never called, and
I still donnot
> know how to get an interface, :-(


Well, comtypes.client.CreateObject /returns/ a pointer to a
COM interface.
If it is not the interface you want, you can call
o.QueryInterface(IOtherInterface)
on it - how you get or create the IOtherInterface Python
class that 'describes'
the interface is another question.  If the COM object
supplies type information
then comtypes generates a Python wrapper for the type
library automatically
in the 'comtypes.gen' package.  The module name is derived
from the type library,
for example for InternetExplorer  the typelib is name
comtypes.gen.SHDocVw (Note
that this is a very shallow module, it imports everything
from another module that
is named after the GUID and version from the typelib; for IE
it is
'comtypes.gen._EAB22AC0_30C1_11CF_A7EB_0000C05BAE0B_0_1_1').
You can import the interfaces from any of these modules.

For the events: First, the event handler methods receive an
additional argument
'this' just after the 'self', this is an implementation
detail of comtypes; which
mirrors that COM methods you *implement* also receive this
argument.
Then, it could be that the name of the method is wrong;
comtypes does *not* prepend
an 'On' to the method name.

A useful event sink for COM objects is one that responds to
__getattr__ by dynamically
creating event handlers, the attached script demonstrates
this (for IE, again).

Oh, there 'may* be a totally different reason why you don't
receive events:
By default, comtypes (like pythoncom) creates a
single-threaded COM appartment,
in which you usually should run a message pump.

Thomas

import unittest
import comtypes.client

class EventHandler(object):
    """Instances are called when the COM
object fires events."""
    def __init__(self, name):
        self.name = name

    def __call__(self, this, *args, **kw):
        print "Event %s fired" % self.name, args,
kw

class Events(object):
    """Provide handlers for COM
events."""
    def __init__(self):
        self._event_names = set()

    def __getattr__(self, name):
        if name.startswith("__") and
name.endswith("__"):
            raise AttributeError(name)
        print "# event found:", name
        self._event_names.add(name)
        return EventHandler(name)

class EventsTest(unittest.TestCase):
    def test(self):
        sink = Events()
        o =
comtypes.client.CreateObject("InternetExplorer.Applicat
ion",
                                         sink = sink)

        from comtypes.gen.SHDocVw import IWebBrowser
        print
        print "Default interface", o
        print "IWebBrowser interface",
o.QueryInterface(IWebBrowser)

        o.Visible = True
        o.Visible = False
        o.Quit()

if __name__ == "__main__":
    unittest.main()
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
how to get the interface of an COM object?
user name
2006-11-14 05:43:11
> I googled the web and find some old posts in this
maillist, it seems
> that only interfaces inherit from IDispatch can be used
by pythonCOM,
> and interfaces here inherits from IUnknown. But I just
need to use
> these interfaces, so could anybody tell me how to do
it?

I'm afraid that what you googled is still correct - only
interfaces
implemented using IDispatch can be called by pywin32.

> And this control and its interfaces are all available
to VBScript or
> Visual Bacis:
>
>    Set ISomeInterface_ =
SomeObj.interface("ISomeInterface")
>    ...
>    <OBJECT ID="SomeObj"
>
CLASSID="CLSID:7A4F5191-68A8-14D5-82C1-0001083952C1&quo
t; ></OBJECT>
>
> Since VBScript and python are both script languages, I
guess python
> can also access these interfaces.

VB can use all interfaces, but I believe VBScript can not. 
Can you show me
some VBScript code that runs under the Windows Scripting
Host (WSH - ie,
wscript.exe/cscript.exe) that does what you need?

Cheers,

Mark

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32
[1-10] [11-15]

about | contact  Other archives ( Real Estate discussion Medical topics )