win32com extension help

P

Peter Sparago

(Sorry in advance for the long post.)

Hi,

I'm having a great deal of difficulty buiding a Python COM extension.
I am using the MSHTML ActiveX control in my application but I need to
interact with and implement some Custom COM interfaces. All is well
with the basic operation of the control using Python.

Basically, I want to take over the right-click (context) menu from
MSHTML. To do this (to the best of my knowledge), you must request the
ICustomDoc interface from the control and set up your own
implementation of the IDocHostUIHandler interface. In Python COM
extension lingo, I need an ICustomDoc interface and an
IDocHOstUIHandler gateway.

First off, any link to HOW-TO documentation on this subject would be
greatly appreciated.

I wanted to start with something very simple, so I am trying to
implement an extension that understands only the ICustomDoc interface.
I ran makegw and hand-edited the resulting interfaces to get my .PYD
to compile and link (and even load!).

I pretty much understand the actual implementation of the COM support
generated by makegw, but I'm very hazy on the extension "wiring"
necessary to publish this COM support to Python. (i.e. the use of
PyMethodDef and PyCom_InterfaceSupportInfo etc.)

So, I thought I'd look for a working example and do a knock-off. I'm
running Python 2.3 and I've downloaded and built pywin32-203. I have
previously worked in great detail with the IFilter interface, so I
chose that win32comext project to use as a model. The problem is, when
I run the demo python code (called filterDemo.py) to exercise the
IFilter support, it fails with the following error:

C:\Joey\pywin32-203\com\win32comext\ifilter\demo>filterDemo.py
test.txt
Traceback (most recent call last):
File "C:\Joey\pywin32-203\com\win32comext\ifilter\demo\filterDemo.py",
line 4,
in ?
from win32com.ifilter import ifilter
ImportError: cannot import name ifilter

This is the same error I'm getting with my own implementation .PYD.

(BTW: Yes, I have moved ifilter.pyd into the
C:\Python23\Lib\site-packages\win32com directory.)

Can ANYONE help? Please?
 
R

Roger Upole

Peter Sparago said:
(Sorry in advance for the long post.)

Hi,

I'm having a great deal of difficulty buiding a Python COM extension.
I am using the MSHTML ActiveX control in my application but I need to
interact with and implement some Custom COM interfaces. All is well
with the basic operation of the control using Python.

Basically, I want to take over the right-click (context) menu from
MSHTML. To do this (to the best of my knowledge), you must request the
ICustomDoc interface from the control and set up your own
implementation of the IDocHostUIHandler interface. In Python COM
extension lingo, I need an ICustomDoc interface and an
IDocHOstUIHandler gateway.

First off, any link to HOW-TO documentation on this subject would be
greatly appreciated.

I wanted to start with something very simple, so I am trying to
implement an extension that understands only the ICustomDoc interface.
I ran makegw and hand-edited the resulting interfaces to get my .PYD
to compile and link (and even load!).

I pretty much understand the actual implementation of the COM support
generated by makegw, but I'm very hazy on the extension "wiring"
necessary to publish this COM support to Python. (i.e. the use of
PyMethodDef and PyCom_InterfaceSupportInfo etc.)


Basically, you construct an array of PyCom_InterfaceSupportInfo's
that contain the interfaces your extension module will implement.
(the PYCOM_INTERFACE_ macros require very strict naming conventions)

When you pass this array to PyCom_RegisterExtensionSupport,
it adds your interfaces to Pythoncom's internal array of supported
interfaces. This is used to lookup the type object that's used as
a constructor for the Python interface object.
So, I thought I'd look for a working example and do a knock-off. I'm
running Python 2.3 and I've downloaded and built pywin32-203. I have
previously worked in great detail with the IFilter interface, so I
chose that win32comext project to use as a model. The problem is, when
I run the demo python code (called filterDemo.py) to exercise the
IFilter support, it fails with the following error:

C:\Joey\pywin32-203\com\win32comext\ifilter\demo>filterDemo.py
test.txt

Just a guess here, do you have .py files registered to open with a debug
build of python ? If so, you'll need to have debug versions of all the
extension
modules (*_d.pyd) alongside the normal .pyd files to be able to
import them. Or vice-versa, if you only have a debug version of the
extension
built you won't be able to import it from a normal python.exe.

hth
Roger
 
P

psparago

Hi Roger,

Thanks so much for the post back. I really appreciate it.

The problem with ifilter has been corrected by uninstalling python and
all related python packages I'm using. I hate doing that, but a
colleague was able to run filterDemo.py without incident.

