2 #include ".\fontinstaller.h"
4 CFontInstaller::CFontInstaller()
6 if(HMODULE hGdi
= GetModuleHandle(_T("gdi32.dll")))
8 pAddFontMemResourceEx
= (HANDLE (WINAPI
*)(PVOID
,DWORD
,PVOID
,DWORD
*))GetProcAddress(hGdi
, "AddFontMemResourceEx");
9 pAddFontResourceEx
= (int (WINAPI
*)(LPCTSTR
,DWORD
,PVOID
))GetProcAddress(hGdi
, "AddFontResourceExA");
10 pRemoveFontMemResourceEx
= (BOOL (WINAPI
*)(HANDLE
))GetProcAddress(hGdi
, "RemoveFontMemResourceEx");
11 pRemoveFontResourceEx
= (BOOL (WINAPI
*)(LPCTSTR
,DWORD
,PVOID
))GetProcAddress(hGdi
, "RemoveFontResourceExA");
14 if(HMODULE hGdi
= GetModuleHandle(_T("kernel32.dll")))
16 pMoveFileEx
= (BOOL (WINAPI
*)(LPCTSTR
, LPCTSTR
, DWORD
))GetProcAddress(hGdi
, "MoveFileExA");
20 CFontInstaller::~CFontInstaller()
25 bool CFontInstaller::InstallFont(const CAtlArray
<BYTE
>& data
)
27 return InstallFont(data
.GetData(), data
.GetCount());
30 bool CFontInstaller::InstallFont(const void* pData
, UINT len
)
32 return InstallFontFile(pData
, len
) || InstallFontMemory(pData
, len
);
35 void CFontInstaller::UninstallFonts()
37 if(pRemoveFontMemResourceEx
)
39 POSITION pos
= m_fonts
.GetHeadPosition();
40 while(pos
) pRemoveFontMemResourceEx(m_fonts
.GetNext(pos
));
44 if(pRemoveFontResourceEx
)
46 POSITION pos
= m_files
.GetHeadPosition();
49 CString fn
= m_files
.GetNext(pos
);
50 pRemoveFontResourceEx(fn
, FR_PRIVATE
, 0);
51 if(!DeleteFile(fn
) && pMoveFileEx
)
52 pMoveFileEx(fn
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
);
59 bool CFontInstaller::InstallFontMemory(const void* pData
, UINT len
)
61 if(!pAddFontMemResourceEx
)
65 HANDLE hFont
= pAddFontMemResourceEx((PVOID
)pData
, len
, NULL
, &nFonts
);
66 if(hFont
&& nFonts
> 0) m_fonts
.AddTail(hFont
);
67 return hFont
&& nFonts
> 0;
70 bool CFontInstaller::InstallFontFile(const void* pData
, UINT len
)
72 if(!pAddFontResourceEx
)
76 TCHAR path
[MAX_PATH
], fn
[MAX_PATH
];
77 if(!GetTempPath(MAX_PATH
, path
) || !GetTempFileName(path
, _T("g_font"), 0, fn
))
80 if(f
.Open(fn
, CFile::modeWrite
))
85 if(pAddFontResourceEx(fn
, FR_PRIVATE
, 0) > 0)