ReactOS 0.4.16-dev-1097-g530d26a
fontext.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Font Shell Extension
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Shell extension entry point
5 * COPYRIGHT: Copyright 2019,2020 Mark Jansen <mark.jansen@reactos.org>
6 */
7
8#include "precomp.h"
9#include "undocgdi.h" // for GetFontResourceInfoW
10
12
13const GUID CLSID_CFontExt = { 0xbd84b380, 0x8ca2, 0x1069, { 0xab, 0x1d, 0x08, 0x00, 0x09, 0x48, 0xf5, 0x34 } };
14
16{
17public:
18 void Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE h, const GUID *plibid)
19 {
20 g_FontCache = new CFontCache();
21 CComModule::Init(p, h, plibid);
22 }
23
24 void Term()
25 {
26 delete g_FontCache;
29 }
30};
31
32BEGIN_OBJECT_MAP(ObjectMap)
35
38
40{
42 return S_FALSE;
43 return gModule.DllCanUnloadNow();
44}
45
47{
48 return gModule.DllGetClassObject(rclsid, riid, ppv);
49}
50
52{
53 WCHAR Path[MAX_PATH] = { 0 };
54 static const char DesktopIniContents[] = "[.ShellClassInfo]\r\n"
55 "CLSID={BD84B380-8CA2-1069-AB1D-08000948F534}\r\n"
56 "IconResource=%SystemRoot%\\system32\\shell32.dll,-39\r\n"; // IDI_SHELL_FONTS_FOLDER
58 HRESULT hr;
59
62 return hr;
63
66 return hr;
67
68 // Make this a system folder:
69 // Ideally this should not be done here, but when installing
70 // Otherwise, livecd won't have this set properly
73 {
76 }
77 else
78 {
79 ERR("Unable to get attributes for fonts folder (%d)\n", GetLastError());
80 }
81
82 if (!PathAppendW(Path, L"desktop.ini"))
83 return E_FAIL;
84
88
89 DWORD BytesWritten, BytesToWrite = strlen(DesktopIniContents);
90 if (WriteFile(hFile, DesktopIniContents, (DWORD)BytesToWrite, &BytesWritten, NULL))
91 hr = (BytesToWrite == BytesWritten) ? S_OK : E_FAIL;
92 else
95 return hr;
96}
97
99{
101}
102
105 _Out_ CStringW& strMsg,
106 _In_ PCUIDLIST_ABSOLUTE pidlParent,
107 _In_ UINT cidl,
109{
110 CAtlArray<CStringW> FontPaths;
111 for (UINT n = 0; n < cidl; ++n)
112 {
114 pidl.Attach(ILCombine(pidlParent, apidl[n]));
115 if (!pidl)
116 {
117 ERR("Out of memory\n");
118 return E_OUTOFMEMORY;
119 }
120
124 {
125 ERR("Not font file: %s\n", wine_dbgstr_w(szPath));
126 return E_FAIL;
127 }
128
129 FontPaths.Add(szPath);
130 }
131
132 CRegKey keyFonts;
133 if (keyFonts.Open(FONT_HIVE, FONT_KEY, KEY_WRITE) != ERROR_SUCCESS)
134 {
135 ERR("CRegKey::Open failed\n");
136 return E_FAIL;
137 }
138
139 for (SIZE_T iItem = 0; iItem < FontPaths.GetCount(); ++iItem)
140 {
141 HRESULT hr = DoInstallFontFile(strMsg, FontPaths[iItem], g_FontCache->FontPath(), keyFonts);
143 return hr;
144 }
145
146 return S_OK;
147}
148
151 _Out_ CStringW& strMsg,
152 _In_ PCWSTR pszFontPath,
153 _In_ PCWSTR pszFontsDir,
154 _In_ HKEY hkeyFonts)
155{
156 WCHAR szDestFile[MAX_PATH];
157
158 // Add this font to the font list, so we can query the name
159 if (!AddFontResourceW(pszFontPath))
160 {
161 ERR("AddFontResourceW('%S') failed\n", pszFontPath);
162 return E_FAIL;
163 }
164
165 CStringW strFontName;
166 HRESULT hr = DoGetFontTitle(pszFontPath, strFontName);
167
168 // We got the name, remove it again
169 RemoveFontResourceW(pszFontPath);
170
171 if (!SUCCEEDED(hr))
172 {
173 ERR("DoGetFontTitle failed (err=0x%x)!\n", hr);
174 return hr;
175 }
176
177 StringCchCopyW(szDestFile, sizeof(szDestFile), pszFontsDir);
178
179 LPCWSTR pszFileTitle = PathFindFileName(pszFontPath);
180 PathAppendW(szDestFile, pszFileTitle);
181 if (!CopyFileW(pszFontPath, szDestFile, FALSE))
182 {
183 ERR("CopyFileW('%S', '%S') failed\n", pszFontPath, szDestFile);
184 return E_FAIL;
185 }
186
187 DWORD cbData = (wcslen(pszFileTitle) + 1) * sizeof(WCHAR);
188 LONG nError = RegSetValueExW(hkeyFonts, strFontName, 0, REG_SZ,
189 (const BYTE *)pszFileTitle, cbData);
190 if (nError)
191 {
192 ERR("RegSetValueExW failed with %ld\n", nError);
193 DeleteFileW(szDestFile);
194 return E_FAIL;
195 }
196
197 return AddFontResourceW(szDestFile) ? S_OK : E_FAIL;
198}
199
202 _In_ LPCWSTR pszFontPath,
203 _Out_ CStringW& strFontName)
204{
205 DWORD cbInfo = 0;
206 BOOL ret = GetFontResourceInfoW(pszFontPath, &cbInfo, NULL, 1);
207 if (!ret || !cbInfo)
208 {
209 ERR("GetFontResourceInfoW failed (err: %u)\n", GetLastError());
210 return E_FAIL;
211 }
212
213 LPWSTR pszBuffer = strFontName.GetBuffer(cbInfo / sizeof(WCHAR));
214 ret = GetFontResourceInfoW(pszFontPath, &cbInfo, pszBuffer, 1);
216 strFontName.ReleaseBuffer();
217 if (ret)
218 {
219 TRACE("pszFontName: %S\n", (LPCWSTR)strFontName);
220 return S_OK;
221 }
222
223 ERR("GetFontResourceInfoW failed (err: %u)\n", dwErr);
224 return E_FAIL;
225}
226
229{
230 switch (dwReason)
231 {
234 gModule.Init(ObjectMap, hInstance, NULL);
235 break;
237 gModule.Term();
238 break;
239 }
240
241 return TRUE;
242}
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
PRTL_UNICODE_STRING_BUFFER Path
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static DWORD const LPVOID const lpReserved
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
DWORD dwErr
Definition: service.c:36
#define ERR(fmt,...)
Definition: precomp.h:57
DWORD dwReason
Definition: misc.cpp:135
#define STDAPI
Definition: basetyps.h:41
#define EXTERN_C
Definition: basetyps.h:12
HINSTANCE hInstance
Definition: charmap.c:19
size_t Add(INARGTYPE element)
Definition: atlcoll.h:295
size_t GetCount() const
Definition: atlcoll.h:373
void Term()
Definition: atlbase.h:916
HRESULT DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
Definition: atlbase.h:1037
HRESULT DllUnregisterServer(BOOL bUnRegTypeLib=TRUE)
Definition: atlbase.h:1047
HRESULT Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE, const GUID *plibid)
Definition: atlbase.h:886
HRESULT DllCanUnloadNow()
Definition: atlbase.h:1030
HRESULT DllRegisterServer(BOOL bRegTypeLib=TRUE)
Definition: atlbase.h:1042
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired=KEY_READ|KEY_WRITE) noexcept
Definition: atlbase.h:1173
const CStringW & FontPath() const
Definition: CFontCache.hpp:51
void Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE h, const GUID *plibid)
Definition: fontext.cpp:18
void Term()
Definition: fontext.cpp:24
void Attach(T *lp)
Definition: atlalloc.h:162
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL IsFontDotExt(LPCWSTR pchDotExt)
Definition: precomp.h:44
#define FONT_HIVE
Definition: precomp.h:35
#define FONT_KEY
Definition: precomp.h:36
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4882
#define CloseHandle
Definition: compat.h:739
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define CreateFileW
Definition: compat.h:741
#define FAILED_UNEXPECTEDLY(hr)
Definition: precomp.h:65
BOOL WINAPI CopyFileW(IN LPCWSTR lpExistingFileName, IN LPCWSTR lpNewFileName, IN BOOL bFailIfExists)
Definition: copy.c:439
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:652
BOOL WINAPI SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
Definition: fileinfo.c:794
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
HRESULT WINAPI SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
Definition: shellpath.c:2716
LPWSTR WINAPI PathFindExtensionW(LPCWSTR lpszPath)
Definition: path.c:447
BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
Definition: path.c:1729
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
Definition: fontext.cpp:228
HRESULT DoInstallFontFile(_Out_ CStringW &strMsg, _In_ PCWSTR pszFontPath, _In_ PCWSTR pszFontsDir, _In_ HKEY hkeyFonts)
Definition: fontext.cpp:150
STDAPI DllRegisterServer()
Definition: fontext.cpp:51
const GUID CLSID_CFontExt
Definition: fontext.cpp:13
LONG g_ModuleRefCnt
Definition: fontext.cpp:36
STDAPI DllCanUnloadNow()
Definition: fontext.cpp:39
STDAPI DllUnregisterServer()
Definition: fontext.cpp:98
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
Definition: fontext.cpp:46
HRESULT InstallFontFiles(_Out_ CStringW &strMsg, _In_ PCUIDLIST_ABSOLUTE pidlParent, _In_ UINT cidl, _In_ PCUITEMID_CHILD_ARRAY apidl)
Definition: fontext.cpp:104
CFontExtModule gModule
Definition: fontext.cpp:37
HRESULT DoGetFontTitle(_In_ LPCWSTR pszFontPath, _Out_ CStringW &strFontName)
Definition: fontext.cpp:201
BOOL WINAPI GetFontResourceInfoW(LPCWSTR lpFileName, DWORD *pdwBufSize, void *lpBuffer, DWORD dwType)
GLdouble n
Definition: glext.h:7729
GLfloat GLfloat p
Definition: glext.h:8902
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
REFIID riid
Definition: atlbase.h:39
REFIID LPVOID * ppv
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define wine_dbgstr_w
Definition: kernel32.h:34
#define REG_SZ
Definition: layer.c:22
#define END_OBJECT_MAP()
Definition: atlcom.h:691
#define OBJECT_ENTRY(clsid, class)
Definition: atlcom.h:693
#define BEGIN_OBJECT_MAP(x)
Definition: atlcom.h:689
#define CREATE_ALWAYS
Definition: disk.h:72
LPCWSTR szPath
Definition: env.c:37
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned int UINT
Definition: ndis.h:50
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define FILE_ATTRIBUTE_HIDDEN
Definition: nt_native.h:703
#define FILE_ATTRIBUTE_SYSTEM
Definition: nt_native.h:704
#define KEY_WRITE
Definition: nt_native.h:1031
#define GENERIC_WRITE
Definition: nt_native.h:90
#define L(x)
Definition: ntvdm.h:50
#define PathAppendW
Definition: pathcch.h:309
long LONG
Definition: pedump.c:60
LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
Definition: pidl.c:816
BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
Definition: pidl.c:1489
#define REFIID
Definition: guiddef.h:118
#define REFCLSID
Definition: guiddef.h:117
HRESULT hr
Definition: shlfolder.c:183
#define CSIDL_FONTS
Definition: shlobj.h:2200
#define PathFindFileName
Definition: shlwapi.h:946
const ITEMIDLIST_ABSOLUTE UNALIGNED * PCUIDLIST_ABSOLUTE
Definition: shtypes.idl:63
const PCUITEMID_CHILD * PCUITEMID_CHILD_ARRAY
Definition: shtypes.idl:71
#define TRACE(s)
Definition: solgame.cpp:4
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
DWORD dwAttributes
Definition: vdmdbg.h:34
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
int ret
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesWritten
Definition: wdfiotarget.h:960
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
#define S_FALSE
Definition: winerror.h:2357
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
int WINAPI AddFontResourceW(_In_ LPCWSTR pszFilename)
Definition: font.c:2167
BOOL WINAPI RemoveFontResourceW(_In_ LPCWSTR)
Definition: font.c:2175
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193