Microsoft KB Archive/309336

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

Article ID: 309336

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Outlook 2000 Standard Edition

-



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 &quot;Symptoms&quot; 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, \Bin for a Visual Basic project, or  \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.</li> 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. <ol style="list-style-type: lower-alpha;"> In Visual Studio .NET Solution Explorer, right-click Outlook in the list of project references, and then click Remove.</li> On the Project menu, click Add Reference.</li> 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.</li> In the Add References dialog box, click OK to accept your selection.</li></ol> </li> Test the program again with the modified interop assembly.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

Additional query words: kbreadme

Keywords: kbautomation kbbug kbinterop kbmsg kbreadme KB309336

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.