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.

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