This article was contributed by Glenn Carr.
A single file selection...
Multiple files selected...
Environment: VC6 SP3, NT4 SP5, IE5
There numerous times that I've wished I could simply right-click on a file, and copy its full pathname in order to paste it into an e-mail or a file open dialog. I finally decided to write it myself and thought someone else might find it useful. The extension adds a 'Copy Path' command to the context menu for a single file/folder selection and a 'Copy All Paths' command for multi-file/folder selection. For a single selection the complete pathname of the file is simply placed on the clipboard. For a multi-file selection, all the paths of the files are placed on the clipboard separated by <cr><lf> pairs.
One unexpected 'feature' is that you can right-click on a shortcut and the path of the target will be copied to the clipboard. Of course, the shortcut can be anywhere, including shell menu items if the IE4 active desktop option is installed.
Some of the code was based on code written by Dino Esposito from his book, Visual C++ Window Shell Programming (Wrox). This is an excellent resource for shell programming. I've run across a few things that his book didn't cover and found a bug related to a newer version of IE (I think).
This has been tested only on NT4 SP5 with IE5, and 95 without IE.
This is not a tutorial on shell extensions. I'll just give a brief overview of the requirements for a shell extension with some specifics about this example. There are several good resources, one of the best of which is the book I mentioned earlier by Dino Esposito, Visual C++ Window Shell Programming. Chris Losinger also has an example, Shell Extension Context Menu Sample at CodeGuru.
I started building this component by simply creating an ATL COM project, and then adding a simple ATL object. I then added the necessary interfaces and their implementations. A shell extension requires implementation of two interfaces, IShellExtInit and IContextMenu.
IShellExtInit has only one method, Initialize which is used to get the name of the files that have been selected (this extension only gets the name of the file with the current focus, although it could be expanded to be disabled if multiple files were selected, or to put all the paths on the clipboard.)
IContextMenu has three methods to implement, GetCommandString, InvokeCommand, and QueryContextMenu.
I had a bit of trouble getting this to work correctly and I think it has to do with most recent IE installation. Following is the code for GetCommandString:
// IContextMenu::GetCommandString HRESULT CCopyPathExt::GetCommandString(UINT /*idCmd*/, UINT uFlags, UINT* /*pwReserved*/, LPSTR pszText, UINT cchMax) { try { USES_CONVERSION; // We don't care about the command ID, since we have a // single item // This is rather funky, but GCS_HELPTEXTA refers to a // single bit (4) and GCS_UNICODE refers to single bit // (1), but GCS_HELPTEXTW is a combination // of those values (5). This works on both NT and 95. if ( uFlags & GCS_HELPTEXTA ) { // I'm assuming that this value always contains the number // of bytes available for the message, just to be safe. // So half it for Unicode characters. const int cChars = cchMax / sizeof(TCHAR); // Create some storage on the stack for building the // help text LPTSTR szHelpText = reinterpret_cast( _alloca( cChars ) ); memset( szHelpText, 0, cChars ); // Create a generic version of the help text depending on // the number of file selected if ( 1 == m_cFiles ) { _sntprintf( szHelpText, cChars - 1, _T("Copy full path of file to clipboard (%s)"), m_szFile); } else { const LPTSTR szMultiFileHelpText = _T("Copy full paths of selected files to clipboard"); _tcsncpy( szHelpText, szMultiFileHelpText, min(cChars - 1, _tcslen( szMultiFileHelpText ) ) ); } // Do character conversion depending on flag if ( uFlags & GCS_UNICODE ) { wcsncpy( reinterpret_cast(pszText), T2W(szHelpText), cChars ); } else { strncpy(pszText, T2A(szHelpText), cchMax); } } } catch ( ... ) { return E_FAIL; } return S_OK; }
Most examples simply test for the bits GCS_HELPTEXTA or GCS_HELPTEXTW, but the value of GCS_HELPTEXTW is actually 5, not a single bit value, which is also equal to GSC_HELPTEXTA|GCS_UNICODE. So, I simply test for GCS_HELPTEXTA and then test for the GCS_UNICODE bit. I also assume that if GetCommandString requests a Unicode string, then the max length of the string is half of what it would normally be. This may be incorrect, but it's a safe guess.
Add Comment
INSTANT EXPERTS!Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,. Learn about Free & Open Source software at the Bazaar Conference & Expo!,December 14-16, 1999 in New York City! Click here for more information. For tons of Windows technology resources, check out the Windows Programming directory. Go to Fatbrain for the NEWEST and BEST technical books for IT professionals. Get Linux Utilities for ONLY $34.95 at EarthWeb Direct.