Click here to Skip to main content
15,118,573 members
Articles / Desktop Programming / ATL
Posted 9 May 2000


54 bookmarked

Copy Path Context Menu Extension

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
9 May 2000
A context menu shell extension that allows you to copy full file paths to the clipboard.
  • Download source files - 270 Kb
  • Sample Image - CopyPathExt.jpg


    As a Windows programmer, one of the big headaches for me has been referencing fully qualified file paths. A file buried deep within the filesystem becomes a bear to deal with, as neither I nor any other programmers I know want to manually type


    time after time. This annoyance sent me on my initial foray into the land of context menu extensions, in the summer of 1997. I initially implemented this context menu extension as a MFC Dll, which got the job done, but it certainly wasn't pretty. Later, in May of '99, in the process of trying to familiarize myself with the ATL and the STL, I realized that I could make a much cleaner and more efficient version using ATL. So that's just what I did.

    This extension may seem to be derivative of Glenn Carr's recent article "Shell Extension to Copy Full Filename Path" (See the Article), but was developed independently (aside from some last-minute changes to GetCommandString to correctly deal with Unicode). Mine is simply another way to do pretty much the same thing, with some extras. I decided to post this alternative because it adds a few programmer-friendly features. There has also been a PowerToy in existence for quite a while that adds an option to the "Send To" menu allowing you to do basically the same thing, but once again, I thought that this implementation adds enough to make it worth posting.

    One hassle when dealing with file paths in C or C++ (or other similar languages, such as Perl) is the way that these languages deal with literal strings. They treat the backslash character as an escape sequence to insert special characters into the string (e.g "\n" for newline). In order to insert a single backslash into a literal string, two backslashes must be included in the code (e.g. "C:\\config.sys"). One can imagine how this would be a headache when dealing with a file that resides deep within a jumble of subdirectories. Another problem when dealing with file paths arises when dealing with legacy applications that don't take too kindly to long filenames or filenames with spaces.

    In addition to simply copying a file path to the clipboard, this extension offers functionality to remedy the above problems. By simply holding down the Control key while selecting the menu item, the "C-Friendly" path will be copied to the clipboard (e.g. "C:\\Windows\\System32\\user32.dll"). Holding down the Shift key will copy the short "DOS-Friendly" path (e.g. "C:\Progra~1\Multim~1\sound.wav"). And should the desire strike you, you can even hold down both with predictable results. The extension also sports multiple selection capability, file and/or directory selections, as well as anti-lock brakes and a no-scratch finish. Also, as Glenn Carr mentions in his article, with no programming effort on my part, running the extension on a shortcut copies the shortcut's target path to the clipboard.


    The code consists of an ATL DLL project with a single class, CCopyPathContextMenu. Like all context menus, it implements the IContextMenu and IShellExtInit interfaces. It also supports the IContextMenu2 and IContextMenu3 interfaces, but does not use their added functionality.

    • IShellExtInit::Initialize - Culls the selected file and directory names from the OLE data object and adds them to a STL list of basic strings.
    • IContextMenu::QueryContextMenu - Adds the new menu item to the context menu.
    • IContextMenu::GetCommandString - Provides help text for Explorer to display on the status bar.
    • IContextMenu::InvokeCommand - Called when the user selects our menu item, copying the directory and file names from the STL list filled in the Initialize function to the clipboard.

    Build Versions and Registering

    There are several build configurations you need to consider in the project:
    • bin\ReleaseUMinDependency\ - Unicode
    • bin\DebugU\ - Unicode
    • bin\ReleaseMinDependency\
    • bin\ReleaseMinSize\
    • bin\Debug\
    • bin\ReleaseUMinSize\ - Unicode

    To register the file, simply select the appropriate version of the DLL (remember that Unicode builds will not work correctly on Win95/98), copy it to an appropriate location (Windows System Directory recommended), and run "RegSvr32 CopyPathExt.dll" from the command line. The item "Copy Path to Clipboard" will now appear when you right-click on files or directories!


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Nick Carruthers
    United States United States
    <A HREF=>Nicks's Web Page - My wild and crazy home on the web.

    Read the new issue of Modern Advances in Medicine (MAIM)!

    <A HREF=>Hearts of Gold - Check out a silly game I wrote for the Macintosh (boo hiss) back in the winter of 1995.

    Comments and Discussions

    Question64-bit compatability? Pin
    Noob7667586755-Learning SQL + C#, Very Grate31-Mar-14 3:49
    MemberNoob7667586755-Learning SQL + C#, Very Grate31-Mar-14 3:49 
    GeneralPerfect Pin
    michaelsheehy6-Jul-09 21:14
    Membermichaelsheehy6-Jul-09 21:14 
    GeneralProblems on Win2008 Pin
    David Horner25-Jun-08 3:24
    MemberDavid Horner25-Jun-08 3:24 
    GeneralRe: Problems on Win2008 Pin
    David Horner1-Jul-08 15:26
    MemberDavid Horner1-Jul-08 15:26 
    GeneralRe: Problems on Win2008 Pin
    Gautam Jain10-Sep-08 6:05
    MemberGautam Jain10-Sep-08 6:05 
    GeneralRe: Problems on Win2008 Pin
    David Horner10-Sep-08 20:17
    MemberDavid Horner10-Sep-08 20:17 
    Generalcontext menu within start menu bug Pin
    Brivolbm7q17-Aug-07 7:38
    MemberBrivolbm7q17-Aug-07 7:38 
    GeneralHowto compile Copy Path Shell Extension in VS2005 Pin
    Jaguas24-Feb-07 10:41
    MemberJaguas24-Feb-07 10:41 
    GeneralThis is in Vista Pin
    IAteTheWholeThing26-Dec-06 23:52
    MemberIAteTheWholeThing26-Dec-06 23:52 
    GeneralRe: This is in Vista Pin
    Jeffrey Walton11-Dec-06 16:41
    MemberJeffrey Walton11-Dec-06 16:41 
    GeneralRe: This is in Vista [modified] Pin
    Kaz Baygan26-Jan-09 18:26
    MemberKaz Baygan26-Jan-09 18:26 
    GeneralThis extension is awesome Pin
    jeng111127-Sep-06 10:25
    Memberjeng111127-Sep-06 10:25 
    GeneralCopy Path Context Menu Extension Pin
    kallicat8-Sep-06 8:37
    Memberkallicat8-Sep-06 8:37 
    QuestionDLL conflict with MS Image Preview Pin
    Osborn19-Jan-06 3:55
    MemberOsborn19-Jan-06 3:55 
    QuestionUNC names? Pin
    jeed1-Dec-03 2:44
    Memberjeed1-Dec-03 2:44 
    QuestionWhy include a typelibrary? Pin
    pmndr15-Nov-03 17:16
    Memberpmndr15-Nov-03 17:16 
    GeneralPath-related problem Pin
    Eric at Cognos1-May-03 8:03
    MemberEric at Cognos1-May-03 8:03 
    GeneralRe: Path-related problem Pin
    EricT.13-May-04 3:36
    MemberEricT.13-May-04 3:36 
    GeneralI like it Pin
    Philippe Lhoste4-Dec-02 5:28
    MemberPhilippe Lhoste4-Dec-02 5:28 
    GeneralDragQueryFile with Shortcut Problem Pin
    Brad Brilliant1-Nov-00 5:11
    MemberBrad Brilliant1-Nov-00 5:11 
    GeneralRe: DragQueryFile with Shortcut Problem Pin
    Nick Carruthers6-Nov-00 5:01
    MemberNick Carruthers6-Nov-00 5:01 
    GeneralRe: DragQueryFile with Shortcut Problem Pin
    Oleg Krivtsov12-Dec-05 3:32
    MemberOleg Krivtsov12-Dec-05 3:32 
    GeneralRe: DragQueryFile with Shortcut Problem Pin
    m.vinod8512-Jan-10 12:19
    Memberm.vinod8512-Jan-10 12:19 
    GeneralThere's another extension like this Pin
    samzor14-May-00 21:32
    Membersamzor14-May-00 21:32 
    GeneralRe: There's another extension like this Pin
    Nick Carruthers15-May-00 4:03
    MemberNick Carruthers15-May-00 4:03 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.