Click here to Skip to main content
Click here to Skip to main content

Copy Path Context Menu Extension

By , 9 May 2000
 
  • Download source files - 270 Kb
  • Sample Image - CopyPathExt.jpg

    Introduction

    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

    "C:\Windows\System32\Drivers\SomeDriver\AnotherSubFolder\YetAnother\Will-It-Ever-End\MyFile.txt"

    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.

    Code

    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!

    License

    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
    Member
    Nicks's Web Page - My wild and crazy home on the web.
     
    Read the new issue of Modern Advances in Medicine (MAIM)!
     
    Hearts of Gold - Check out a silly game I wrote for the Macintosh (boo hiss) back in the winter of 1995.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralPerfectmembermichaelsheehy6 Jul '09 - 20:14 
    Many thanks, just what I needed!
    GeneralProblems on Win2008memberDavid Horner25 Jun '08 - 2:24 
    I'm running WS2008 x64 and the context menus will not show up. I've downloaded the code and built release mindep. Then it registers fine...but nothing shows up when right clicking in explorer! I'm trying to debug now...
    GeneralRe: Problems on Win2008memberDavid Horner1 Jul '08 - 14:26 
    I've confirmed all the registry entries are there. All three for the shellex stuff. Then the guid entries for std. COM registration.
     
    The only thing I can think is that maybe it is because of the 32bit dll on the 64bit os. I've tried to compile using x64 and for some reason I get:
     
    Error 2 error C2259: 'ATL::CComObject<base>' : cannot instantiate abstract class C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\include\atlcom.h 1801 CopyPathExt
     
    What gives!
     
    --Dave </base>
    GeneralRe: Problems on Win2008memberGautam Jain10 Sep '08 - 5:05 
    This happens because some of the interface's function arguments have changed little bit. For example: UINT to UINT_PTR.
     
    Check GetCommandString function. Its first parameter has changed from UINT to UINT_PTR.
     
    The compiler thinks that the function is not implemented hence the error.
     
    Thanks.
     
    Regards,
    Gautam Jain
    http://www.conceptworld.com

    GeneralRe: Problems on Win2008memberDavid Horner10 Sep '08 - 19:17 
    You are the man! Thanks so much. I'm very happy to finally have this wonderful shell extension on my win2008 x64 machine now. I made the change to the declaration and definition and everything compiled fine. The component registered correctly and provides the additional menu.
     
    Thanks again!
    --Dave
    http://dave.thehorners.com/[^]
    Generalcontext menu within start menu bugmemberBrivolbm7q17 Aug '07 - 6:38 
    I really like this extension - saves a lot of time. But since I installed I can't edit my start menu by right clicking on a start menu folder and clicking Open, Explore, Open All Users, or Explore All Users.
     
    Removing this registry key solves the problem, but removes all functionality of CopyPathExt on folders within explorer.
    [HKEY_CLASSES_ROOT\Directory\shellex\ContextMenuHandlers\CopyPathExt]
     

    GeneralHowto compile Copy Path Shell Extension in VS2005memberphm3724 Feb '07 - 9:41 
    Add "_ATL_NO_UUIDOF" to the Preprocessor definitions of the "CopyPathExt" project. Read here why this works:
     
    http://www.kbalertz.com/192561/Error.C2787.Building.Project.Using.aspx
    GeneralThis is in VistamemberIAteTheWholeThing26 Dec '06 - 22:52 
    This functionality is in Vista now. SHIFT+RIGHT-CLICK on filesystem objects to get the Copy Path menu item.
    GeneralRe: This is in VistamemberJeffrey Walton11 Dec '06 - 15:41 
    Only 7 years late...
    GeneralRe: This is in Vista [modified]memberKaz Baygan26 Jan '09 - 17:26 
    Vista does not do the extra stuff such as C style formatting etc..., does it?
     
    Baytek software
     
    modified on Monday, January 26, 2009 11:58 PM

    GeneralThis extension is awesomememberje0227 Sep '06 - 9:25 
    I can now copy paths that include Japanese characters in them. And there's no little 'hesitation' time when doing it, like there is with CopyThisPath.exe
     
    Hat's off!

    GeneralCopy Path Context Menu Extensionmemberkallicat8 Sep '06 - 7:37 
    Ok I downloaded the file now where do i put it???
    I am new to learning how to make a web page and this sounds like it will make things so much easier but i don't know where to put it on my computer. i have it in my c/program files. where does it need to be so it will show up when i right click???
    Thank your for your help..

    QuestionDLL conflict with MS Image PreviewmemberOsborn19 Jan '06 - 2:55 
    I used this code as a template for my own shell extension and I've got a conflict.
     
    Try this:
    Register DLL -> Open Explorer and try to find a image file (e.g. bmp) and open this image with the Microsoft Image Preview (WinXP). And after this click on the button "Open image with Editingprogram, normally Paint would open the image now or maybe another installed image tool. But in this case it doesn't happen, instead of this the DLL is called. Why? I don't understand this. If you try to edit the image by clicking on the context menu item "Edit" this would not happen, only if you use the button within the MS Image Preview Program.
     
    With this delivered code you would not see it because nothing will happen, you can only see that no other program will be opened instead, like it supposed to be. But debug this by simply adding a msg box dialog when InvokeCommand of DLl is called and you'll see that this msg box would appear...
     
    I don't have a glue, anybode else does?
     
    Thanks a lot...
    QuestionUNC names?sussjeed1 Dec '03 - 1:44 
    How about adding the ability to use UNC style naming on mapped network drives?
     
    eg. \\fredbob\my_share\readme.txt
     
    though we're running out of key aren't we?
    QuestionWhy include a typelibrary?memberpoimander15 Nov '03 - 16:16 
    This is an excellent example of shell extension code. But I have a number of questions. Why is a typelibrary included as a resource? Is this necessary for marshalling purposes? Also, since the dispinterface methods aren't called by the shell in order for the extension to work, why is it useful to code a shell extension in that way? TIA.
    GeneralPath-related problemmemberEric at Cognos1 May '03 - 7:03 
    This is a great shell extension.
    Around the same time I installed this DLL (I didn't build it myself but took one out of the ZIP), we've been having trouble with services (on win2k) if their ImagePath registry entry has spaces in the path and no quotes around the executable name. Even though the Platform SDK stresses that quotes are needed in ImagePath if the executable name / path has spaces, there are even some Visual Studio services that don't follow this rule. The symptom is a cryptic Message Box showing text such as: Could not start the yadda-yadda service on Local Computer. Error 193: %1 is not a valid Win32 application.
    We have not been able to determine what caused win2k to suddenly require quotes around service paths. Uninstalling CopyPathExt.dll does not solve the problem. Does anybody have any ideas or suggestions?
    Thanks,
    Eric T.
    GeneralRe: Path-related problemsusserict200013 May '04 - 2:36 
    Found the problem Smile | :) Some unrelated task (don't know what) creates a file called Program into the root directory. This file is 0 bytes long. Because of this file, the file Programs takes precedence over the folder Program Files, and quotes are needed in services to insure the service can be started.
    GeneralI like itmemberPhilippe Lhoste4 Dec '02 - 4:28 
    I like the capability to make various style of copy without cluttering the context menu. Very nice.
     
    One suggestion: remove .aps, .ncb, .plg and .opt files from your distribution, it is already quite big without them. Well, giving the binary files is good, I could use the extension without firing up my Visual Studio...
     
    Thank you.
     
    Philippe Lhoste (Paris -- France)
    Professional programmer and amateur artist
    http://jove.prohosting.com/~philho/

    GeneralDragQueryFile with Shortcut ProblemsussBrad Brilliant1 Nov '00 - 4:11 
    Thanks for the great example. When I do a multiple select in which one of the selected items is a shortcut and right click to bring the context menu the code calls Initialize which calls DragQueryFile to get the number of files. If I right click on a non-shortcut all works just fine. If I right click on the shortcut 1 is returned and when the pathname is extracted it turns out to be what the shortcut is pointing to. Any idea what goes wrong in DragQueryFile or how to work around this.

     
    Thanks,
     
    Brad
    GeneralRe: DragQueryFile with Shortcut ProblemmemberNick Carruthers6 Nov '00 - 4:01 
    Interesting. I had never noticed that before. Sure enough, DragQueryFile seems to think there is only one
    file selected when you right click on the shortcut. I knew that Windows would give you the target of a shortcut,
    but I didn't realize that it would behave like this with multiple selections. To be honest, it seems like a Windows bug,
    though if anyone can see something I'm doing wrong, please let me know. I guess the only workaround at the
    moment would be to make sure you right click on a non-shortcut. Not a solution, I know, but I don't really have
    any other suggestions.
     
    Nick Carruthers
    GeneralRe: DragQueryFile with Shortcut Problemmember[Oleg]12 Dec '05 - 2:32 
    I had the same problem with my context menu handler.
     
    You can forse explorer to send shortcut's name to your extension by registering it such way:
     
    HKCR\*\shellex\ContextMenuHandlers\YourExtensionName
    HKCR\lnkfile\shellex\ContextMenuHandlers\YourExtensionName
     
    or
     
    HKCR\AllFileSystemObjects\shellex\ContextMenuHandlers\YourExtensionName
    HKCR\lnkfile\shellex\ContextMenuHandlers\YourExtensionName
     
    It will work in Windows 2000/XP, but I have problem with such registration in Windows 95/98. When I right-click a shortcut in Windows 98, context menu appears, which has two identical menu items of my extension. Frown | :( I have not found the solution of that problem.
     

     
    OlegK
    GeneralRe: DragQueryFile with Shortcut Problemmemberm.vinod8512 Jan '10 - 11:19 
    How can we get the target path of the shortcut file if we have this registry set up.
    --
    Vinod
    GeneralThere's another extension like thissussSam Maguire14 May '00 - 20:32 
    Take a look at:
    http://www.codeproject.com/shell/copylocation.as
    GeneralRe: There's another extension like thissussNick Carruthers15 May '00 - 3:03 
    Yes, but this one adds the ability to copy C-style path ("C:\\Winnt\\System32\\Shell32.dll") and short paths ("C:\Progra~1\Norton~1\test.txt") or both.
     
    More than one way to skin a cat..
    GeneralRe: There's another extension like thismemberShaun Harrington31 Jul '06 - 10:28 
    I wrote one back in 96. It also allows you to open a command window on a folder, and provide a command line argument to a program.
     
    Here is the link to the article: http://www.codeproject.com/shell/cc100submit.asp
     
    And here us a link to SmartStartMenu that also includes the "maintained" version of the shell extensions: http://www.codeproject.com/shell/cc100submit.asp
     
    There is an XP Power Tool that does this too.
     
    Anyway, Great idea!
     

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

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130523.1 | Last Updated 10 May 2000
    Article Copyright 2000 by Nick Carruthers
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid