Compartilhar via


Debugging a OneNote Toolbar Addin (C#)

I was just asked this question internally and I thought I would share with everyone:

Can I debug the OneNote add-in within Visual Studio? I tried to attach OneNote.exe but it did not stop at any breakpoint.

I wanted to let people know that you can debug one of these powertoys, here's some notes for debugging a C# OneNote toolbar addin:

  1. When OneNote runs your toolbar addin your app is not running on OneNote.exe instead we are using COM and a DLL surrogate.
  2. The app which has the DLL surrogate is actually DllHost.exe
  3. You want to attach the debugger to the DllHost.exe process when you addin is running
  4. Since the addins normally start/stop quickly you can add MessageBox.Show command at the beginning of your code
  5. When you click on the button in OneNote you will see your message box popup, during this pause attach your debugger to DllHost.exe
  6. Debug away!

If anyone has any problems or needs help please let me know.  I answered this internally and I wanted to make sure external people knew about this as well.  Take care!

Note: a lot of the credit goes to David Tse who gave me these details.

Comments

  • Anonymous
    May 02, 2007
    You can also add this code to your object constructor in order to launch the debugger from inside of your addin: ctor{ #if DEBUG  System.Diagnostics.Debugger.Launch(); #endif } This will either launch a new debugger or attach to an already existing one (also the code gets compiled away under Release mode removing all chances of the debugger launching). blair

  • Anonymous
    May 07, 2007
    This also works, if (!Debugger.IsAttached) Debugger.Break();

  • Anonymous
    May 08, 2007
    Hello, I am having a problem, the button appears in the tool bar in One note correctly, but as soon as it is clicked on it becomes disabled. the popup never appears. Kevin Damen

  • Anonymous
    May 09, 2007
    Kevin - This means that the toolbar addin isn't registered correctly.  Have you read my documentation here: http://blogs.msdn.com/descapa/archive/2006/08/31/734298.aspx I have also seen problems if you are using Vista and have UAC/LUA disabled.  You can always turn on the debugging and see what error OneNote sees when trying to call the COM methods.

  • Anonymous
    January 30, 2008
    The comment has been removed

  • Anonymous
    January 29, 2009
    I had the same issue trying to use the Search and Replace add-in. I went back into my Office installtion setup and noticed that .NET Programmability Support was not installed.  Just for good measure I also installed Microsoft Forms 2.0 .NET Programmability Support. Worked like a charm! I launched OneNote and clicked the button; the form window popped right up.  I didn't even have to reboot or go dig out my Office 2007 installation media!