Never change members inside a property getter

For example instead of using NextRow which advances the row cursor each time it is called, define a GetNextRow() method.
This might seem petty, but consider the ‘Autos’ and ‘Local’ windows in the debugger. Each time the property is in context, it is being evaluated and the member is modified.

Published in: on March 26, 2008 at 19:20 Leave a Comment

Editing resources in existing .net assemblies.

Technorati Tags:

Preface

Editing binary resources when you have the source code is pretty easy. Just change the original file and maybe change some properties in the project to re-embed the modified resource. Piece of cake.

But what if you don’t have the source code? Or maybe you do but the build process is too lengthy/fragile/complicated/error-prone/etc. ? Another good reason is obfuscation with a changing/random map file, which means you can’t rebuild a single assembly and expect it to interact with the previously built assemblies.

Here’s how to modify a binary resource in 3 easy steps.

Step #1: Disassemble the assembly.

Use the following command to disassemble your .net exe or dll:

ildasm /out=assembly_name.il assembly_name.dll

(replace assembly_name.dll with your dll or exe name).

This will output plenty of data files. We are interested in these:

  1. *.resources - The binary resource files.
  2. assembly_name.res - The resources manifest.
  3. assembly_name.il - The actual MSIL code.

Step #2: Edit the resources file(s).

Prepare the modified/new resource, it doesn’t matter what name it is but the format should be the same as the one originally included in the assembly (e.g. replacing bitmap for bitmap, icon for icon).

Use a resource editor to edit the resource file. I use Resourcer for DotNet by Lutz Roeder, it’s freeware. By the way, the same author also wrote Reflector.NET, an awesome tool for .NET developers.

If you’re using the Resourcer, here are the steps required:

  1. Open the .resources file you want to change and rename the original resource item.
  2. Hit Ctrl-F to insert file and select the modified version from you file system.
  3. Rename the new item to have the same original name as the previous one (you can’t you copy/paste for that, for some reason it just copies the entire item instead of just the text).

Step #3: Reassemble the assembly.

Use the following command to disassemble your .net exe or dll:

Important: This will overwrite the original dll/exe !

ilasm /resource=assembly_name.res /dll /output=assembly_name.dll /key=signing_key.snk assembly_name.il

(replace assembly_name.dll with your dll or exe name and of course change the /dll to /exe if needed).

The signing key parameter is optional and is only needed if the original assembly was signed.

Of course, if it was signed by someone else to prevent tempering, you won’t be able to use the modified assembly unless you provide the original signing key, which I assume you won’t have. But that’s just how .NET works.

Published in: on September 24, 2007 at 20:42 Comments (1)

It’s not the same GAC anymore.

So, you think you know where the GAC is…

The problem (a.k.a. “why do I need to know?”)

Windows doesn’t let you access the GAC assemblies directly from Explorer. You just get the basic GAC metadata and can only add/remove entries but not manipulate them. You can’t even see file size!

The solution for .NET 1.1 (a.k.a. “oh, I already know that”)

You probably know you can access these assemblies using the console window by diving into the C:\WINDOWS\assembly\GAC\<assembly-name.dll> folder.

What changed in .NET 2.0 (a.k.a. “more places to hide in”)

The .NET 2.0 GAC is split between several folders.

Mostly you’ll find what you’re looking for in the C:\WINDOWS\assembly\GAC_MSIL\<assembly-name.dll> folder.

The previous “GAC” folder still exists and contains only .NET 1.1 assemblies.

There are a couple of other “GAC” prefixed folders which contain 32 and 64 bit specific assemblies. In these folders you’ll find assemblies which use some native code.

In short

.NET Version

GAC Location

1.1

C:\WINDOWS\assembly\GAC\

2.0

C:\WINDOWS\assembly\GAC_MSIL\ and C:\WINDOWS\assembly\GAC_32\ and

C:\WINDOWS\assembly\GAC_64\

Published in: on August 21, 2007 at 17:24 Leave a Comment