Skip to content

Commit 4cbe644

Browse files
committed
Modified Icon and Win32Registry to properly support icons with negative index. The api ExtractIconExW() supports negative index.
This effectively reverts changes made for #155.
1 parent 094a67f commit 4cbe644

5 files changed

Lines changed: 100 additions & 7 deletions

File tree

CHANGES

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
Changes for 0.10.0
22

3-
* Deprecated support for icons with negative resource id.
43
* Fixed issue #6 : (twice) Right-click on a directory with Windows Explorer in the left panel shows the menus twice.
54
* Fixed issue #31 : (twice) Error in logs for CContextMenu::GetCommandString()
65
* Fixed issue #109: Implement default and verbose logging.
76
* Fixed issue #150: ico icon (that do not specifically force index=0) are not working.
8-
* Fixed issue #155: Drop support for loading icons from a resource id (icons with a negative index smaller than -1).
97
* Fixed issue #157: Compilation fails on Github Action: `fatal error C1083: Cannot open include file: 'atlbase.h': No such file or directory`.
108
* Fixed issue #158: Compilation fails on Github Action: `CPack error : Problem running WiX.`.
119
* Fixed issue #159: Unit test fails on Github Actions: TestPlugins.testServices().

src/core/Icon.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ namespace shellanything
7676
{
7777
if (!mFileExtension.empty())
7878
return true;
79-
if (!mPath.empty() && mIndex >= 0) // not a resource id. See Issue #17, #150, #155
79+
80+
//See issue #17, 155, 164.
81+
//An icon with a negative index is valid from the registry.
82+
//Only the special case index = -1 should be considered invalid (Issue #17).
83+
//And ShellAnything accept positive (index) and negative index (resource id). (Issue #155, Issue #164).
84+
if (!mPath.empty() && mIndex != INVALID_ICON_INDEX)
8085
return true;
86+
8187
return false;
82-
}
88+
}
8389

