using the Windows Media Player Interop DLL
It’s finally time to use early binding to create a connection to the Windows Media Player. This example
uses the Windows Media Player as a control. You might find a number of online sources that say it’s
impossible to use the Windows Media Player as a control, but it’s actually quite doable. Of course, you
need assistance from yet another one of Microsoft’s handy utilities, Resource Generator (ResGen) to
do it. The example itself relies on the normal combination of a form file and associated application file.
The following sections provide everything needed to create the example.
Working with ResGen
Whenever you drop a control based on a COM component onto a Windows Forms dialog box, the
IDE creates an entry for it in the
.RESX
file for the application. This entry contains binary data that
describes the properties for the COM component. You may not know it, but most COM components
have a Properties dialog box that you access by right-clicking the control and choosing Properties from
the context menu. These properties are normally different from those shown in the Properties window
for the managed control. Figure 9-13 shows the Properties dialog box for the Windows Media Player.
fIguRE 9-13:
The COM component has properties that
differ from the managed control.
548592c09.indd 180
2/24/10 12:48:21 PM
www.finebook.ir
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
Do'stlaringiz bilan baham: |