When you set Windows Explorer to display folders in the
Windows Classic mode, the
SelectedItems method of the
IShellFolderViewDual interface returns an incorrect set of
FolderItems .
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
Steps to Reproduce the Behavior
- Open a text editor such as Notepad, paste the following code into a new file, and then save the file as Sample.cpp:
#include <Windows.h>
#include <TChar.h>
#import <SHDocVw.dll>
#import <Shell32.dll>
#include <iostream>
using namespace std;
int _tmain(int argc, TCHAR ** argv)
{
CoInitialize(0);
try
{
SHDocVw::IShellWindowsPtr sw(__uuidof(SHDocVw::ShellWindows));
for (long i = 0; i < sw->Count; ++i)
{
if (SHDocVw::IWebBrowser2Ptr ie = sw->Item(i))
{
cout << _com_util::ConvertBSTRToString(ie->LocationURL) << endl;
if (Shell32::IShellFolderViewDualPtr view = ie->Document)
{
cout << view->SelectedItems()->Count << _T(" selected item(s)") << endl;
}
}
}
}
catch (_com_error const & e)
{
cout << e.ErrorMessage() << endl;
}
CoUninitialize();
return 0;
} - At a command prompt, compile the file by running the following command:cl /EHsc Sample.cpp
- Close all the Windows Explorer windows that are currently open.
- Right-click Start, and then click
Explore to open a Windows Explorer window.
- On the Tools menu, click Folder
Options
- On theGeneral tab, select the Use Windows classic folders option under Web View, and then click OK.
- Select a folder
that contains many items. In the right pane, select some of the items in this folder.
- At a command prompt, run the Sample.exe executable (that you generated in step 2).
The output
displays the result of the Count property of the FolderItems instance that is returned by the SelectedItems method of IShellFolderViewDual interface.
You expect the output to be the number of items that you
have selected in the folder in Windows Explorer from the earlier step. However, the output contains the number of
all the items in the folder that was selected in Windows Explorer. - When Windows Explorer is inWeb view mode, the folder items
that are returned by SelectedItems is correctly set to the FolderItem instances that are selected.
To verify the previous statement in Windows Explorer, on
the Tools menu, click Folder options. - On the General tab, click the Enable Web content on
my desktop option under Web View, and then click
OK to close the dialog box.
- At a command prompt, run Sample.exe (generated in step 2).
The output
displays the result of the Count property of the FolderItems instance that is returned by the SelectedItems method of IShellFolderViewDual interface.
The output is, as expected, the number of items that you had
selected in the folder.
Alternatively, you can use the
FocusedItem method of the
IShellFolderViewDual interface to replace the incorrect
functionality of the
SelectedItems method. The
FocusedItem method returns the currently selected item in the folder and restricts your application to a single folder selection in Windows Explorer.
For additional information about the Shell programming guide, see the following MSDN documentation: