win32com extension help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Sparago

    #1

    win32com extension help

    (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 IDocHostUIHandl er interface. In Python COM
    extension lingo, I need an ICustomDoc interface and an
    IDocHOstUIHandl er 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_Interface SupportInfo 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\win32co mext\ifilter\de mo>filterDemo.p y
    test.txt
    Traceback (most recent call last):
    File "C:\Joey\pywin3 2-203\com\win32co mext\ifilter\de mo\filterDemo.p y",
    line 4,
    in ?
    from win32com.ifilte r 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\win32c om directory.)

    Can ANYONE help? Please?
  • Roger Upole

    #2
    Re: win32com extension help


    "Peter Sparago" <psparago@yahoo .com> wrote in message
    news:584da15e.0 410170900.3f090 aa4@posting.goo gle.com...[color=blue]
    > (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 IDocHostUIHandl er interface. In Python COM
    > extension lingo, I need an ICustomDoc interface and an
    > IDocHOstUIHandl er 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_Interface SupportInfo etc.)
    >[/color]


    Basically, you construct an array of PyCom_Interface SupportInfo'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_RegisterE xtensionSupport ,
    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.
    [color=blue]
    > 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\win32co mext\ifilter\de mo>filterDemo.p y
    > test.txt[/color]

    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


    Comment

    • psparago@yahoo.com

      #3
      Re: win32com extension help

      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_RegisterE xtensionSupport , 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_m ethods[]=
      {
      //{ "SetUIHandl er", PyICustomDoc::S etUIHandler, 1 }, // @pymeth
      SetUIHandler|De scription of SetUIHandler
      { NULL }
      };

      static const PyCom_Interface SupportInfo register_data[] =
      {
      PYCOM_INTERFACE _CLIENT_ONLY ( CustomDoc ),
      NULL
      };

      extern "C" __declspec(dlle xport)
      void initJoeyCustomC om(void)
      {
      printf("1\n");

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

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

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

      // Register all of our interfaces, gateways and IIDs.
      PyCom_RegisterE xtensionSupport (dict, register_data,
      sizeof(register _data)/sizeof(PyCom_In terfaceSupportI nfo));
      printf("5\n");



      Roger Upole wrote:[color=blue]
      > "Peter Sparago" <psparago@yahoo .com> wrote in message
      > news:584da15e.0 410170900.3f090 aa4@posting.goo gle.com...[color=green]
      > > (Sorry in advance for the long post.)
      > >
      > > Hi,
      > >
      > > I'm having a great deal of difficulty buiding a Python COM[/color][/color]
      extension.[color=blue][color=green]
      > > I am using the MSHTML ActiveX control in my application but I need[/color][/color]
      to[color=blue][color=green]
      > > 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[/color][/color]
      the[color=blue][color=green]
      > > ICustomDoc interface from the control and set up your own
      > > implementation of the IDocHostUIHandl er interface. In Python COM
      > > extension lingo, I need an ICustomDoc interface and an
      > > IDocHOstUIHandl er gateway.
      > >
      > > First off, any link to HOW-TO documentation on this subject would[/color][/color]
      be[color=blue][color=green]
      > > greatly appreciated.
      > >
      > > I wanted to start with something very simple, so I am trying to
      > > implement an extension that understands only the ICustomDoc[/color][/color]
      interface.[color=blue][color=green]
      > > I ran makegw and hand-edited the resulting interfaces to get my[/color][/color]
      ..PYD[color=blue][color=green]
      > > to compile and link (and even load!).
      > >
      > > I pretty much understand the actual implementation of the COM[/color][/color]
      support[color=blue][color=green]
      > > 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_Interface SupportInfo etc.)
      > >[/color]
      >
      >
      > Basically, you construct an array of PyCom_Interface SupportInfo'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_RegisterE xtensionSupport ,
      > 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.
      >[color=green]
      > > So, I thought I'd look for a working example and do a knock-off.[/color][/color]
      I'm[color=blue][color=green]
      > > running Python 2.3 and I've downloaded and built pywin32-203. I[/color][/color]
      have[color=blue][color=green]
      > > previously worked in great detail with the IFilter interface, so I
      > > chose that win32comext project to use as a model. The problem is,[/color][/color]
      when[color=blue][color=green]
      > > 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\win32co mext\ifilter\de mo>filterDemo.p y
      > > test.txt[/color]
      >
      > Just a guess here, do you have .py files registered to open with a[/color]
      debug[color=blue]
      > build of python ? If so, you'll need to have debug versions of all[/color]
      the[color=blue]
      > 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[/color]

      Comment

      • Roger Upole

        #4
        Re: win32com extension help

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


        <psparago@yahoo .com> wrote in message
        news:1098190067 .084805.135060@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > 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_RegisterE xtensionSupport , 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_m ethods[]=
        > {
        > //{ "SetUIHandl er", PyICustomDoc::S etUIHandler, 1 }, // @pymeth
        > SetUIHandler|De scription of SetUIHandler
        > { NULL }
        > };
        >
        > static const PyCom_Interface SupportInfo register_data[] =
        > {
        > PYCOM_INTERFACE _CLIENT_ONLY ( CustomDoc ),
        > NULL
        > };
        >
        > extern "C" __declspec(dlle xport)
        > void initJoeyCustomC om(void)
        > {
        > printf("1\n");
        >
        > // Initialize PyWin32 globals (such as error objects etc)
        > PyWinGlobals_En sure();
        > printf("2\n");
        >
        > PyObject *module = Py_InitModule(" JoeyCustomCom",
        > JoeyCustomCom_m ethods);
        > if (module==NULL)
        > return;
        > printf("3\n");
        >
        > PyObject *dict = PyModule_GetDic t(module);
        > if (dict==NULL)
        > return;
        > printf("4\n");
        >
        > // Register all of our interfaces, gateways and IIDs.
        > PyCom_RegisterE xtensionSupport (dict, register_data,
        > sizeof(register _data)/sizeof(PyCom_In terfaceSupportI nfo));
        > printf("5\n");
        >
        >
        >
        > Roger Upole wrote:[color=green]
        > > "Peter Sparago" <psparago@yahoo .com> wrote in message
        > > news:584da15e.0 410170900.3f090 aa4@posting.goo gle.com...[color=darkred]
        > > > (Sorry in advance for the long post.)
        > > >
        > > > Hi,
        > > >
        > > > I'm having a great deal of difficulty buiding a Python COM[/color][/color]
        > extension.[color=green][color=darkred]
        > > > I am using the MSHTML ActiveX control in my application but I need[/color][/color]
        > to[color=green][color=darkred]
        > > > 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[/color][/color]
        > the[color=green][color=darkred]
        > > > ICustomDoc interface from the control and set up your own
        > > > implementation of the IDocHostUIHandl er interface. In Python COM
        > > > extension lingo, I need an ICustomDoc interface and an
        > > > IDocHOstUIHandl er gateway.
        > > >
        > > > First off, any link to HOW-TO documentation on this subject would[/color][/color]
        > be[color=green][color=darkred]
        > > > greatly appreciated.
        > > >
        > > > I wanted to start with something very simple, so I am trying to
        > > > implement an extension that understands only the ICustomDoc[/color][/color]
        > interface.[color=green][color=darkred]
        > > > I ran makegw and hand-edited the resulting interfaces to get my[/color][/color]
        > .PYD[color=green][color=darkred]
        > > > to compile and link (and even load!).
        > > >
        > > > I pretty much understand the actual implementation of the COM[/color][/color]
        > support[color=green][color=darkred]
        > > > 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_Interface SupportInfo etc.)
        > > >[/color]
        > >
        > >
        > > Basically, you construct an array of PyCom_Interface SupportInfo'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_RegisterE xtensionSupport ,
        > > 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.
        > >[color=darkred]
        > > > So, I thought I'd look for a working example and do a knock-off.[/color][/color]
        > I'm[color=green][color=darkred]
        > > > running Python 2.3 and I've downloaded and built pywin32-203. I[/color][/color]
        > have[color=green][color=darkred]
        > > > previously worked in great detail with the IFilter interface, so I
        > > > chose that win32comext project to use as a model. The problem is,[/color][/color]
        > when[color=green][color=darkred]
        > > > 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\win32co mext\ifilter\de mo>filterDemo.p y
        > > > test.txt[/color]
        > >
        > > Just a guess here, do you have .py files registered to open with a[/color]
        > debug[color=green]
        > > build of python ? If so, you'll need to have debug versions of all[/color]
        > the[color=green]
        > > 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[/color]
        >[/color]


        Comment

        Working...