I was googling for something or another and I ran across NoDeleteDelay. Code Project is awesome for learning new things that you would otherwise have no idea about how to even begin programming. It doesn't always have the most accurate articles (usually the articles that have no rating, make sure to always read the messages about the bottom) so it's always best to check more than one site. Usually doing a cross check with MSDN clears up any questions you might have.
The article talks about two tools WinDbg and Process Explorer. System Internals provides a whole suite of useful utilities that may be helpful when trying to track down an issue.
WinDbg will take you deep into the way a process actually executes. It's especially helpful for programs where you have access to the code, but otherwise wouldn't typically be able to debug (read COM).
Using WinDbg With .NET COM Object
We are going to debug an ActiveX object I found on the net CPieMenuCtrl. This method could be used to debug any COM object, using the same princliples. First add a MessageBox to the inside of Init so we can find the process and skip most of the underlying windows calls when creating the object (like IMPLEMENT_OLECREATE_EX for instance).

Then create a simple VB program to run the COM object. Create a form that creates the object when the form loads. Add a button that executes the method we are trying to debug when pressed.


Don't run the program just yet. First start up WinDbg. Since we will run the code directly from the editor, attach the debugger to the editor.


There are a few windows you want to start with: Call Stack, Processes and Threads. I don't really use command, since I'm still learning WinDbg, so I keep it as a tab in the Call Stack window. Later you'll need: Command, Locals.
Once the debugger attaches the process and you've setup your windows, go ahead and let the process run (Go-F5). Then go back to the editor and execute the form (Start-F5).
What's the first thing we see? The message box we added to Init. This is the perfect time to find the process so go to WinDbg and break (Break-Ctrl+Break). You'll see the number of Processes and Threads increase. Click through the different threads until you find calls you recognize.

Here we have our object. Double click one of the CPieMenuCtrl calls to bring up the call in the source viewer.


Now we can begin debugging. Search through the source viewer for the method we are trying to debug. If it's in a different source file use File->Open Source File to open the correct one.

Go to the beginning of the method we are trying to debug and press F9 to set a break statement. If your curious the showErrort2 was to show any possible errors.

Press Go-F5 to resume again. Go back to the form and click the button. It will cause the debugger to break and we can begin debugging the COM object!

You'll definitely want to add at least one more window: Locals. Now you can debug the code using Step Into, Step Over, Step Out.
No comments:
Post a Comment