ReactOS 0.4.16-dev-814-g656a5dc
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  EXTRACTICONTESTS
 

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)
 
 START_TEST (ExtractIconEx)
 
static HRESULT SHDEI (LPCWSTR pszIconFile, int Index=0, UINT GIL=0, UINT Size=0)
 
 START_TEST (SHDefExtractIcon)
 

Variables

EXTRACTICONTESTS 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:4468
BOOL WINAPI GetIconInfo(_In_ HICON, _Out_ PICONINFO)
Definition: cursoricon.c:2131

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
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
unsigned int UINT
Definition: ndis.h:50
#define RT_RCDATA
Definition: pedump.c:372
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582

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:2139

Referenced by SHDEI(), and START_TEST().

◆ SHAreIconsEqual()

EXTERN_C BOOL WINAPI SHAreIconsEqual ( HICON  hIcon1,
HICON  hIcon2 
)

Definition at line 43 of file rosordinal.c.

44{
45 ICONINFO iconInfo1, iconInfo2;
46 BITMAP bm1, bm2;
47 BOOL bSame = FALSE;
48
49 if (!hIcon1 || !hIcon2)
50 return FALSE;
51
52 if (!GetIconInfo(hIcon1, &iconInfo1))
53 return FALSE;
54
55 if (!GetIconInfo(hIcon2, &iconInfo2))
56 {
57 DeleteObject(iconInfo1.hbmColor);
58 DeleteObject(iconInfo1.hbmMask);
59 return FALSE;
60 }
61
62 GetObjectW(iconInfo1.hbmColor, sizeof(bm1), &bm1);
63 GetObjectW(iconInfo2.hbmColor, sizeof(bm2), &bm2);
64
65 if (bm1.bmWidth == bm2.bmWidth && bm1.bmHeight == bm2.bmHeight)
66 {
67 BITMAPINFO bmi = { { sizeof(bmi), bm1.bmWidth, bm1.bmHeight, 1, 32 } };
68 HDC hdc = GetDC(0);
69 SIZE_T size = bm1.bmWidth * bm1.bmHeight * 4;
70 BYTE *data1, *data2;
71
74
75 if (data1 && data2)
76 {
77 if (GetDIBits(hdc, iconInfo1.hbmColor, 0, bm1.bmHeight, data1, &bmi, DIB_RGB_COLORS) &&
78 GetDIBits(hdc, iconInfo2.hbmColor, 0, bm2.bmHeight, data2, &bmi, DIB_RGB_COLORS))
79 {
80 bSame = memcmp(data1, data2, size) == 0;
81 }
82 }
85
87 }
88
89 DeleteObject(iconInfo1.hbmColor);
90 DeleteObject(iconInfo1.hbmMask);
91
92 DeleteObject(iconInfo2.hbmColor);
93 DeleteObject(iconInfo2.hbmMask);
94
95 return bSame;
96}
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:3130
HBITMAP hbmMask
Definition: winuser.h:3129
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 148 of file ExtractIconEx.cpp.

149{
150 HICON hIco = NULL;
151 HRESULT hr = SHDefExtractIcon(pszIconFile, Index, GIL, &hIco, NULL, Size);
152 if (hr == S_OK)
153 {
154 hr = GetIcoSize(hIco);
155 SafeDestroyIcon(hIco);
156 }
157 return hr;
158}
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:2300
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533

Referenced by START_TEST().

◆ START_TEST() [1/2]

START_TEST ( ExtractIconEx  )

Definition at line 120 of file ExtractIconEx.cpp.

121{
122 UINT i, nReturnedIcons, nExtractedIcons;
123 CHAR FileName[2][13] = { "ROS.ico", "sysicon.ico" };
124
125 if (!ResourceToFile(2, FileName[0]))
126 return;
127 if (!ResourceToFile(3, FileName[1]))
128 return;
129
130 /* Check count of icons returned */
131 for (i = 0; i < _countof(IconTests); ++i)
132 {
133 nReturnedIcons = ExtractIconExW(IconTests[i].pszFilePath, -1, NULL, NULL, 0);
134 ok(nReturnedIcons == IconTests[i].nIcons, "ExtractIconExW(%u): Expects %u icons, got %u\n", i, IconTests[i].nIcons, nReturnedIcons);
135 }
136
137 /* Check if the 0th icon can be extracted successfully */
138 for (i = 0; i < _countof(IconTests); ++i)
139 {
140 nExtractedIcons = ExtractIconExW(IconTests[i].pszFilePath, 0, NULL, NULL, 1);
141 ok(nExtractedIcons == IconTests[i].nIcons, "ExtractIconExW(%u): Expects %u icons, got %u\n", i, IconTests[i].nIcons, nExtractedIcons);
142 }
143
146}
BOOL ResourceToFile(INT i, LPCSTR FileName)
EXTRACTICONTESTS IconTests[]
#define ok(value,...)
Definition: atltest.h:57
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIcons)
Definition: iconcache.cpp:855
#define _countof(array)
Definition: sndvol32.h:70
char CHAR
Definition: xmlstorage.h:175

