views:

210

answers:

2

Hi!

I want to get my hands on an assembly by saving it to disc or reflect it at runtime. The assembly is generated dynamically in memory by third party.

Does anyone know how to do this?

//Daniel

A: 

You can use the Save method on AssemblyBuilder.

Romain Verdier
HAHA, try again :)
leppie
Assembly saving is so funny! But obviously you're right -- if Daniel has an Assembly instance AssemblyBuilder won't be very usefull :)
Romain Verdier
You can only set those flags when you create the assembly/module.
leppie
Well, if the guy's lucky enough, he can cast the Assembly to an AssemblyBuilder, and call Save on it if it has been created with Save or RunAndSave flags.
Jb Evain
I can't use Save since I have an InternalAssemblyBuilder.
Daniel
@Daniel: What do you mean by "InternalAssemblyBuilder"? Is that a custom type of yours? Of the 3rd party library? What does it look like then?
Romain Verdier
A: 

You can do this using SOS in WinDbg.

The problem is finding the modules.

If you do get your hands on it, there is a 'SaveModule' command to dump the module/assembly to file.

Good luck :)

leppie
Well, talking about trying again, you can't SaveModule a «Run» only module :)
Jb Evain
@JB: this is from within WinDbg, I am not aware of such limitations.
leppie
That's what am saying. You can see the module with !DumpDomain, but you can't !SaveModule it.
Jb Evain
That's because you need the base address which is very difficult to find! http://blogs.msdn.com/debuggingtoolbox/archive/2007/07/02/windbg-script-extracting-base-address-and-image-name-from-a-method-call.aspx
leppie
The fact that you're not aware of the limitation doesn't make the limitation any less real. A dynamically created assembly doesn't have a module that you can interact with like any loaded PE module in windbg. They don't have a base address that you can use, and you can't dump them.
Jb Evain
@JB: thanks. I always thought it was possible. So how does MS debug dynamic assemblies? ;)
leppie
You can still debug dynamic assemblies (albeit you lose a bit of details and possibilities iirc). It's just that dynamic module are not in a PE shape that msdbg can dump until they're really saved to disk. They have their custom representation in the CLR.
Jb Evain