ReactOS 0.4.16-dev-1946-g52006dd
ExtractIconEx.cpp File Reference
#include "shelltest.h"
#include <stdio.h>
Include dependency graph for ExtractIconEx.cpp:

Go to the source code of this file.

Classes

struct  EXTRACTICONTEST
 

Functions

EXTERN_C BOOL WINAPI SHAreIconsEqual (HICON hIcon1, HICON hIcon2)
 
static void SafeDestroyIcon (HICON hIco)
 
static UINT GetIcoSize (HICON hIco)
 
BOOL FileExists (LPCSTR FileName)
 
BOOL ResourceToFile (INT i, LPCSTR FileName)
 
VOID RunExtractIconTest (EXTRACTICONTEST *Test)
 
 START_TEST (ExtractIconEx)
 
static HRESULT SHDEI (LPCWSTR pszIconFile, int Index=0, UINT GIL=0, UINT Size=0)
 
 START_TEST (SHDefExtractIcon)
 

Variables

EXTRACTICONTEST IconTests []
 

Function Documentation

◆ FileExists()

BOOL FileExists ( LPCSTR  FileName)

Definition at line 40 of file ExtractIconEx.cpp.

41{
42 FILE *fp = NULL;
43 bool exists = FALSE;
44
45 fp = fopen(FileName, "r");
46 if (fp != NULL)
47 {
48 exists = TRUE;
49 fclose(fp);
50 }
51 return exists;
52}
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)

Referenced by ResourceToFile().

◆ GetIcoSize()

static UINT GetIcoSize ( HICON  hIco)
static

Definition at line 20 of file ExtractIconEx.cpp.

21{
23 if (!GetIconInfo(hIco, &info))
24 return 0;
25
26 BITMAP bm;
27 if (!GetObject(info.hbmColor ? info.hbmColor : info.hbmMask, sizeof(bm), &bm))
28 bm.bmWidth = 0;
29 DeleteObject(info.hbmMask);
30 DeleteObject(info.hbmColor);
31 return bm.bmWidth;
32}
pKey DeleteObject()
Definition: bl.h:1331
#define GetObject
Definition: wingdi.h:4914
BOOL WINAPI GetIconInfo(_In_ HICON, _Out_ PICONINFO)
Definition: cursoricon.c:2397

Referenced by SHDEI().

◆ ResourceToFile()

BOOL ResourceToFile ( INT  i,
LPCSTR  FileName 
)

Definition at line 54 of file ExtractIconEx.cpp.

55{
56 FILE *fout;
57 HGLOBAL hData;
58 HRSRC hRes;
59 LPVOID lpResLock;
60 UINT iSize;
61
63 {
64 skip("'%s' already exists. Exiting now\n", FileName);
65 return FALSE;
66 }
67
69 if (hRes == NULL)
70 {
71 skip("Could not locate resource (%d). Exiting now\n", i);
72 return FALSE;
73 }
74
75 iSize = SizeofResource(NULL, hRes);
76
77 hData = LoadResource(NULL, hRes);
78 if (hData == NULL)
79 {
80 skip("Could not load resource (%d). Exiting now\n", i);
81 return FALSE;
82 }
83
84 // Lock the resource into global memory.
85 lpResLock = LockResource(hData);
86 if (lpResLock == NULL)
87 {
88 skip("Could not lock resource (%d). Exiting now\n", i);
89 return FALSE;
90 }
91
92 fout = fopen(FileName, "wb");
93 fwrite(lpResLock, iSize, 1, fout);
94 fclose(fout);
95 return TRUE;
96}
BOOL FileExists(LPCSTR FileName)
#define skip(...)
Definition: atltest.h:64
HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
Definition: res.c:176
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int UINT
Definition: ndis.h:50
#define RT_RCDATA
Definition: pedump.c:372
size_t fwrite(const void *, size_t, size_t, FILE *)
Definition: file.c:3077
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582

Referenced by START_TEST().

◆ RunExtractIconTest()

VOID RunExtractIconTest ( EXTRACTICONTEST Test)

