Source code of Windows XP (NT5)
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.

518 lines
18 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: uapiinit.cpp
  4. //
  5. // Module: UAPIINIT (static lib)
  6. //
  7. // Synopsis: This static library wraps up the initialization code needed for a module
  8. // to use cmutoa.dll. Calling the InitUnicodeAPI function either sets up
  9. // the modules U pointers (this library also contains all the U pointer
  10. // declarations) with the W version of the API if running on NT or the
  11. // appropriate A or UA API depending on whether conversion is needed before/after
  12. // calling the windows API or not. Modules using this lib should include the
  13. // uapi.h header in their precompiled header and should have cmutoa.dll handy
  14. // on win9x. The idea for this library and the associated dll was borrowed
  15. // from F. Avery Bishop's April 1999 MSJ article "Design a Single Unicode
  16. // App that Runs on Both Windows 98 and Windows 2000"
  17. //
  18. // Copyright (c) 1999 Microsoft Corporation
  19. //
  20. // Author: quintinb Created 4-25-99
  21. //
  22. // History:
  23. //+----------------------------------------------------------------------------
  24. #include <windows.h>
  25. #include <shlobj.h>
  26. #include "cmdebug.h"
  27. #include "cmutoa.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif /* __cplusplus */
  31. UAPI_CallWindowProc CallWindowProcU;
  32. UAPI_CharLower CharLowerU;
  33. UAPI_CharPrev CharPrevU;
  34. UAPI_CharNext CharNextU;
  35. UAPI_CharUpper CharUpperU;
  36. UAPI_CreateDialogParam CreateDialogParamU;
  37. UAPI_CreateDirectory CreateDirectoryU;
  38. UAPI_CreateEvent CreateEventU;
  39. UAPI_CreateFile CreateFileU;
  40. UAPI_CreateFileMapping CreateFileMappingU;
  41. UAPI_CreateMutex CreateMutexU;
  42. UAPI_CreateProcess CreateProcessU;
  43. UAPI_CreateWindowEx CreateWindowExU;
  44. UAPI_DefWindowProc DefWindowProcU;
  45. UAPI_DeleteFile DeleteFileU;
  46. UAPI_DialogBoxParam DialogBoxParamU;
  47. UAPI_DispatchMessage DispatchMessageU;
  48. UAPI_ExpandEnvironmentStrings ExpandEnvironmentStringsU;
  49. UAPI_FindResourceEx FindResourceExU;
  50. UAPI_FindWindowEx FindWindowExU;
  51. UAPI_GetClassLong GetClassLongU;
  52. UAPI_GetDateFormat GetDateFormatU;
  53. UAPI_GetDlgItemText GetDlgItemTextU;
  54. UAPI_GetFileAttributes GetFileAttributesU;
  55. UAPI_GetMessage GetMessageU;
  56. UAPI_GetModuleFileName GetModuleFileNameU;
  57. UAPI_GetModuleHandle GetModuleHandleU;
  58. UAPI_GetPrivateProfileInt GetPrivateProfileIntU;
  59. UAPI_GetPrivateProfileString GetPrivateProfileStringU;
  60. UAPI_GetStringTypeEx GetStringTypeExU;
  61. UAPI_GetSystemDirectory GetSystemDirectoryU;
  62. UAPI_GetTempFileName GetTempFileNameU;
  63. UAPI_GetTempPath GetTempPathU;
  64. UAPI_GetTimeFormat GetTimeFormatU;
  65. UAPI_GetUserName GetUserNameU;
  66. UAPI_GetVersionEx GetVersionExU;
  67. UAPI_GetWindowLong GetWindowLongU;
  68. UAPI_GetWindowText GetWindowTextU;
  69. UAPI_GetWindowTextLength GetWindowTextLengthU;
  70. UAPI_InsertMenu InsertMenuU;
  71. UAPI_IsDialogMessage IsDialogMessageU;
  72. UAPI_LoadCursor LoadCursorU;
  73. UAPI_LoadIcon LoadIconU;
  74. UAPI_LoadImage LoadImageU;
  75. UAPI_LoadLibraryEx LoadLibraryExU;
  76. UAPI_LoadMenu LoadMenuU;
  77. UAPI_LoadString LoadStringU;
  78. UAPI_lstrcat lstrcatU;
  79. UAPI_lstrcmp lstrcmpU;
  80. UAPI_lstrcmpi lstrcmpiU;
  81. UAPI_lstrcpy lstrcpyU;
  82. UAPI_lstrcpyn lstrcpynU;
  83. UAPI_lstrlen lstrlenU;
  84. UAPI_OpenEvent OpenEventU;
  85. UAPI_OpenFileMapping OpenFileMappingU;
  86. UAPI_PeekMessage PeekMessageU;
  87. UAPI_PostMessage PostMessageU;
  88. UAPI_PostThreadMessage PostThreadMessageU;
  89. UAPI_RegCreateKeyEx RegCreateKeyExU;
  90. UAPI_RegDeleteKey RegDeleteKeyU;
  91. UAPI_RegDeleteValue RegDeleteValueU;
  92. UAPI_RegEnumKeyEx RegEnumKeyExU;
  93. UAPI_RegisterClassEx RegisterClassExU;
  94. UAPI_RegisterWindowMessage RegisterWindowMessageU;
  95. UAPI_RegOpenKeyEx RegOpenKeyExU;
  96. UAPI_RegQueryValueEx RegQueryValueExU;
  97. UAPI_RegSetValueEx RegSetValueExU;
  98. UAPI_SearchPath SearchPathU;
  99. UAPI_SendDlgItemMessage SendDlgItemMessageU;
  100. UAPI_SendMessage SendMessageU;
  101. UAPI_SetCurrentDirectory SetCurrentDirectoryU;
  102. UAPI_SetDlgItemText SetDlgItemTextU;
  103. UAPI_SetWindowLong SetWindowLongU;
  104. UAPI_SetWindowText SetWindowTextU;
  105. UAPI_UnregisterClass UnregisterClassU;
  106. UAPI_WinHelp WinHelpU;
  107. UAPI_wsprintf wsprintfU;
  108. UAPI_WritePrivateProfileString WritePrivateProfileStringU;
  109. UAPI_wvsprintf wvsprintfU;
  110. //+----------------------------------------------------------------------------
  111. //
  112. // Function: CheckUAPIFunctionPointers
  113. //
  114. // Synopsis: Checks all of the xxxU function pointers to ensure that they are
  115. // non-NULL. Will catch a function load failure.
  116. //
  117. // Arguments: None
  118. //
  119. // Returns: BOOL - TRUE on success
  120. //
  121. // History: quintinb Created 6/24/99
  122. //
  123. //+----------------------------------------------------------------------------
  124. BOOL CheckUAPIFunctionPointers()
  125. {
  126. return (CallWindowProcU &&
  127. CharLowerU &&
  128. CharPrevU &&
  129. CharNextU &&
  130. CharUpperU &&
  131. CreateDialogParamU &&
  132. CreateDirectoryU &&
  133. CreateEventU &&
  134. CreateFileU &&
  135. CreateFileMappingU &&
  136. CreateMutexU &&
  137. CreateProcessU &&
  138. CreateWindowExU &&
  139. DefWindowProcU &&
  140. DeleteFileU &&
  141. DialogBoxParamU &&
  142. DispatchMessageU &&
  143. ExpandEnvironmentStringsU &&
  144. FindResourceExU &&
  145. FindWindowExU &&
  146. GetClassLongU &&
  147. GetDateFormatU &&
  148. GetDlgItemTextU &&
  149. GetFileAttributesU &&
  150. GetMessageU &&
  151. GetModuleFileNameU &&
  152. GetModuleHandleU &&
  153. GetPrivateProfileIntU &&
  154. GetPrivateProfileStringU &&
  155. GetStringTypeExU &&
  156. GetSystemDirectoryU &&
  157. GetTempFileNameU &&
  158. GetTempPathU &&
  159. GetTimeFormatU &&
  160. GetUserNameU &&
  161. GetVersionExU &&
  162. GetWindowLongU &&
  163. GetWindowTextU &&
  164. GetWindowTextLengthU &&
  165. InsertMenuU &&
  166. IsDialogMessageU &&
  167. LoadCursorU &&
  168. LoadIconU &&
  169. LoadImageU &&
  170. LoadLibraryExU &&
  171. LoadMenuU &&
  172. LoadStringU &&
  173. lstrcatU &&
  174. lstrcmpU &&
  175. lstrcmpiU &&
  176. lstrcpyU &&
  177. lstrcpynU &&
  178. lstrlenU &&
  179. OpenEventU &&
  180. OpenFileMappingU &&
  181. PeekMessageU &&
  182. PostMessageU &&
  183. PostThreadMessageU &&
  184. RegCreateKeyExU &&
  185. RegDeleteKeyU &&
  186. RegDeleteValueU &&
  187. RegEnumKeyExU &&
  188. RegisterClassExU &&
  189. RegisterWindowMessageU &&
  190. RegOpenKeyExU &&
  191. RegQueryValueExU &&
  192. RegSetValueExU &&
  193. SearchPathU &&
  194. SendDlgItemMessageU &&
  195. SendMessageU &&
  196. SetCurrentDirectoryU &&
  197. SetDlgItemTextU &&
  198. SetWindowLongU &&
  199. SetWindowTextU &&
  200. UnregisterClassU &&
  201. WinHelpU &&
  202. wsprintfU &&
  203. WritePrivateProfileStringU &&
  204. wvsprintfU);
  205. }
  206. //+----------------------------------------------------------------------------
  207. //
  208. // Function: InitUnicodeAPI
  209. //
  210. // Synopsis: Initializes the Unicode wrapper APIs. Thus on Windows NT we will
  211. // use the native Unicode (xxxW) form of the API and on Win9x we
  212. // will use the UA form located in cmutoa.dll. This prevents have
  213. // a wrapper function to call through on the NT platforms were it is
  214. // really unneccesary.
  215. //
  216. // Arguments: None
  217. //
  218. // Returns: BOOL - TRUE on success
  219. //
  220. // History: quintinb Created 6/24/99
  221. //
  222. //+----------------------------------------------------------------------------
  223. BOOL InitUnicodeAPI()
  224. {
  225. OSVERSIONINFO Osv;
  226. BOOL IsWindowsNT;
  227. HMODULE hCmUtoADll = NULL;
  228. Osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
  229. if(!GetVersionEx(&Osv))
  230. {
  231. return FALSE ;
  232. }
  233. IsWindowsNT = (BOOL) (Osv.dwPlatformId == VER_PLATFORM_WIN32_NT) ;
  234. // define this symbol in UAPI.H to emulate Windows 9x when testing on Windows NT.
  235. #ifdef EMULATE9X
  236. IsWindowsNT = FALSE;
  237. #endif
  238. if(IsWindowsNT)
  239. {
  240. CallWindowProcU = CallWindowProcW;
  241. CharLowerU = CharLowerW;
  242. CharPrevU = CharPrevW;
  243. CharNextU = CharNextW;
  244. CharUpperU = CharUpperW;
  245. CreateDialogParamU = CreateDialogParamW;
  246. CreateDirectoryU = CreateDirectoryW;
  247. CreateEventU = CreateEventW;
  248. CreateFileU = CreateFileW;
  249. CreateFileMappingU = CreateFileMappingW;
  250. CreateMutexU = CreateMutexW;
  251. CreateProcessU = CreateProcessW;
  252. CreateWindowExU = CreateWindowExW;
  253. DefWindowProcU = DefWindowProcW;
  254. DeleteFileU = DeleteFileW;
  255. DialogBoxParamU = DialogBoxParamW;
  256. DispatchMessageU = DispatchMessageW;
  257. ExpandEnvironmentStringsU = ExpandEnvironmentStringsW;
  258. FindResourceExU = FindResourceExW;
  259. FindWindowExU = FindWindowExW;
  260. GetClassLongU = GetClassLongW;
  261. GetDateFormatU = GetDateFormatW;
  262. GetDlgItemTextU = GetDlgItemTextW;
  263. GetFileAttributesU = GetFileAttributesW;
  264. GetMessageU = GetMessageW;
  265. GetModuleFileNameU = GetModuleFileNameW;
  266. GetModuleHandleU = GetModuleHandleW;
  267. GetPrivateProfileIntU = GetPrivateProfileIntW;
  268. GetPrivateProfileStringU = GetPrivateProfileStringW;
  269. GetStringTypeExU = GetStringTypeExW;
  270. GetSystemDirectoryU = GetSystemDirectoryW;
  271. GetTempFileNameU = GetTempFileNameW;
  272. GetTempPathU = GetTempPathW;
  273. GetTimeFormatU = GetTimeFormatW;
  274. GetUserNameU = GetUserNameW;
  275. GetVersionExU = GetVersionExW;
  276. GetWindowLongU = GetWindowLongPtrW;
  277. GetWindowTextU = GetWindowTextW;
  278. GetWindowTextLengthU = GetWindowTextLengthW;
  279. InsertMenuU = InsertMenuW;
  280. IsDialogMessageU = IsDialogMessageW;
  281. LoadCursorU = LoadCursorW;
  282. LoadIconU = LoadIconW;
  283. LoadImageU = LoadImageW;
  284. LoadLibraryExU = LoadLibraryExW;
  285. LoadMenuU = LoadMenuW;
  286. LoadStringU = LoadStringW;
  287. lstrcatU = lstrcatW;
  288. lstrcmpU = lstrcmpW;
  289. lstrcmpiU = lstrcmpiW;
  290. lstrcpyU = lstrcpyW;
  291. lstrcpynU = lstrcpynW;
  292. lstrlenU = lstrlenW;
  293. OpenEventU = OpenEventW;
  294. OpenFileMappingU = OpenFileMappingW;
  295. PeekMessageU = PeekMessageW;
  296. PostMessageU = PostMessageW;
  297. PostThreadMessageU = PostThreadMessageW;
  298. RegCreateKeyExU = RegCreateKeyExW;
  299. RegDeleteKeyU = RegDeleteKeyW;
  300. RegDeleteValueU = RegDeleteValueW;
  301. RegEnumKeyExU = RegEnumKeyExW;
  302. RegisterClassExU = RegisterClassExW;
  303. RegisterWindowMessageU = RegisterWindowMessageW;
  304. RegOpenKeyExU = RegOpenKeyExW;
  305. RegQueryValueExU = RegQueryValueExW;
  306. RegSetValueExU = RegSetValueExW;
  307. SearchPathU = SearchPathW;
  308. SendDlgItemMessageU = SendDlgItemMessageW;
  309. SendMessageU = SendMessageW;
  310. SetCurrentDirectoryU = SetCurrentDirectoryW;
  311. SetDlgItemTextU = SetDlgItemTextW;
  312. SetWindowLongU = SetWindowLongPtrW;
  313. SetWindowTextU = SetWindowTextW;
  314. UnregisterClassU = UnregisterClassW;
  315. WinHelpU = WinHelpW;
  316. wsprintfU = wsprintfW;
  317. WritePrivateProfileStringU = WritePrivateProfileStringW;
  318. wvsprintfU = wvsprintfW;
  319. }
  320. else
  321. {
  322. BOOL (*InitCmUToA)(PUAPIINIT);
  323. UAPIINIT UAInit;
  324. ZeroMemory(&UAInit, sizeof(UAPIINIT));
  325. hCmUtoADll = LoadLibraryExA("cmutoa.DLL", NULL, 0);
  326. if(!hCmUtoADll)
  327. {
  328. DWORD dwError = GetLastError();
  329. CMASSERTMSG(FALSE, TEXT("InitUnicodeAPI -- Cmutoa.dll Failed to Load."));
  330. return FALSE;
  331. }
  332. // Get Initialization routine from the DLL
  333. InitCmUToA = (BOOL (*)(PUAPIINIT)) GetProcAddress(hCmUtoADll, "InitCmUToA") ;
  334. // Set up structure containing locations of U function pointers
  335. UAInit.pCallWindowProcU = &CallWindowProcU;
  336. UAInit.pCharLowerU = &CharLowerU;
  337. UAInit.pCharPrevU = &CharPrevU;
  338. UAInit.pCharNextU = &CharNextU;
  339. UAInit.pCharUpperU = &CharUpperU;
  340. UAInit.pCreateDialogParamU = &CreateDialogParamU;
  341. UAInit.pCreateDirectoryU = &CreateDirectoryU;
  342. UAInit.pCreateEventU = &CreateEventU;
  343. UAInit.pCreateFileU = &CreateFileU;
  344. UAInit.pCreateFileMappingU = &CreateFileMappingU;
  345. UAInit.pCreateMutexU = &CreateMutexU;
  346. UAInit.pCreateProcessU = &CreateProcessU;
  347. UAInit.pCreateWindowExU = &CreateWindowExU;
  348. UAInit.pDefWindowProcU = &DefWindowProcU;
  349. UAInit.pDeleteFileU = &DeleteFileU;
  350. UAInit.pDialogBoxParamU = &DialogBoxParamU;
  351. UAInit.pDispatchMessageU = &DispatchMessageU;
  352. UAInit.pExpandEnvironmentStringsU = &ExpandEnvironmentStringsU;
  353. UAInit.pFindResourceExU = &FindResourceExU;
  354. UAInit.pFindWindowExU = &FindWindowExU;
  355. UAInit.pGetClassLongU = &GetClassLongU;
  356. UAInit.pGetDateFormatU = &GetDateFormatU;
  357. UAInit.pGetDlgItemTextU = &GetDlgItemTextU;
  358. UAInit.pGetFileAttributesU = &GetFileAttributesU;
  359. UAInit.pGetMessageU = &GetMessageU;
  360. UAInit.pGetModuleFileNameU = &GetModuleFileNameU;
  361. UAInit.pGetModuleHandleU = &GetModuleHandleU;
  362. UAInit.pGetPrivateProfileIntU = &GetPrivateProfileIntU;
  363. UAInit.pGetPrivateProfileStringU = &GetPrivateProfileStringU;
  364. UAInit.pGetStringTypeExU = &GetStringTypeExU;
  365. UAInit.pGetSystemDirectoryU = &GetSystemDirectoryU;
  366. UAInit.pGetTempFileNameU = &GetTempFileNameU;
  367. UAInit.pGetTempPathU = &GetTempPathU;
  368. UAInit.pGetTimeFormatU = &GetTimeFormatU;
  369. UAInit.pGetUserNameU = &GetUserNameU;
  370. UAInit.pGetVersionExU = &GetVersionExU;
  371. UAInit.pGetWindowLongU = &GetWindowLongU;
  372. UAInit.pGetWindowTextU = &GetWindowTextU;
  373. UAInit.pGetWindowTextLengthU = &GetWindowTextLengthU;
  374. UAInit.pInsertMenuU = &InsertMenuU;
  375. UAInit.pIsDialogMessageU = &IsDialogMessageU;
  376. UAInit.pLoadCursorU = &LoadCursorU;
  377. UAInit.pLoadIconU = &LoadIconU;
  378. UAInit.pLoadImageU = &LoadImageU;
  379. UAInit.pLoadLibraryExU = &LoadLibraryExU;
  380. UAInit.pLoadMenuU = &LoadMenuU;
  381. UAInit.pLoadStringU = &LoadStringU;
  382. UAInit.plstrcatU = &lstrcatU;
  383. UAInit.plstrcmpU = &lstrcmpU;
  384. UAInit.plstrcmpiU = &lstrcmpiU;
  385. UAInit.plstrcpyU = &lstrcpyU;
  386. UAInit.plstrcpynU = &lstrcpynU;
  387. UAInit.plstrlenU = &lstrlenU;
  388. UAInit.pOpenEventU = &OpenEventU;
  389. UAInit.pOpenFileMappingU = &OpenFileMappingU;
  390. UAInit.pPeekMessageU = &PeekMessageU;
  391. UAInit.pPostMessageU = &PostMessageU;
  392. UAInit.pPostThreadMessageU = &PostThreadMessageU;
  393. UAInit.pRegCreateKeyExU = &RegCreateKeyExU;
  394. UAInit.pRegDeleteKeyU = &RegDeleteKeyU;
  395. UAInit.pRegDeleteValueU = &RegDeleteValueU;
  396. UAInit.pRegEnumKeyExU = &RegEnumKeyExU;
  397. UAInit.pRegisterClassExU = &RegisterClassExU;
  398. UAInit.pRegisterWindowMessageU = &RegisterWindowMessageU;
  399. UAInit.pRegOpenKeyExU = &RegOpenKeyExU;
  400. UAInit.pRegQueryValueExU = &RegQueryValueExU;
  401. UAInit.pRegSetValueExU = &RegSetValueExU;
  402. UAInit.pSearchPathU = &SearchPathU;
  403. UAInit.pSendDlgItemMessageU = &SendDlgItemMessageU;
  404. UAInit.pSendMessageU = &SendMessageU;
  405. UAInit.pSetCurrentDirectoryU = &SetCurrentDirectoryU;
  406. UAInit.pSetDlgItemTextU = &SetDlgItemTextU;
  407. UAInit.pSetWindowLongU = &SetWindowLongU;
  408. UAInit.pSetWindowTextU = &SetWindowTextU;
  409. UAInit.pUnregisterClassU = &UnregisterClassU;
  410. UAInit.pWinHelpU = &WinHelpU;
  411. UAInit.pwsprintfU = &wsprintfU;
  412. UAInit.pWritePrivateProfileStringU = &WritePrivateProfileStringU;
  413. UAInit.pwvsprintfU = &wvsprintfU;
  414. if( NULL == InitCmUToA || !InitCmUToA(&UAInit))
  415. {
  416. CMASSERTMSG(FALSE, TEXT("InitUnicodeAPI -- Unable to find InitCmUToA or InitCmUToA Failed."));
  417. FreeLibrary(hCmUtoADll);
  418. return FALSE;
  419. }
  420. for (int i = 0; i < (sizeof(UAPIINIT) / sizeof(LPVOID)); i++)
  421. {
  422. if (UAInit.ppvUapiFun[i])
  423. {
  424. if (NULL == *(UAInit.ppvUapiFun[i]))
  425. {
  426. CMTRACE1(TEXT("Unable to fill UAInit[%d]. Please Investigate"), i);
  427. }
  428. }
  429. else
  430. {
  431. CMTRACE1(TEXT("No Memory for UAInit[%d]. Please Investigate"), i);
  432. }
  433. }
  434. }
  435. if(!CheckUAPIFunctionPointers())
  436. {
  437. CMASSERTMSG(FALSE, TEXT("InitUnicodeAPI -- CheckUAPIFunctionPointers failed."));
  438. FreeLibrary(hCmUtoADll);
  439. return FALSE;
  440. }
  441. return TRUE ;
  442. }
  443. //+----------------------------------------------------------------------------
  444. //
  445. // Function: UnInitUnicodeAPI
  446. //
  447. // Synopsis: De-Initializes the Unicode wrapper APIs. See InitUnicodeAPI for
  448. // details. Primarily, this function frees the module handle(s).
  449. //
  450. // Arguments: None
  451. //
  452. // Returns: BOOL - TRUE on success
  453. //
  454. // History: sumitc Created 7/01/2000
  455. //
  456. //+----------------------------------------------------------------------------
  457. BOOL UnInitUnicodeAPI()
  458. {
  459. OSVERSIONINFO Osv;
  460. BOOL IsWindowsNT;
  461. Osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
  462. if(!GetVersionEx(&Osv))
  463. {
  464. return FALSE ;
  465. }
  466. IsWindowsNT = (BOOL) (Osv.dwPlatformId == VER_PLATFORM_WIN32_NT) ;
  467. // define this symbol in UAPI.H to emulate Windows 9x when testing on Windows NT.
  468. #ifdef EMULATE9X
  469. IsWindowsNT = FALSE;
  470. #endif
  471. if(!IsWindowsNT)
  472. {
  473. HMODULE hCmUtoADll;
  474. hCmUtoADll = GetModuleHandleA("cmutoa.DLL");
  475. if (!hCmUtoADll)
  476. {
  477. CMTRACE(TEXT("UnInitUnicodeAPI - strange.. cmutoa.dll is not loaded into the process!"));
  478. return FALSE;
  479. }
  480. FreeLibrary(hCmUtoADll);
  481. }
  482. return TRUE;
  483. }
  484. #ifdef __cplusplus
  485. }
  486. #endif /* __cplusplus */