[IronPython] COM Late Binding in IronPython

Shri Borde Shri.Borde at microsoft.com
Thu Apr 10 15:16:15 PDT 2008


I am not sure what the exact question is. However, I have posted the following at http://blogs.msdn.com/shrib/archive/2008/04/10/ole-automation-idispatch-support-in-ironpython.aspx to help understand what -X:PreferComDispatch does.

OLE automation (IDispatch) support in IronPython
IronPython 1.X supported COM interop by building on top of the COM interop<http://msdn2.microsoft.com/en-us/library/6bw51z5z(VS.80).aspx> support built into the CLR. This relies on the use of interop assemblies for managed code to access COM objects. The interop assembly can be accessed in different ways:
1.       clr.AddReference("interopAssembly") - This is the preferred mechanism so that the interop assembly does not have to be created multiple times. The down-side is that it complicates deployment. You have to ensure<http://msdn2.microsoft.com/en-us/library/697w37zd(VS.80).aspx> that the interop assembly exists on the machine.
2.       Having IronPython automatically generate it on the fly - if you have a OLE automation object, IronPython tries to find its type library (TLB) and then to convert the TLB to an interop assembly on the fly using the TypeLibConverter<http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.typelibconverter(VS.80).aspx> class. The advantage is that this works seamlessly as long as there is a type library on the machine. The down-side is that this conversion can take a long time for large TLB files.
This is how Word could be accessed. It requires the interop assembly Microsoft.Office.Interop.Word.dll to be available on the machine.
c:\>ipy.exe
IronPython 2.0 Beta (2.0.0.1000) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> clr.AddReference("Microsoft.Office.Interop.Word")
>>> from Microsoft.Office.Interop.Word import ApplicationClass
>>> word = ApplicationClass()
>>> word.Visible = True
>>> doc = word.Documents.Add()
>>> doc.Range().Text = "Hello from IronPython"
>>> word.Quit()
>>>
IronPython 2.0 has added support for interacting with OLE automation objects using the IDispatch interface. This completely eliminates the need for interop assemblies. This is the same mechanism that VBA<http://msdn2.microsoft.com/en-us/isv/bb190538.aspx> late-bound code and also WSH<http://msdn2.microsoft.com/en-us/library/9bbdkx3k.aspx> use to interact with OLE automation object. This feature can currently be accessed using the -X:PreferComDispatch command line option. Here is an interactive session showing access to Word. There are no interop assemblies in the picture.
c:\>ipyd -X:PreferComDispatch
IronPython 2.0 Beta (2.0.0.1000) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> import System
>>> wordTypeLibGuid = System.Guid("00020905-0000-0000-C000-000000000046")
>>> clr.AddReferenceToTypeLibrary(wordTypeLibGuid)
>>> from Word import Application
>>> word = Application()
>>> word.Visible = True
>>> doc = word.Documents.Add()
>>> doc.Range().Text = "Hello from IronPython"
>>> word.Quit()
>>>
The feature is not complete yet, and so is not currently (April 08) enabled by default. I cant say for sure when we will enable it by default, but its already quite stable for many real-world uses. We run all of our COM tests in both modes - using interop assemblies and using IDispatch. There are a few differences between the two cases in the minute details, but the mainline scenarios work well in both. This feature is implemented in the DLR, and so should be accessible from all DLR-based languages with little effort.

Thanks,
Shri
Want to work on IronPython, IronRuby, F#?<http://blogs.msdn.com/ironpython/archive/2008/02/25/ironpython-ironruby-and-f-openings-in-dev-test-and-pm.aspx> Visit http://blogs.msdn.com/ironpython

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Davy Mitchell
Sent: Wednesday, April 09, 2008 1:31 PM
To: Discussion of IronPython
Subject: [IronPython] Late Binding in IronPython

Hi List,

It seems odd posting here with my work hat on but here goes... :-) !!

I've been looking at using IronPython and COM to work with some COM objects with VB6 (Project Compatibility - a *TOTAL* nightmare for interop).
In VB.net we get round this with CreateObject which works well though somewhat limit.

Is -X:PreferComDispatch flag intended to be an equivalent for IronPython and any other DLR languages? VBX?

Thanks,
Davy Mitchell

--
Davy Mitchell
Blog - http://www.latedecember.co.uk/sites/personal/davy/
Twitter - http://twitter.com/daftspaniel
Skype - daftspaniel http://needgod.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080410/fa5e452c/attachment-0001.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 480 bytes
Desc: image001.gif
Url : http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080410/fa5e452c/attachment-0002.gif 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.gif
Type: image/gif
Size: 466 bytes
Desc: image002.gif
Url : http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080410/fa5e452c/attachment-0003.gif 


More information about the Users mailing list