Definition at line 117 of file ExtractIconEx.cpp.

118{
119 UINT nReturnedIcons, nExtractedIcons;
120
121 /* Check count of icons returned */
122 nReturnedIcons = ExtractIconExW(Test->pszFilePath, -1, NULL, NULL, 0);
123 ok(nReturnedIcons == Test->nIcons, "ExtractIconExW(L\"%S\"): Expects %u icons, got %u\n", Test->pszFilePath, Test->nIcons, nReturnedIcons);
124
125 /* Check if the 0th icon can be extracted successfully */
126 nExtractedIcons = ExtractIconExW(Test->pszFilePath, 0, NULL, NULL, 1);
127 ok(nExtractedIcons == Test->nIcons, "ExtractIconExW(L\"%S\"): Expects %u icons, got %u\n", Test->pszFilePath, Test->nIcons, nExtractedIcons);
128}
#define ok(value,...)
Definition: atltest.h:57
UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIcons)
Definition: iconcache.cpp:855

Referenced by START_TEST().

◆ SafeDestroyIcon()

static void SafeDestroyIcon ( HICON  hIco)
static

Definition at line 14 of file ExtractIconEx.cpp.

15{
16 if (hIco)
17 DestroyIcon(hIco);
18}
BOOL WINAPI DestroyIcon(_In_ HICON)
Definition: cursoricon.c:2405

Referenced by SHDEI(), and START_TEST().

◆ SHAreIconsEqual()

EXTERN_C BOOL WINAPI SHAreIconsEqual ( HICON  hIcon1,
HICON  hIcon2 
)

Definition at line 48 of file rosordinal.c.

49{
50 ICONINFO iconInfo1, iconInfo2;
51 BITMAP bm1, bm2;
52 BOOL bSame = FALSE;
53
54 if (!hIcon1 || !hIcon2)
55 return FALSE;
56
57 if (!GetIconInfo(hIcon1, &iconInfo1))
58 return FALSE;
59
60 if (!GetIconInfo(hIcon2, &iconInfo2))
61 {
62 DeleteObject(iconInfo1.hbmColor);
63 DeleteObject(iconInfo1.hbmMask);
64 return FALSE;
65 }
66
67 GetObjectW(iconInfo1.hbmColor, sizeof(bm1), &bm1);
68 GetObjectW(iconInfo2.hbmColor, sizeof(bm2), &bm2);
69
70 if (bm1.bmWidth == bm2.bmWidth && bm1.bmHeight == bm2.bmHeight)
71 {
72 BITMAPINFO bmi = { { sizeof(bmi), bm1.bmWidth, bm1.bmHeight, 1, 32 } };
73 HDC hdc = GetDC(0);
74 SIZE_T size = bm1.bmWidth * bm1.bmHeight * 4;
75 BYTE *data1, *data2;
76
79
80 if (data1 && data2)
81 {
82 if (GetDIBits(hdc, iconInfo1.hbmColor, 0, bm1.bmHeight, data1, &bmi, DIB_RGB_COLORS) &&
83 GetDIBits(hdc, iconInfo2.hbmColor, 0, bm2.bmHeight, data2, &bmi, DIB_RGB_COLORS))
84 {
85 bSame = memcmp(data1, data2, size) == 0;
86 }
87 }
90
92 }
93
94 DeleteObject(iconInfo1.hbmColor);
95 DeleteObject(iconInfo1.hbmMask);
96
97 DeleteObject(iconInfo2.hbmColor);
98 DeleteObject(iconInfo2.hbmMask);
99
100 return bSame;
101}
static HICON hIcon2
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizeiptr size
Definition: glext.h:5919
HDC hdc
Definition: main.c:9
static HDC
Definition: imagelist.c:88
HBITMAP hbmColor
Definition: winuser.h:3229
HBITMAP hbmMask
Definition: winuser.h:3228
Definition: tftpd.h:126
Definition: tftpd.h:138
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define DIB_RGB_COLORS
Definition: wingdi.h:367
int WINAPI GetObjectW(_In_ HANDLE h, _In_ int c, _Out_writes_bytes_opt_(c) LPVOID pv)
int WINAPI GetDIBits(_In_ HDC hdc, _In_ HBITMAP hbm, _In_ UINT start, _In_ UINT cLines, _Out_opt_ LPVOID lpvBits, _At_((LPBITMAPINFOHEADER) lpbmi, _Inout_) LPBITMAPINFO lpbmi, _In_ UINT usage)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HDC WINAPI GetDC(_In_opt_ HWND)
unsigned char BYTE
Definition: xxhash.c:193