8490
void Icon::ResolveFileExtensionIcon()
8591
{
@@ -100,7 +106,12 @@ namespace shellanything
100106

101107
//try to find the path to the icon module for the given file extension.
102108
Win32Registry::REGISTRY_ICON resolved_icon = Win32Registry::GetFileTypeIcon(file_extension.c_str());
103-
if (!resolved_icon.path.empty() && resolved_icon.index >= 0) // See Issue #17 #155. Do not accept icons which are resource ids.
109+
110+
//An icon with a negative index is valid from the registry.
111+
//Only the special case index = -1 should be considered invalid (Issue #17).
112+
//And ShellAnything accept positive (index) and negative index (resource id). (Issue #155, Issue #164).
113+
//See issue #17, 155, 164.
114+
if (Win32Registry::IsValid(resolved_icon))
104115
{
105116
//found the icon for the file extension
106117
//replace this menu's icon with the new information

src/shared/Win32Registry.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,13 @@ namespace Win32Registry
684684
{
685685
if (icon.path.empty())
686686
return false;
687-
if (icon.index == INVALID_ICON_INDEX) //See issue #17
687+
688+
//See issue #17, 155, 164.
689+
//An icon with a negative index is valid from the registry.
690+
//Only the special case index = -1 should be considered invalid (Issue #17).
691+
if (icon.index == INVALID_ICON_INDEX)
688692
return false;
693+
689694
if (IsIconEquals(icon, NULL_ICON))
690695
return false;
691696
return true;

src/tests/TestIcon.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace shellanything
133133
Icon icon;
134134
icon.SetPath("path/to/a/file.ico");
135135
icon.SetIndex(-99);
136-
ASSERT_FALSE(icon.IsValid());
136+
ASSERT_TRUE(icon.IsValid());
137137
}
138138
}
139139
//--------------------------------------------------------------------------------------------------
@@ -153,6 +153,22 @@ namespace shellanything
153153
ASSERT_TRUE(icon != Icon::GetDefaultUnknownFileTypeIcon());
154154
}
155155
//--------------------------------------------------------------------------------------------------
156+
TEST_F(TestIcon, testResolveFileExtensionIconHtml)
157+
{
158+
Icon icon;
159+
icon.SetFileExtension("html");
160+
161+
//act
162+
icon.ResolveFileExtensionIcon();
163+
164+
ASSERT_TRUE(icon.GetFileExtension().empty());
165+
ASSERT_FALSE(icon.GetPath().empty());
166+
ASSERT_NE(Icon::INVALID_ICON_INDEX, icon.GetIndex());
167+
168+
// assert we did not resolve to default unknown icon
169+
ASSERT_TRUE(icon != Icon::GetDefaultUnknownFileTypeIcon());
170+
}
171+
//--------------------------------------------------------------------------------------------------
156172
TEST_F(TestIcon, testGetDefaultUnknownFileTypeIcon)
157173
{
158174
Icon icon = Icon::GetDefaultUnknownFileTypeIcon();

src/tests/TestWin32Utils.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#pragma warning( pop )
2929

3030
#include "TestWin32Utils.h"
31+
#include "Icon.h"
3132
#include "Win32Utils.h"
3233
#include "ArgumentsHandler.h"
3334

@@ -652,6 +653,68 @@ namespace shellanything
652653
}
653654
}
654655
//--------------------------------------------------------------------------------------------------
656+
TEST_F(TestWin32Utils, testNegativeIconIndexIssue155) // Issue #155
657+
{
658+
Icon icon;
659+
660+
// On my system, with current implementation,
661+
// Icon::ResolveFileExtensionIcon() resolves HTML file extension to the following:
662+
icon.SetPath("C:\\Program Files\\Internet Explorer\\iexplore.exe");
663+
icon.SetIndex(-17);
664+
665+
// act, load an icon with negative index
666+
HBITMAP hBitmap = Win32Utils::LoadIconFromFileAsBitmap32Bpp(icon.GetPath().c_str(), icon.GetIndex());
667+
ASSERT_NE(hBitmap, Win32Utils::INVALID_BITMAP_HANDLE);
668+
669+
// build output files
670+
std::string test_dir = ra::filesystem::GetTemporaryDirectory();
671+
ASSERT_TRUE(ra::filesystem::CreateDirectory(test_dir.c_str()));
672+
std::string bitmap_filename = ra::testing::GetTestQualifiedName() + ".bmp";
673+
std::string output_path = test_dir + "\\" + bitmap_filename;
674+
675+
// remove data from previous runs
676+
ra::filesystem::DeleteFile(output_path.c_str());
677+
678+
// Save as *.bmp
679+
bool saved = Win32Utils::SaveBitmapFile(output_path.c_str(), hBitmap);
680+
ASSERT_TRUE(saved) << "Failed to save bitmap to file: " << output_path;
681+
682+
// Cleanup
683+
DeleteObject(hBitmap);
684+
}
685+
//--------------------------------------------------------------------------------------------------
686+
TEST_F(TestWin32Utils, testNegativeIconIndexIssue164) // Issue #164
687+
{
688+
Icon icon;
689+
690+
// On my system, Icon::ResolveFileExtensionIcon() resolves 'txt' file extension to a negative icon index
691+
icon.SetFileExtension("txt");
692+
icon.ResolveFileExtensionIcon();
693+
694+
ASSERT_TRUE(icon.IsValid());
695+
ASSERT_TRUE(icon.GetIndex() < 1);
696+
697+
// act, load an icon with negative index
698+
HBITMAP hBitmap = Win32Utils::LoadIconFromFileAsBitmap32Bpp(icon.GetPath().c_str(), icon.GetIndex());
699+
ASSERT_NE(hBitmap, Win32Utils::INVALID_BITMAP_HANDLE);
700+
701+
// build output files
702+
std::string test_dir = ra::filesystem::GetTemporaryDirectory();
703+
ASSERT_TRUE(ra::filesystem::CreateDirectory(test_dir.c_str()));
704+
std::string bitmap_filename = ra::testing::GetTestQualifiedName() + ".bmp";
705+
std::string output_path = test_dir + "\\" + bitmap_filename;
706+
707+
// remove data from previous runs
708+
ra::filesystem::DeleteFile(output_path.c_str());
709+
710+
// Save as *.bmp
711+
bool saved = Win32Utils::SaveBitmapFile(output_path.c_str(), hBitmap);
712+
ASSERT_TRUE(saved) << "Failed to save bitmap to file: " << output_path;
713+
714+
// Cleanup
715+
DeleteObject(hBitmap);
716+
}
717+
//--------------------------------------------------------------------------------------------------
655718

656719
} //namespace test
657720
} //namespace shellanything

0 commit comments

Comments
 (0)