Protecting .NET Applications that Use Excel Interop

Microsoft Office primary interop assemblies give us the ability to create and modify Excel Spreadsheets from a .NET application.  Office applications like Excel are written in unmanaged code.  The primary interop assembly provides wrappers to call unmanaged COM objects from our managed .NET application. 

By default, when you reference an Office primary interop assembly, the interop types are embedded into your application to avoid having to deploy extra assemblies.  When applying protection, we must preserve some of these embedded types and methods to maintain COM interoperability.

Please consider the following example.  This simple C# application uses Excel Interop to create a spreadsheet and populate cells:


When applying for full protection with Dotfuscator, I experience a TypeLoadException at runtime:


To avoid this error, I will configure a rename exclusion for the embedded interop types.  All the embedded Interop types are in the Microsoft.Office.Interop.Excel namespace.  The specific types I need to preserve are interfaces, most of which contain placeholder methods of the form “_VtblGapX_XX” that also must be preserved.

Based on these patterns, I can leverage custom rules to simplify the Renaming configuration.  From my DotfuscatorConfig.xml:

After configuring this rule, the protected output runs properly, and my spreadsheet is created.

The above pattern is general enough to work if the Office primary interop assembly is used for creating any type of Office document: Excel spreadsheet, PowerPoint presentation, Word document, etc.

The full example can be downloaded here.

If you have any feedback on this topic or other topics you would like us to discuss in the Support Corner, please feel free to contact our Support Department.