◆ START_TEST() [2/2]

START_TEST ( SHDefExtractIcon  )

Definition at line 160 of file ExtractIconEx.cpp.

161{
162 HRESULT hr;
163 int SysBigIconSize = GetSystemMetrics(SM_CXICON);
164
165 // Modern Windows requires the system image list to be initialized for GIL_SIMULATEDOC to work!
166 SHFILEINFOW shfi;
167 SHGetFileInfoW(L"x", 0, &shfi, sizeof(shfi), SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
168
171 PathAppendW(path, L"user32.dll");
172 int index = 1;
173
174 ok(SHDEI(path, index, 0, 0) == SysBigIconSize, "0 size must match GetSystemMetrics\n");
175 ok(SHDEI(path, index, 0, SysBigIconSize * 2) == SysBigIconSize * 2, "Resize failed\n");
176
177 HICON hIcoLarge, hIcoSmall;
178 if (SHDefExtractIcon(path, index, 0, &hIcoLarge, &hIcoSmall, 0) != S_OK)
179 hIcoLarge = hIcoSmall = NULL;
180 ok(hIcoLarge && hIcoSmall && !SHAreIconsEqual(hIcoLarge, hIcoSmall), "Large+Small failed\n");
181 SafeDestroyIcon(hIcoLarge);
182 SafeDestroyIcon(hIcoSmall);
183
184 static const int sizes[] = { 0, SysBigIconSize * 2 };
185 for (UINT i = 0; i < _countof(sizes); ++i)
186 {
187 HICON hIcoNormal, hIcoSimDoc;
188 if (FAILED(hr = SHDefExtractIcon(path, index, 0, &hIcoNormal, NULL, sizes[i])))
189 hIcoNormal = NULL;
190 if (FAILED(hr = SHDefExtractIcon(path, index, GIL_SIMULATEDOC, &hIcoSimDoc, NULL, sizes[i])))
191 hIcoSimDoc = NULL;
192 ok(hIcoNormal && hIcoSimDoc && !SHAreIconsEqual(hIcoNormal, hIcoSimDoc), "GIL_SIMULATEDOC failed\n");
193 SafeDestroyIcon(hIcoNormal);
194 SafeDestroyIcon(hIcoSimDoc);
195 }
196
198 GetTempFileNameW(path, L"TEST", 0, path);
199 ok(SHDEI(path) == S_FALSE, "Empty file should return S_FALSE\n");
202 {
203 DWORD io;
204 WriteFile(hFile, "!", 1, &io, NULL);
206 ok(SHDEI(path) == S_FALSE, "File without icons should return S_FALSE\n");
207 }
209}
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:43
#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 @545 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 L(x)
Definition: ntvdm.h:50
#define PathAppendW
Definition: pathcch.h:309
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:172
#define SHGFI_USEFILEATTRIBUTES
Definition: shellapi.h:182
#define DeleteFile
Definition: winbase.h:3789
#define S_FALSE
Definition: winerror.h:2357
#define SM_CXICON
Definition: winuser.h:975
int WINAPI GetSystemMetrics(_In_ int)
__wchar_t WCHAR
Definition: xmlstorage.h:180

Variable Documentation

◆ IconTests

EXTRACTICONTESTS IconTests[]
Initial value:
=
{
{L"%SystemRoot%\\System32\\cmd.exe", 1},
{L"%SystemRoot%\\System32\\autochk.exe", 0},
{L"%SystemRoot%\\non-existent-file.sdf", 0},
{L"%SystemRoot%\\explorer.exe", 18},
{L"sysicon.ico", 1},
{L"ROS.ico", 0}
}

Definition at line 98 of file ExtractIconEx.cpp.

Referenced by START_TEST().