Referenced by START_TEST().

◆ SHDEI()

static HRESULT SHDEI ( LPCWSTR  pszIconFile,
int  Index = 0,
UINT  GIL = 0,
UINT  Size = 0 
)
static

Definition at line 175 of file ExtractIconEx.cpp.

176{
177 HICON hIco = NULL;
178 HRESULT hr = SHDefExtractIcon(pszIconFile, Index, GIL, &hIco, NULL, Size);
179 if (hr == S_OK)
180 {
181 hr = GetIcoSize(hIco);
182 SafeDestroyIcon(hIco);
183 }
184 return hr;
185}
static UINT GetIcoSize(HICON hIco)
static void SafeDestroyIcon(HICON hIco)
#define S_OK
Definition: intsafe.h:52
static HICON
Definition: imagelist.c:80
HRESULT hr
Definition: shlfolder.c:183
#define SHDefExtractIcon
Definition: shlobj.h:2306
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539

Referenced by START_TEST().

◆ START_TEST() [1/2]

START_TEST ( ExtractIconEx  )

Definition at line 130 of file ExtractIconEx.cpp.

131{
132 UINT i;
133 CHAR FileName[2][13] = { "ROS.ico", "sysicon.ico" };
134 EXTRACTICONTEST explorer_exe = {L"%SystemRoot%\\explorer.exe", 0};
135
136 if (!ResourceToFile(2, FileName[0]))
137 return;
138 if (!ResourceToFile(3, FileName[1]))
139 return;
140
141 /* Run normal tests */
142 for (i = 0; i < _countof(IconTests); ++i)
144
145 /* Run special case checks */
146 switch (GetNTVersion())
147 {
149 explorer_exe.nIcons = 18;
150 break;
152 explorer_exe.nIcons = 23;
153 break;
155 explorer_exe.nIcons = 25;
156 break;
159 explorer_exe.nIcons = 24;
160 break;
162 explorer_exe.nIcons = 28;
163 break;
164 }
165
166 if (explorer_exe.nIcons)
167 RunExtractIconTest(&explorer_exe);
168 else
169 skip("Unknown NT Version: 0x%lX\n", GetNTVersion());
170
173}
BOOL ResourceToFile(INT i, LPCSTR FileName)
VOID RunExtractIconTest(EXTRACTICONTEST *Test)
EXTRACTICONTEST IconTests[]
#define GetNTVersion()
Definition: apitest.h:17
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
#define L(x)
Definition: resources.c:13
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32
#define _WIN32_WINNT_WS03
Definition: sdkddkver.h:23
#define _WIN32_WINNT_WINBLUE
Definition: sdkddkver.h:30
#define _WIN32_WINNT_WIN8
Definition: sdkddkver.h:29
#define _WIN32_WINNT_WIN7
Definition: sdkddkver.h:28
#define _WIN32_WINNT_VISTA
Definition: sdkddkver.h:25
#define _countof(array)
Definition: sndvol32.h:70
char CHAR
Definition: xmlstorage.h:175

◆ START_TEST() [2/2]

START_TEST ( SHDefExtractIcon  )

Definition at line 187 of file ExtractIconEx.cpp.

