When you attempt to sink
Microsoft Outlook application events, an instance of the InvalidCastException
class is generated.
This behavior occurs because the event wrapper classes in
the interop assembly that is generated for Outlook have access privileges that
are too strict. Because these wrapper classes are marked Private
, the universal runtime disallows an IUnknown::QueryInterface
call to the wrapper classes, and then returns E_NOINTERFACE. This
translates to the exception error that is described in the "Symptoms" section.
To work around this problem, you can manually modify the
interop assembly that is generated for Outlook to relax the access privileges
for the event wrapper classes. To do this, follow these steps:
- Save and then close your Visual Studio .NET
- Open a Visual Studio .NET command prompt, and then change
the directory to the output directory of your project (for example,
Project Name\Bin for a Visual Basic project, or
Project Name\Bin\Release for a C#
- Use Ildasm.exe to extract the intermediate language from
the Outlook interop assembly. To do this, type the following at the command
ildasm.exe /source Interop.Outlook.dll /output=Interop.Outlook.il
- Open Interop.Outlook.il in a text editor such as WordPad,
and then search for occurrences of the _SinkHelper class. Change the access privileges of the _SinkHelper classes from Private to Public. Save and then close Interop.Outlook.il.
- Use Ilasm.exe with the /dll switch to recompile the
intermediate language file into an interop assembly. To do this, type the
following at the Visual Studio .NET command prompt:
ilasm.exe /dll Interop.Outlook.il /output=Interop.Outlook.dll
- Open your project in Visual Studio .NET.
- Add a reference to the Interop.Outlook.dll that you created
in step 5. To do this, follow these steps.
IMPORTANT: These steps are required. If you do not set a reference to the
new interop assembly, Visual Studio .NET regenerates the interop assembly when
you compile the project or create a setup package.
- In Visual Studio .NET Solution Explorer, right-click Outlook in the list of project references, and then click Remove.
- On the Project menu, click Add Reference.
- On the .NET tab, click Browse, click to select the Interop.Outlook.dll file in the output directory of your project, and then click Open.
- In the Add References dialog box, click OK to accept your selection.
- Test the program again with the modified interop
Microsoft has confirmed that this is a bug in the Microsoft
products that are listed at the beginning of this article.
Article ID: 309336 - Last Review: February 23, 2007 - Revision: 6.4
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
- Microsoft Outlook 2002 Standard Edition
- Microsoft Outlook 2000 Standard Edition
|kbautomation kbbug kbinterop kbmsg kbreadme KB309336|