Open COM application in NET using IronPython
Question: I have been trying to perform what initially seemed a trivial task: opening up
a WordPerfect program using python and .NET. After 2 weeks of near-success and
miserable failure, I am starting to wonder if all .NET paths are designed to
lead the programmer toward the inevitable purchase of a real version of Visual
Studio...

Here are my basic tools: .NET version 4.030... ; IronPython 2.7.1; Eclipse
Indigo IDE with PyDev; WordPerfect x4 (I have tried using x5 also, with the
same results).

I converted the wpwin14.tlb into a .NET assembly using the tlbimp.exe program
from the Windows command line:

> tlbimp wpwin14.tlb /out: NETwpwin14.dll

The program converted the .tlb file, but renamed it as "WordPerfect.dll" for
some reason.

Then I registered the assembly by using the RegAsm command, acting as computer
administrator:

> regasm WordPerfect.dll

I got a message that said the assembly was registered (where it was registered
is unknown).

Then I attempted to connect to the program using the following code:

    
    
    import clr
    clr.AddReference ('WordPerfect')
    import WordPerfect
    WP = WordPerfect.PerfectScript
    WP.AppMaximize ()       # AppMaximize is a PerfectScript call to open the program
    

The clr reference to WordPerfect and the import statement were recognized by
Eclipse, and the entire set of PerfectScript commands was made available
inside the editor (only, however, after putting a copy of the WordPerfect.dll
inside the IronPython2.7\Lib\site-packages folder).

Running the script from Eclipse produced the following error:

> TypeError: AppMaximize() takes exactly 1 argument (0 given)

Please note that AppMaximize() does not need any arguments to run correctly.

Trying other PerfectScript commands such as WPActivate() and RevealCodes(1)
gave similar errors, except that the RevealCodes(1) command somehow managed to
turn that feature on the next time I opened WordPerfect from the GUI manually.

I got the same error when running the script inside the IronPython
interpreter, and with the same ability to import the class and inspect it.

I worded the python code this way, based on previous successful experience
using VB.NET inside Visual Basic 2010 Express. That code was essentially this:

    
    
    Imports WordPerfect
    Module Module1
    Dim wpwin As WordPerfect.PerfectScript = New WordPerfect.PerfectScript
    wpwin.AppMaximize()
    ...
    

In order to create the reference to "WordPerfect" in VB Express, I merely
browsed to the very same wpwin14.tlb file inside the WordPerfect program
directory and dropped it into the COM box. VB Express then converted that file
into a usable dll (apparently, not using the same methods I used).

I then tried an approach similar to what the IronPython tutorial page suggests
(<http://www.ironpython.info/index.php/Interacting_with_Excel>), which is to
call the "ApplicationClass" within the object:

    
    
    import clr
    clr.AddReference ('Microsoft.Office.Interop.Excel')
    from Microsoft.Office.Interop import Excel 
    ex = Excel.ApplicationClass()
    

Running that code opened up Excel with no complaints.

A quick look into the structure of my imported WordPerfect class using dir()
revealed an ApplicationClass method.

Then I tried the following commands (using the IronPython interpreter):

    
    
    import clr
    clr.AddReference ('WordPerfect')
    import WordPerfect as WP
    WP.ApplicationClass()
    

I got another error, with the pertinent part here:

> EnvironmentError: System.Runtime.InteropServices.COMException (0x80040154):
> Retrieving the COM class factory for component with CLSID
> {40852D4E-0076-47CD-8C70-92E42B35E5EC} failed due to the following error:
> 80040154 Class not registered (Exception from HRESULT: >0x80040154
> (REGDB_E_CLASSNOTREG)).

Thinking I perhaps needed to put my dll into the GAC, I tried this:

    
    
    gacutil /i WordPerfect.dll
    

That threw me this error:

> Failure adding assembly to the cache: Attempt to install an assembly without
> a strong name.

Now I am stuck.

Do I need to make a strongly named assembly for this third party dll? (I am
inclined to think that is not needed.)

Am I missing something important on the python side regarding how class
objects are called?

Have I missed some sort of instantiation/invoking/incantation spell?

Why does VB Express have no trouble with the dll, and I am unable to use it
effectively in Eclipse?

Am I doomed to spend big $ on more Microsoft products?

I apologize for the length of this post. And, yes, there still are good
reasons to use WordPerfect!


Answer: Maybe you need to call the constructor for PerfectScript

    
    
    WP = WordPerfect.PerfectScript()
    WP.AppMaximize ()  
    

instead of the current

    
    
    WP = WordPerfect.PerfectScript
    WP.AppMaximize ()  
    