188{
189 HRESULT hr;
190 int SysBigIconSize = GetSystemMetrics(SM_CXICON);
191
192 // Modern Windows requires the system image list to be initialized for GIL_SIMULATEDOC to work!
193 SHFILEINFOW shfi;
194 SHGetFileInfoW(L"x", 0, &shfi, sizeof(shfi), SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
195
198 PathAppendW(path, L"user32.dll");
199 int index = 1;
200
201 ok(SHDEI(path, index, 0, 0) == SysBigIconSize, "0 size must match GetSystemMetrics\n");
202 ok(SHDEI(path, index, 0, SysBigIconSize * 2) == SysBigIconSize * 2, "Resize failed\n");
203
204 HICON hIcoLarge, hIcoSmall;
205 if (SHDefExtractIcon(path, index, 0, &hIcoLarge, &hIcoSmall, 0) != S_OK)
206 hIcoLarge = hIcoSmall = NULL;
207 ok(hIcoLarge && hIcoSmall && !SHAreIconsEqual(hIcoLarge, hIcoSmall), "Large+Small failed\n");
208 SafeDestroyIcon(hIcoLarge);
209 SafeDestroyIcon(hIcoSmall);
210
211 static const int sizes[] = { 0, SysBigIconSize * 2 };
212 for (UINT i = 0; i < _countof(sizes); ++i)
213 {
214 HICON hIcoNormal, hIcoSimDoc;
215 if (FAILED(hr = SHDefExtractIcon(path, index, 0, &hIcoNormal, NULL, sizes[i])))
216 hIcoNormal = NULL;
217 if (FAILED(hr = SHDefExtractIcon(path, index, GIL_SIMULATEDOC, &hIcoSimDoc, NULL, sizes[i])))
218 hIcoSimDoc = NULL;
219 ok(hIcoNormal && hIcoSimDoc && !SHAreIconsEqual(hIcoNormal, hIcoSimDoc), "GIL_SIMULATEDOC failed\n");
220 SafeDestroyIcon(hIcoNormal);
221 SafeDestroyIcon(hIcoSimDoc);
222 }
223
225 GetTempFileNameW(path, L"TEST", 0, path);
226 ok(SHDEI(path) == S_FALSE, "Empty file should return S_FALSE\n");
229 {
230 DWORD io;
231 WriteFile(hFile, "!", 1, &io, NULL);
233 ok(SHDEI(path) == S_FALSE, "File without icons should return S_FALSE\n");
234 }
236}
static HRESULT SHDEI(LPCWSTR pszIconFile, int Index=0, UINT GIL=0, UINT Size=0)
EXTERN_C BOOL WINAPI SHAreIconsEqual(HICON hIcon1, HICON hIcon2)
Definition: rosordinal.c:48
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2080
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2313
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint index
Definition: glext.h:6031
#define FAILED(hr)
Definition: intsafe.h:51
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK io
Definition: file.c:100
static const struct @594 sizes[]
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define GENERIC_WRITE
Definition: nt_native.h:90
#define PathAppendW
Definition: pathcch.h:310
DWORD_PTR WINAPI SHGetFileInfoW(LPCWSTR path, DWORD dwFileAttributes, SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags)
Definition: shell32_main.c:430
#define SHGFI_SYSICONINDEX
Definition: shellapi.h:171
#define SHGFI_USEFILEATTRIBUTES
Definition: shellapi.h:181
#define DeleteFile
Definition: winbase.h:3513
#define S_FALSE
Definition: winerror.h:3451
#define SM_CXICON
Definition: winuser.h:983
int WINAPI GetSystemMetrics(_In_ int)
__wchar_t WCHAR
Definition: xmlstorage.h:180

Variable Documentation

◆ IconTests

EXTRACTICONTEST IconTests[]
Initial value:
=
{
{L"%SystemRoot%\\System32\\cmd.exe", 1},
{L"%SystemRoot%\\System32\\autochk.exe", 0},
{L"%SystemRoot%\\non-existent-file.sdf", 0},
{L"sysicon.ico", 1},
{L"ROS.ico", (UINT)(GetNTVersion() >= _WIN32_WINNT_VISTA ? 1 : 0)}
}

Definition at line 98 of file ExtractIconEx.cpp.

Referenced by START_TEST().