Welcome to AddressOf.com Sign in | Help

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):

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

 

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

 

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.

Published Tuesday, May 18, 2004 12:45 AM by CorySmith

Comments

# re: Obfuscation Tip of the Day - Embedded Resources

Wednesday, May 19, 2004 7:58 PM by HumanCompiler
hhhmmm...that stinks...thanks for the info though!

I went into reflector to check out the ctor's and found that the new ctor you mention calls type.Module.Assembly.GetManifestResourceStream

I'm not expert, but I'd assume that stuff isn't accessible at design time or something like that. By design? I don't know really as I'm no expert in that particular area, but I thought it was interesting and would let you konw...

# re: Obfuscation Tip of the Day - Embedded Resources

Thursday, December 07, 2006 6:11 AM by KenKen

Yes, I found that problem too. Solution is to avoid renaming the type that is returned from Me.GetType() or this.GetType(), since the loader, simply uses the Type's name for locating the resource. Hopefully Preemptive will update the dotfuscator, so that i detects this correctly. Also any use of Enum.Parse or Enum.ToString() will mostly break during Obfuscation.

Anonymous comments are disabled