"Toggle Header" Add-In for Visual Studio

Environment: Visual C++ 5 & 6


When I attempted to use the various toogleheader macros freely availabe in the open source space, Developer Studio would continually crash for such simple things as the file not existing! I think this is a result of a bug in the scripting engine, when you try to open a nonexisting file. Therefore, I decided to write a true Visual Studio Add-In to solve this problem.


Here are the installation notes to copy and register this Add-In's .DLL and to use it from Visual Studio.
  1. Unpack the DLL from the accompanying .zip file to the Visual Studio Add-Ins folder (typically c:\program files\microsoft visual studio\common\msdev98\addins).
  2. From there, open a command window and after navigating to the aforementioned "Add-Ins" folder, run the following program.
    c:\program files\microsoft visual studio\common\msdev98\addins>regsvr32 toogleh.dll
  3. Once you've done that, start Visual Studio and click on the Tools->Customize menu option

  4. As you can see in the figure above, at this point the Add-In will appear in Customize dialog. Simply click the checkbox next to it and then click the dialog's Close button.
  5. Now a new toolbar should be created with a single button on it representing your new Add-In. Simply click this button any time you want to open a .cpp file's associated .h file (or vice versa)!

How it Works

The first thing the Add-In does is to determine which file it is searching for (the target file). If you have a file open that is named test.cpp or test.h, the Add-In will be searching for a target file of either test.hpp or test.h. Conversely, if the currently open file is test.h or test.hpp, the target file would be test.cpp or test.c

Once the target file has been established, the Add-In first searches for this file in the current folder. If it can't find the it, the search continues in a subdirectory called "include" (if one exists). If the search is still unsuccessful, the Add-In then searches in the current folder's parent folder.

Here's a couple of examples of how the search would take place.

	Currently open file : folder\src\sample.cpp
	->	1. look		project folder\src\sample.h
		2. look		project folder\src\sample.hpp
		3. look		project folder\src\include\sample.h
		4. look		project folder\src\include\sample.hpp
		5. look		project folder\include\sample.h
		6. look		project folder\include\sample.hpp

	Currently open file : folder\include\sample.h
	->	1. look		project folder\include\sample.c
		2. look		project folder\include\sample.cpp
		3. look		project folder\include\src\sample.c
		4. look		project folder\include\src\sample.cpp
		5. look		project folder\src\sample.c
		6. look		project folder\src\sample.cpp

Licensing Information

This Add-In is absolutly free. You may copy, distribute or modify in any kind you wish.

Contact Information

I hope you'll find this add-in usefull in any ways. If you have any suggestions, comments etc. please feel free to email me -> powerstation@odn.de


Download source (including release build) - 54 Kb


  • A great alternative to this toggle add-in

    Posted by Legacy on 02/02/2004 12:00am

    Originally posted by: Eric Nuckols

    Check out WndTabs. It's a nice add-in and it's freeware up to a point. The advanced features cost $.


  • Minor Correction

    Posted by Legacy on 04/09/2003 12:00am

    Originally posted by: Malik

    Its great but correct the first line in the constructor:

    m_pApplication == NULL;

  • Works very good

    Posted by Legacy on 03/10/2003 12:00am

    Originally posted by: Alessandro

    This is the best source<->header toggler I found on the net.
    I am using VC++6.

    Thank You.

  • Update the macro to .NET

    Posted by Legacy on 02/22/2002 12:00am

    Originally posted by: Eran Strauchler

    I realy liked this macro so I update it to run under .NET

    Imports EnvDTE
    Imports System.Diagnostics

    Public Module Module1

    Sub ToggleHandCPP()
    'DESCRIPTION: Opens the .cpp or .h file for the current document.
    'Toggles between the .cpp & .h file
    'TODO: Put macro code here
    Dim ext As String
    Dim DocName As String
    Dim pos As VariantType
    Dim fn As String
    'Dim Documents1 As Document
    Dim ItemOp As ItemOperations

    ItemOp = DTE.ItemOperations

    ext = ActiveDocument.FullName
    If ext = "" Then
    MsgBox("Error, not a .cpp or .h file")
    Exit Sub
    End If
    DocName = UCase(ext)
    pos = InStr(ext, ".")
    Do While pos > 1
    ext = Mid(ext, pos, (Len(ext) - pos + 1))
    pos = InStr(ext, ".")
    ext = LCase(ext)
    pos = InStr(DocName, ".")
    If ext <> ".cpp" And ext <> ".h" Then
    MsgBox("Error, not a .cpp or a .h file")
    Exit Sub
    End If
    If ext = ".cpp" Then
    fn = Left(DocName, pos) & "h"
    fn = Left(DocName, pos) & "cpp"
    End If
    'msgbox (fn)
    On Error Resume Next

    ItemOp.OpenFile(fn, Constants.vsViewKindAny)

    End Sub

    End Module

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • On-demand Event Event Date: December 3, 2014 Microsoft SQL Server includes a high availability option called AlwaysOn Availability Groups (AGs). In this session we will see how AGs are implemented using a Windows Server Failover Cluster and SQL Server 2014 with the Pure Storage FlashArray. We will showcase how to use Pure Storage snapshots to seed secondary database copies and then replicate to another location for backup or disaster recovery operations. Windows PowerShell and the Pure Storage PowerShell …

Most Popular Programming Stories

More for Developers

RSS Feeds