In CCWSpy.NET Progress Update Part 1, I mentioned I was unable to view the generated assembly. Turns out that I had to create my module like so,

  LModule := LAsm.DefineDynamicModule('EventSpyModule', 'EventSpyModule.dll');

In addition, I was also getting a MethodAccessException. I kept thinking something was wrong with my code, so I added the following code,

  ilctor.Emit(OpCodes.Break); // Insert a breakpoint
  for J := 1 to 5 do
    ilctor.Emit(OpCodes.Nop);    // Insert no-ops so I can identify the location of this code

and

  il.Emit(OpCodes.Break);
  for J := 1 to 10 do
    il.Emit(OpCodes.Nop);

into the generated code stream. Then, I discover a bug in the Delphi 8 IDE. Turns out that it cannot break on dynamically generated breaks. Even worse, the IDE disappears after encountering an IL OpCodes.Break instruction. (QC8330) Also, if an IL OpCodes.Break instruction is encountered in a constructor, the IDE doesn't even continue running the program. (QC8331) Using Reflector then, I discovered that Reflector can only handle the disassembly of OpCodes.Break instruction in IL, but not in C#, Delphi and VB.NET, so I sent Lutz a feedback, and he was pretty quick, responding with a request for the module, and the instructions used to generate it.

So, I had saved the generated IL stream to disk. In a flash of inspiration, I used peverify to peek at EventSpyModule.dll (which is the name of the generated module) and discovered that there's some errors.

Suspecting that it had an issue with the problem of scope, I moved the declaration of the EventSpy from the implementation section to the interface section. When I re-run the application, I didn't get any more errors.

Therefore, moving the EventSpy from the implementation section to the interface section actually causes something to have changed. Using Reflector, I wasn't able to locate anything that could have changed, but I'm suspecting that the visibility of the EventSpy class might have changed.

So, CCWSpy.NET is almost ready to spy on all events. Now, I've to do some more work, and I'll unleash the application upon the world.

Anyone interested in testing it out? Provide your email address, and I'll send it to ya. ;o)