Defining an Interop DLL
❘
181
It’s essential to remember that the managed control is separate from the COM component in a
Windows Forms application. The COM component properties appear in a separate location and
the managed environment works with them differently. If you look in the
.RESX
file, you see some-
thing like this:
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACFTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5BeEhvc3QrU3RhdGUBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAAywAAAAIB
AAAAAQAAAAAAAAAAAAAAALYAAAAAAwAACAAUAAAAQgBlAGwAbABzAC4AdwBhAHYAAAAFAAAAAAAAAPA/
AwAAAAAABQAAAAAAAAAAAAgAAgAAAAAAAwABAAAACwD//wMAAAAAAAsA//8IAAIAAAAAAAMAMgAAAAsA
AAAIAAoAAABmAHUAbABsAAAACwAAAAsAAAALAP//CwD//wsAAAAIAAIAAAAAAAgAAgAAAAAACAACAAAA
AAAIAAIAAAAAAAsAAAAuHgAAfhsAAAs=
This binary data contains the information needed to configure the COM aspects of the component.
When the application creates the form, the binary data is added to the component using the
OcxState
property like this:
this.MP.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject(“MP.OcxState”)));
Because of the managed code/COM component duality of a Windows Forms application, you can’t
simply embed the COM component into an IronPython application using techniques such as the one
shown at
http://msdn.microsoft.com/library/dd564350.aspx
. You must provide the binary data
to the COM component using the
OcxState
property. Unfortunately, IronPython developers have an
added twist to consider. The C# code shown previously won’t work because you don’t have access to a
ComponentResourceManager
for the IronPython form. Instead, you must read the resource from disk
using code like this (note the code will appear on a single line in the source code file, even though it
appears on multiple lines in the book):
self.resources = System.ComponentModel.ComponentResourceManager.
CreateFileBasedResourceManager(
‘frmUseWMP’, ‘C:/0255 - Source Code/Chapter09’, None)
Now, here’s where the tricky part begins (you might have thought we were there already, but
we weren’t). The
CreateFileBasedResourceManager()
method doesn’t support
.RESX
files.
Instead, it supports
.RESOURCES
files. The ResGen utility can create
.RESOURCES
files. You might
be tempted to think that you can duplicate the binary data from the
.RESX
file using
.TXT
files as
suggested by the ResGen documentation. Unfortunately,
.TXT
files can only help you create string
data in
.RESOURCES
files.
So your first step is to create a Windows Forms application, add the component to it, perform any
required COM component configuration (no need to perform the managed part), save the result, and
then take the resulting
.RESX
file for your IronPython application. You can then use ResGen to create
the
.RESOURCES
file using a command line like this:
ResGen frmUseWMP.RESX
548592c09.indd 181
2/24/10 12:48:21 PM
www.finebook.ir