BUG: InvalidCastException Is Generated When You Try to Sink Outlook Application Events (309336)
The information in this article applies to:
- Microsoft Windows .NET Framework 1.1
- Microsoft .NET Framework 1.0
- Microsoft Outlook 2002
- Microsoft Outlook 2000
This article was previously published under Q309336 SYMPTOMS When you attempt to sink Microsoft Outlook application events, an instance of the InvalidCastException class is generated. CAUSE 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.
RESOLUTION 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
project.
- 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#
project).
- Use Ildasm.exe to extract the intermediate language from
the Outlook interop assembly. To do this, type the following at the command
prompt:
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
assembly.
STATUSMicrosoft has confirmed that this is a bug in the Microsoft
products that are listed at the beginning of this article.
Modification Type: | Major | Last Reviewed: | 2/9/2004 |
---|
Keywords: | kbAutomation kbbug kbinterop kbMsg kbreadme KB309336 |
---|
|