I'm still struggling with importing my ICustomDoc interface. Here's the
first of my issues. This is the .PYD registration function. (I didn't
include the definition of the interface itself, but it's the result of
running makegw on the MSHTML IDL include that has ICustomDoc.)

If I try to call PyCom_RegisterExtensionSupport, the python executor
crashes. OK, I know it's me, but I'm having trouble tracking the
problem down.

If I try to build a debug version of my stuff, it complains that i
don't have a debug version of python (something I'm trying to avoid).
If I could use a debugger, I'd be much more independent about figuring
out what is wrong. Any hints on debugging something like this without
using a debug build of Python? (I'm on Windows.)

Here's the code that fails. Any thoughts would be most appreciated.

static struct PyMethodDef JoeyCustomCom_methods[]=
{
//{ "SetUIHandler", PyICustomDoc::SetUIHandler, 1 }, // @pymeth
SetUIHandler|Description of SetUIHandler
{ NULL }
};

static const PyCom_InterfaceSupportInfo register_data[] =
{
PYCOM_INTERFACE_CLIENT_ONLY ( CustomDoc ),
NULL
};

extern "C" __declspec(dllexport)
void initJoeyCustomCom(void)
{
printf("1\n");

// Initialize PyWin32 globals (such as error objects etc)
PyWinGlobals_Ensure();
printf("2\n");

PyObject *module = Py_InitModule("JoeyCustomCom",
JoeyCustomCom_methods);
if (module==NULL)
return;
printf("3\n");

PyObject *dict = PyModule_GetDict(module);
if (dict==NULL)
return;
printf("4\n");

// Register all of our interfaces, gateways and IIDs.
PyCom_RegisterExtensionSupport(dict, register_data,
sizeof(register_data)/sizeof(PyCom_InterfaceSupportInfo));
printf("5\n");
 
R

Roger Upole

You can do a release build of your extention that contains
debugging information, and use it with a normal python installation.
Roger


Hi Roger,

Thanks so much for the post back. I really appreciate it.

The problem with ifilter has been corrected by uninstalling python and
all related python packages I'm using. I hate doing that, but a
colleague was able to run filterDemo.py without incident.

I'm still struggling with importing my ICustomDoc interface. Here's the
first of my issues. This is the .PYD registration function. (I didn't
include the definition of the interface itself, but it's the result of
running makegw on the MSHTML IDL include that has ICustomDoc.)

If I try to call PyCom_RegisterExtensionSupport, the python executor
crashes. OK, I know it's me, but I'm having trouble tracking the
problem down.

If I try to build a debug version of my stuff, it complains that i
don't have a debug version of python (something I'm trying to avoid).
If I could use a debugger, I'd be much more independent about figuring
out what is wrong. Any hints on debugging something like this without
using a debug build of Python? (I'm on Windows.)

Here's the code that fails. Any thoughts would be most appreciated.

static struct PyMethodDef JoeyCustomCom_methods[]=
{
//{ "SetUIHandler", PyICustomDoc::SetUIHandler, 1 }, // @pymeth
SetUIHandler|Description of SetUIHandler
{ NULL }
};

static const PyCom_InterfaceSupportInfo register_data[] =
{
PYCOM_INTERFACE_CLIENT_ONLY ( CustomDoc ),
NULL
};

extern "C" __declspec(dllexport)
void initJoeyCustomCom(void)
{
printf("1\n");

// Initialize PyWin32 globals (such as error objects etc)
PyWinGlobals_Ensure();
printf("2\n");

PyObject *module = Py_InitModule("JoeyCustomCom",
JoeyCustomCom_methods);
if (module==NULL)
return;
printf("3\n");

PyObject *dict = PyModule_GetDict(module);
if (dict==NULL)
return;
printf("4\n");

// Register all of our interfaces, gateways and IIDs.
PyCom_RegisterExtensionSupport(dict, register_data,
sizeof(register_data)/sizeof(PyCom_InterfaceSupportInfo));
printf("5\n");



Roger said:
Basically, you construct an array of PyCom_InterfaceSupportInfo's
that contain the interfaces your extension module will implement.
(the PYCOM_INTERFACE_ macros require very strict naming conventions)

When you pass this array to PyCom_RegisterExtensionSupport,
it adds your interfaces to Pythoncom's internal array of supported
interfaces. This is used to lookup the type object that's used as
a constructor for the Python interface object.


Just a guess here, do you have .py files registered to open with a debug
build of python ? If so, you'll need to have debug versions of all the
extension
modules (*_d.pyd) alongside the normal .pyd files to be able to
import them. Or vice-versa, if you only have a debug version of the
extension
built you won't be able to import it from a normal python.exe.

hth
Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top