Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.1 KiB

  1. #include <windows.h>
  2. #include "resource.h"
  3. #define countof(a) (sizeof(a)/sizeof((a)[0]))
  4. void InstallFonts(HINSTANCE hInst, LPCTSTR pcszIniPath);
  5. void AddFont(HINSTANCE hInst, LPCTSTR pcszFontName, LPCTSTR pcszFontFile);
  6. BOOL FileExists(LPCSTR lpcszFileName);
  7. LPSTR AddPath(LPSTR lpszPath, LPCSTR lpcszFileName);
  8. LPCSTR GetFileName(LPCSTR lpcszFilePath);
  9. LPSTR FAR ANSIStrRChr(LPCSTR lpStart, WORD wMatch);
  10. __inline BOOL ChrCmpA_inline(WORD w1, WORD wMatch);
  11. int _stdcall ModuleEntry(void)
  12. {
  13. int i;
  14. STARTUPINFO si;
  15. LPSTR pszCmdLine = GetCommandLine();
  16. if ( *pszCmdLine == '\"' ) {
  17. /*
  18. * Scan, and skip over, subsequent characters until
  19. * another double-quote or a null is encountered.
  20. */
  21. while ( *++pszCmdLine && (*pszCmdLine
  22. != '\"') );
  23. /*
  24. * If we stopped on a double-quote (usual case), skip
  25. * over it.
  26. */
  27. if ( *pszCmdLine == '\"' )
  28. pszCmdLine++;
  29. }
  30. else {
  31. while (*pszCmdLine > ' ')
  32. pszCmdLine++;
  33. }
  34. /*
  35. * Skip past any white space preceeding the second token.
  36. */
  37. while (*pszCmdLine && (*pszCmdLine <= ' ')) {
  38. pszCmdLine++;
  39. }
  40. si.dwFlags = 0;
  41. GetStartupInfoA(&si);
  42. i = WinMain(GetModuleHandle(NULL), NULL, pszCmdLine,
  43. si.dwFlags & STARTF_USESHOWWINDOW ? si.wShowWindow : SW_SHOWDEFAULT);
  44. ExitProcess(i);
  45. return i; // We never comes here.
  46. }
  47. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  48. {
  49. TCHAR szIniPath[MAX_PATH];
  50. LPTSTR pszPtr;
  51. TCHAR szTitle[MAX_PATH];
  52. TCHAR szMsg[MAX_PATH];
  53. TCHAR szError[MAX_PATH];
  54. LoadString(hInst, IDS_TITLE, szTitle, countof(szTitle));
  55. *szIniPath = '\0';
  56. GetModuleFileName(NULL, szIniPath, countof(szIniPath));
  57. if (*szIniPath == TEXT('\0'))
  58. {
  59. LoadString(hInst, IDS_INVALID_DIR, szError, countof(szError));
  60. MessageBox(NULL, szError, szTitle, MB_OK | MB_SETFOREGROUND);
  61. return 1;
  62. }
  63. if ((pszPtr = ANSIStrRChr(szIniPath,'\\')) != NULL)
  64. pszPtr++;
  65. else
  66. pszPtr = szIniPath;
  67. lstrcpy(pszPtr, TEXT("ieakfont.ini"));
  68. if (!FileExists(szIniPath))
  69. {
  70. LoadString(hInst, IDS_INVALID_INIFILE, szMsg, countof(szMsg));
  71. wsprintf(szError, szMsg, szIniPath);
  72. MessageBox(NULL, szError, szTitle, MB_OK | MB_SETFOREGROUND);
  73. return 1;
  74. }
  75. InstallFonts(hInst, szIniPath);
  76. return 1;
  77. }
  78. void InstallFonts(HINSTANCE hInst, LPCTSTR pcszIniPath)
  79. {
  80. int nFonts = 0;
  81. TCHAR szFontDir[MAX_PATH];
  82. // get the fonts directory
  83. GetWindowsDirectory(szFontDir, countof(szFontDir));
  84. AddPath(szFontDir, "FONTS");
  85. // get the font filenames to be installed from the ini file
  86. nFonts = GetPrivateProfileInt(TEXT("FONTS"), TEXT("NUMFONTS"), 0, pcszIniPath);
  87. for (int nIndex = 0; nIndex < nFonts; nIndex++)
  88. {
  89. TCHAR szKey[10];
  90. TCHAR szFontStr[MAX_PATH];
  91. wsprintf(szKey, TEXT("FONT%d"), nIndex + 1);
  92. if (GetPrivateProfileString(TEXT("FONTS"), szKey, TEXT(""), szFontStr, countof(szFontStr), pcszIniPath))
  93. {
  94. TCHAR szFontFile[MAX_PATH];
  95. TCHAR szFontName[MAX_PATH];
  96. lstrcpy(szFontFile, szFontDir);
  97. AddPath(szFontFile, szFontStr);
  98. wsprintf(szKey, TEXT("FONTNAME%d"), nIndex + 1);
  99. GetPrivateProfileString(TEXT("FONTS"), szKey, szFontStr, szFontName, countof(szFontName), pcszIniPath);
  100. // REVIEW: (a-saship) by the time this api call happens all parameters are validated and not empty.
  101. // AddFont itself doesn't validate in-parameters.
  102. AddFont(hInst, szFontName, szFontFile);
  103. }
  104. }
  105. }
  106. void AddFont(HINSTANCE hInst, LPCTSTR pcszFontName, LPCTSTR pcszFontFile)
  107. {
  108. TCHAR szTitle[MAX_PATH];
  109. TCHAR szMsg[MAX_PATH];
  110. TCHAR szError[MAX_PATH];
  111. TCHAR szFontFileName[MAX_PATH];
  112. TCHAR szKeyName[MAX_PATH];
  113. HKEY hkFontsKey;
  114. LoadString(hInst, IDS_TITLE, szTitle, countof(szTitle));
  115. lstrcpy(szFontFileName, GetFileName(pcszFontFile));
  116. if (!AddFontResource(pcszFontFile))
  117. {
  118. LoadString(hInst, IDS_ADDFONT_ERROR, szMsg, countof(szMsg));
  119. wsprintf(szError, szMsg, szFontFileName);
  120. MessageBox(NULL, szError, szTitle, MB_OK | MB_SETFOREGROUND);
  121. return;
  122. }
  123. SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0L);
  124. // make it permanent by adding it to the registry
  125. if ((GetVersion() & 0x80000000) == 0) // if NT
  126. lstrcpy(szKeyName, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"));
  127. else
  128. lstrcpy(szKeyName, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Fonts"));
  129. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkFontsKey, NULL) == ERROR_SUCCESS)
  130. {
  131. RegSetValueEx(hkFontsKey, pcszFontName, 0, REG_SZ, (CONST BYTE *) szFontFileName, lstrlen(szFontFileName) + 1);
  132. RegCloseKey(hkFontsKey);
  133. }
  134. }
  135. BOOL FileExists(LPCSTR lpcszFileName)
  136. {
  137. DWORD dwAttrib = GetFileAttributes(lpcszFileName);
  138. if (dwAttrib == (DWORD) -1)
  139. return FALSE;
  140. return !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  141. }
  142. LPSTR AddPath(LPSTR lpszPath, LPCSTR lpcszFileName)
  143. {
  144. LPSTR lpszPtr;
  145. if (lpszPath == NULL)
  146. return NULL;
  147. lpszPtr = lpszPath + lstrlen(lpszPath);
  148. if (lpszPtr > lpszPath && *CharPrev(lpszPath, lpszPtr) != '\\')
  149. *lpszPtr++ = '\\';
  150. if (lpcszFileName != NULL)
  151. lstrcpy(lpszPtr, lpcszFileName);
  152. else
  153. *lpszPtr = '\0';
  154. return lpszPath;
  155. }
  156. LPCSTR GetFileName(LPCSTR lpcszFilePath)
  157. // Return the name of the file alone from lpcszFilePath
  158. {
  159. LPCSTR lpcszFileName = ANSIStrRChr(lpcszFilePath, '\\');
  160. return (lpcszFileName == NULL ? lpcszFilePath : lpcszFileName + 1);
  161. }
  162. // copied from \\trango\slmadd\src\shell\shlwapi\strings.c
  163. /*
  164. * StrRChr - Find last occurrence of character in string
  165. * Assumes lpStart points to start of null terminated string
  166. * wMatch is the character to match
  167. * returns ptr to the last occurrence of ch in str, NULL if not found.
  168. */
  169. LPSTR FAR ANSIStrRChr(LPCSTR lpStart, WORD wMatch)
  170. {
  171. LPCSTR lpFound = NULL;
  172. for ( ; *lpStart; lpStart = CharNext(lpStart))
  173. {
  174. // (ChrCmp returns FALSE when characters match)
  175. if (!ChrCmpA_inline(*(UNALIGNED WORD FAR *)lpStart, wMatch))
  176. lpFound = lpStart;
  177. }
  178. return ((LPSTR)lpFound);
  179. }
  180. // copied from \\trango\slmadd\src\shell\shlwapi\strings.c
  181. /*
  182. * ChrCmp - Case sensitive character comparison for DBCS
  183. * Assumes w1, wMatch are characters to be compared
  184. * Return FALSE if they match, TRUE if no match
  185. */
  186. __inline BOOL ChrCmpA_inline(WORD w1, WORD wMatch)
  187. {
  188. /* Most of the time this won't match, so test it first for speed.
  189. */
  190. if (LOBYTE(w1) == LOBYTE(wMatch))
  191. {
  192. if (IsDBCSLeadByte(LOBYTE(w1)))
  193. {
  194. return(w1 != wMatch);
  195. }
  196. return FALSE;
  197. }
  198. return TRUE;
  199. }