Howto debug a dll loaded at runtime?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabio Cannizzo

    Howto debug a dll loaded at runtime?

    I load an assembly DLL dynamically using
    System.AppDomai n.CurrentDomain .Load(... ).
    I create the istance of an object contained in the assembly and invoke one
    of its method.
    How can I make the debugger stop within the code of this DLL?

    If my question is not clear, please have a look below... Thanks a lot


    In ClassLibrary1.d ll I have

    interface MyInterface {
    int MyMethod();
    }

    In ClassLibrary2.d ll I have

    public MyClass : MyInterface {
    int MyMethod() { retrun 10; );
    }


    In Main.exe I have

    .....
    FileStream fs = new FileStream(@"Cl assLibrary2.dll ", FileMode.Open);
    byte[] buffer = new byte[(int) fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    fs.Close();
    Assembly asm = System.AppDomai n.CurrentDomain .Load( buffer ); // load the
    assembly
    Type[] tp = asm.GetTypes();
    foreach ( Type t in tp )
    if (null != t.GetInterface( "MyInterfac e" ) ) { // scan for types
    which implement MyInterface
    // call default constructor with no parameters
    Object obj = t.InvokeMember( null, BindingFlags.De claredOnly |
    BindingFlags.Pu blic | BindingFlags.In stance | BindingFlags.Cr eateInstance,
    null, null, null);
    // invole a method
    int i = (int) ((MyInterface)o bj).MyMethod(); // I would like the
    debugger to stop during the execution of MyMethod. How can I do???
    }
    .....


  • Joshua Flanagan

    #2
    Re: Howto debug a dll loaded at runtime?

    Assuming you have the ClassLibrary2.d ll project loaded in Visual Studio
    (along with ClassLibrary1), you should be able to step into the code if
    you put a breakpoint on the call to MyMethod.

    Curious - do you need to load the assembly as a stream?

    If not, your code might be simpler load the assembly like this:

    Assembly asm = Assembly.Load(" ClassLibrary2") ;



    Joshua Flanagan

    Comment

    • Fabio Cannizzo

      #3
      Re: Howto debug a dll loaded at runtime?

      Hi Joshua.

      Thanks for the tip. I had to use AssemblyLoadFil e(..) instead of
      Assembly.Load() , but now it seems to work (also the debugger).

      Regards,
      Fabio

      "Joshua Flanagan" <josh@msnews.co m> wrote in message
      news:%23TAH$JeA GHA.984@tk2msft ngp13.phx.gbl.. .[color=blue]
      > Assuming you have the ClassLibrary2.d ll project loaded in Visual Studio
      > (along with ClassLibrary1), you should be able to step into the code if
      > you put a breakpoint on the call to MyMethod.
      >
      > Curious - do you need to load the assembly as a stream?
      >
      > If not, your code might be simpler load the assembly like this:
      >
      > Assembly asm = Assembly.Load(" ClassLibrary2") ;
      >
      >
      >
      > Joshua Flanagan
      > http://flimflan.com/blog[/color]


      Comment

      Working...