BUG: InvalidCastException Is Generated When You Try to Sink Outlook Application Events

Article translations Article translations
Article ID: 309336 - View products that this article applies to.
This article was previously published under Q309336
Expand all | Collapse all


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:
  1. Save and then close your Visual Studio .NET project.
  2. 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).
  3. 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
  4. 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.
  5. 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
  6. Open your project in Visual Studio .NET.
  7. 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.
    1. In Visual Studio .NET Solution Explorer, right-click Outlook in the list of project references, and then click Remove.
    2. On the Project menu, click Add Reference.
    3. 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.
    4. In the Add References dialog box, click OK to accept your selection.
  8. Test the program again with the modified interop assembly.


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

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com