Post

Obfuscation Tip of the Day - Embedded Resources

When loading embedded resources like bitmaps and icons from your assembly, such as using the following method as described by Windows Forms Programming in Visual Basic .NET (Chris Sells & Justin Gehtland):

1
NotifyIcon1.Icon = New Icon(Me.GetType, "graphic.ico")

It sure seems straight forward enough. It looks a whole lot simpler than the following method:

1
NotifyIcon1.Icon = New Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication1.graphic.ico"))

So, when I saw this while reading through (what is, by the way, one of the best books I’ve read on Windows Forms development in .NET), I though that I would switch all instances in my code where I’m using the second method with the newly discovered one. Testing the code in debug mode (which I don’t obfuscate) worked without any issues whatsoever. However, once I got ready to debug in Release mode (which is obviously obfuscated), problems started rearing their ugly head. To make matters even worse, one of my components (which is specific to this assembly) also had embedding graphics. Because the OnPaint method was trying to loaded embedded graphics… it apparently caused the component not to be loadable in the designer (while in Release mode) and decided to just conveniently remove itself from the designer. Not to bad in and of itself, however, because I’m using an interesting combination of docking and anchoring, it caused a few headaches.

So, although the newly discovered mechanism looked much more efficient and easier to use; the original one works when you obfuscate the assembly, while the new one definitely fails most miserably.

This post is licensed under CC BY 4.0 by the author.