mluisupp.h
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 14k
Category:

Windows Develop

Development Platform:

Visual C++

  1. #ifndef _INC_MLUISUPP
  2. #define _INC_MLUISUPP
  3. #include <shlwapi.h>
  4. #include <shlwapip.h>
  5. #ifdef __cplusplus
  6. extern "C"
  7. {
  8. #endif
  9. //+------------------------------------------------------------------
  10. // Multilang Pluggable UI support
  11. // inline functions defs (to centralize code)
  12. //+------------------------------------------------------------------
  13. #ifdef UNICODE
  14. #define MLLoadString            MLLoadStringW
  15. #define MLLoadShellLangString   MLLoadShellLangStringW
  16. #define MLBuildResURLWrap       MLBuildResURLWrapW
  17. #define MLLoadResources         MLLoadResourcesW
  18. #define SHHtmlHelpOnDemandWrap  SHHtmlHelpOnDemandWrapW
  19. #define SHWinHelpOnDemandWrap   SHWinHelpOnDemandWrapW
  20. #else
  21. #define MLLoadString            MLLoadStringA
  22. #define MLLoadShellLangString   MLLoadShellLangStringA
  23. #define MLBuildResURLWrap       MLBuildResURLWrapA
  24. #define MLLoadResources         MLLoadResourcesA
  25. #define SHHtmlHelpOnDemandWrap  SHHtmlHelpOnDemandWrapA
  26. #define SHWinHelpOnDemandWrap   SHWinHelpOnDemandWrapA
  27. #endif
  28. void        MLFreeResources(HINSTANCE hinstParent);
  29. HINSTANCE   MLGetHinst();
  30. HINSTANCE   MLLoadShellLangResources();
  31. #ifdef MLUI_MESSAGEBOX
  32. int         MLShellMessageBox(HWND hWnd, LPCTSTR pszMsg, LPCTSTR pszTitle, UINT fuStyle, ...);
  33. #endif
  34. //
  35. // The following should be both A and W suffixed
  36. //
  37. int         MLLoadStringA(UINT id, LPSTR sz, UINT cchMax);
  38. int         MLLoadStringW(UINT id, LPWSTR sz, UINT cchMax);
  39. int         MLLoadShellLangStringA(UINT id, LPSTR sz, UINT cchMax);
  40. int         MLLoadShellLangStringW(UINT id, LPWSTR sz, UINT cchMax);
  41. HRESULT     MLBuildResURLWrapA(LPSTR    pszLibFile,
  42.                                HMODULE  hModule,
  43.                                DWORD    dwCrossCodePage,
  44.                                LPSTR    pszResName,
  45.                                LPSTR    pszResURL,
  46.                                int      nBufSize,
  47.                                LPSTR    pszParentDll);
  48. HRESULT     MLBuildResURLWrapW(LPWSTR   pszLibFile,
  49.                                HMODULE  hModule,
  50.                                DWORD    dwCrossCodePage,
  51.                                LPWSTR   pszResName,
  52.                                LPWSTR   pszResURL,
  53.                                int      nBufSize,
  54.                                LPWSTR   pszParentDll);
  55. void        MLLoadResourcesA(HINSTANCE hinstParent, LPSTR pszLocResDll);
  56. void        MLLoadResourcesW(HINSTANCE hinstParent, LPWSTR pszLocResDll);
  57. HWND        SHHtmlHelpOnDemandWrapA(HWND hwndCaller, LPCSTR pszFile, UINT uCommand, DWORD_PTR dwData, DWORD dwCrossCodePage);
  58. HWND        SHHtmlHelpOnDemandWrapW(HWND hwndCaller, LPCWSTR pszFile, UINT uCommand, DWORD_PTR dwData, DWORD dwCrossCodePage);
  59. BOOL        SHWinHelpOnDemandWrapA(HWND hwndCaller, LPCSTR lpszHelp, UINT uCommand, DWORD_PTR dwData);
  60. BOOL        SHWinHelpOnDemandWrapW(HWND hwndCaller, LPCWSTR lpszHelp, UINT uCommand, DWORD_PTR dwData);
  61. //
  62. // End of: The following should be both A and W suffixed
  63. //
  64. #ifdef MLUI_INIT
  65. // WARNING: do not attempt to access any of these members directly
  66. // these members may not be initialized until appropriate accessors
  67. // are called, for example hinstLocRes won't be intialized until
  68. // you call MLGetHinst()... so just call the accessor.
  69. struct tagMLUI_INFO
  70. {
  71.     HINSTANCE   hinstLocRes;
  72.     HINSTANCE   hinstParent;
  73.     WCHAR       szLocResDll[MAX_PATH];
  74.     DWORD       dwCrossCodePage;
  75. } g_mluiInfo;
  76. // BUGBUG REVIEW: These aren't thread safe... Do they need to be?
  77. //
  78. void MLLoadResourcesA(HINSTANCE hinstParent, LPSTR pszLocResDll)
  79. {
  80. #ifdef RIP
  81.     RIP(hinstParent != NULL);
  82.     RIP(pszLocResDll != NULL);
  83. #endif
  84.     if (g_mluiInfo.hinstLocRes == NULL)
  85.     {
  86. #ifdef MLUI_SUPPORT
  87.         // plugUI: resource dll == ?
  88.         // resource dll must be dynamically determined and loaded.
  89.         // but we are NOT allowed to LoadLibrary during process attach.
  90.         // therefore we cache the info we need and load later when
  91.         // the first resource is requested.
  92.         SHAnsiToUnicode(pszLocResDll, g_mluiInfo.szLocResDll, sizeof(g_mluiInfo.szLocResDll)/sizeof(g_mluiInfo.szLocResDll[0]));
  93.         g_mluiInfo.hinstParent = hinstParent;
  94.         g_mluiInfo.dwCrossCodePage = ML_CROSSCODEPAGE;
  95. #else
  96.         // non-plugUI: resource dll == parent dll
  97.         g_mluiInfo.hinstLocRes = hinstParent;
  98. #endif
  99.     }
  100. }
  101. void MLLoadResourcesW(HINSTANCE hinstParent, LPWSTR pszLocResDll)
  102. {
  103. #ifdef RIP
  104.     RIP(hinstParent != NULL);
  105.     RIP(pszLocResDll != NULL);
  106. #endif
  107.     if (g_mluiInfo.hinstLocRes == NULL)
  108.     {
  109. #ifdef MLUI_SUPPORT
  110.         // plugUI: resource dll == ?
  111.         // resource dll must be dynamically determined and loaded.
  112.         // but we are NOT allowed to LoadLibrary during process attach.
  113.         // therefore we cache the info we need and load later when
  114.         // the first resource is requested.
  115.         StrCpyNW(g_mluiInfo.szLocResDll, pszLocResDll, sizeof(g_mluiInfo.szLocResDll)/sizeof(g_mluiInfo.szLocResDll[0]));
  116.         g_mluiInfo.hinstParent = hinstParent;
  117.         g_mluiInfo.dwCrossCodePage = ML_CROSSCODEPAGE;
  118. #else
  119.         // non-plugUI: resource dll == parent dll
  120.         g_mluiInfo.hinstLocRes = hinstParent;
  121. #endif
  122.     }
  123. }
  124. void
  125. MLFreeResources(HINSTANCE hinstParent)
  126. {
  127.     if (g_mluiInfo.hinstLocRes != NULL &&
  128.         g_mluiInfo.hinstLocRes != hinstParent)
  129.     {
  130.         MLClearMLHInstance(g_mluiInfo.hinstLocRes);
  131.         g_mluiInfo.hinstLocRes = NULL;
  132.     }
  133. }
  134. // this is a private internal helper.
  135. // don't you dare call it from anywhere except at
  136. // the beginning of new ML* functions in this file
  137. __inline void
  138. _MLResAssure()
  139. {
  140. #ifdef MLUI_SUPPORT
  141.     if(g_mluiInfo.hinstLocRes == NULL)
  142.     {
  143.         g_mluiInfo.hinstLocRes = MLLoadLibraryW(g_mluiInfo.szLocResDll,
  144.                                                g_mluiInfo.hinstParent,
  145.                                                g_mluiInfo.dwCrossCodePage);
  146.         // we're guaranteed to at least have resources in the install language
  147.         ASSERT(g_mluiInfo.hinstLocRes != NULL);
  148.     }
  149. #endif
  150. }
  151. int
  152. MLLoadStringA(UINT id, LPSTR sz, UINT cchMax)
  153. {
  154.     _MLResAssure();
  155.     return LoadStringA(g_mluiInfo.hinstLocRes, id, sz, cchMax);
  156. }
  157. int
  158. MLLoadStringW(UINT id, LPWSTR sz, UINT cchMax)
  159. {
  160.     _MLResAssure();
  161.     return LoadStringWrapW(g_mluiInfo.hinstLocRes, id, sz, cchMax);
  162. }
  163. int
  164. MLLoadShellLangStringA(UINT id, LPSTR sz, UINT cchMax)
  165. {
  166.     HINSTANCE   hinstShellLangRes;
  167.     int         nRet;
  168.     hinstShellLangRes = MLLoadShellLangResources();
  169.     
  170.     nRet = LoadStringA(hinstShellLangRes, id, sz, cchMax);
  171.     MLFreeLibrary(hinstShellLangRes);
  172.     return nRet;
  173. }
  174. int
  175. MLLoadShellLangStringW(UINT id, LPWSTR sz, UINT cchMax)
  176. {
  177.     HINSTANCE   hinstShellLangRes;
  178.     int         nRet;
  179.     hinstShellLangRes = MLLoadShellLangResources();
  180.     
  181.     nRet = LoadStringWrapW(hinstShellLangRes, id, sz, cchMax);
  182.     MLFreeLibrary(hinstShellLangRes);
  183.     return nRet;
  184. }
  185. HINSTANCE
  186. MLGetHinst()
  187. {
  188.     _MLResAssure();
  189.     return g_mluiInfo.hinstLocRes;
  190. }
  191. HINSTANCE
  192. MLLoadShellLangResources()
  193. {
  194.     HINSTANCE hinst;
  195.     
  196.     hinst = MLLoadLibraryW(g_mluiInfo.szLocResDll,
  197.                            g_mluiInfo.hinstParent,
  198.                            ML_SHELL_LANGUAGE);
  199.     // we're guaranteed to at least have resources in the install language
  200.     // unless we're 100% toasted
  201.     ASSERT(hinst != NULL);
  202.     return hinst;
  203. }
  204. #ifdef MLUI_MESSAGEBOX
  205. int MLShellMessageBox(HWND hWnd, LPCTSTR pszMsg, LPCTSTR pszTitle, UINT fuStyle, ...)
  206. {
  207.     va_list     vaList;
  208.     int         nRet            = 0;
  209.     LPTSTR      pszFormattedMsg = NULL;
  210.     TCHAR       szTitleBuf[256];
  211.     TCHAR       szBuffer[1024];
  212.     //
  213.     // prepare the message
  214.     //
  215.     if (IS_INTRESOURCE(pszMsg))
  216.     {
  217.         if (MLLoadShellLangString(LOWORD((DWORD_PTR)pszMsg), szBuffer, ARRAYSIZE(szBuffer)))
  218.         {
  219.             pszMsg = szBuffer;
  220.         }
  221.     }
  222.     if (!IS_INTRESOURCE(pszMsg) &&  // the string load might have failed
  223.         pszMsg != NULL)
  224.     {
  225.         va_start(vaList, fuStyle);
  226.         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  227.                           pszMsg, 0, 0, (LPTSTR)&pszFormattedMsg, 0, &vaList))
  228.         {
  229.             pszMsg = pszFormattedMsg;
  230.         }
  231.         va_end(vaList);
  232.     }
  233.     //
  234.     // prepare the title
  235.     //
  236.     if (!IS_INTRESOURCE(pszTitle) && pszTitle != NULL)
  237.     {
  238.         // do nothing
  239.     }
  240.     else if (pszTitle != NULL && MLLoadShellLangString(LOWORD((DWORD_PTR)pszTitle), szTitleBuf, ARRAYSIZE(szTitleBuf)))
  241.     {
  242.         pszTitle = szTitleBuf;
  243.     }
  244.     else if (hWnd && GetWindowText(hWnd, szTitleBuf, ARRAYSIZE(szTitleBuf)))
  245.     {
  246.         pszTitle = szTitleBuf;
  247.     }
  248.     else
  249.     {
  250.         pszTitle = TEXT("");
  251.     }
  252.     //
  253.     // launch a MessageBox
  254.     //
  255.     nRet = MessageBox(hWnd, pszFormattedMsg, pszTitle, fuStyle | MB_SETFOREGROUND);
  256.     if (pszFormattedMsg != NULL)
  257.     {
  258.         LocalFree(pszFormattedMsg);
  259.     }
  260.     return nRet;
  261. }
  262. #endif // MLUI_MESSAGEBOX
  263. #include "htmlhelp.h"
  264. HWND 
  265. SHHtmlHelpOnDemandWrapA(HWND hwndCaller, 
  266.                        LPCSTR pszFile,
  267.                        UINT uCommand,
  268.                        DWORD_PTR dwData,
  269.                        DWORD dwCrossCodePage)
  270. {
  271.     BOOL    fEnabled;
  272. #ifdef MLUI_SUPPORT
  273.     fEnabled = TRUE;
  274. #else
  275.     fEnabled = FALSE;
  276. #endif
  277.     return SHHtmlHelpOnDemandA(hwndCaller,
  278.                               pszFile,
  279.                               uCommand,
  280.                               dwData,
  281.                               dwCrossCodePage,
  282.                               fEnabled);
  283. }
  284. HWND 
  285. SHHtmlHelpOnDemandWrapW(HWND hwndCaller, 
  286.                        LPCWSTR pszFile,
  287.                        UINT uCommand,
  288.                        DWORD_PTR dwData,
  289.                        DWORD dwCrossCodePage)
  290. {
  291.     BOOL    fEnabled;
  292. #ifdef MLUI_SUPPORT
  293.     fEnabled = TRUE;
  294. #else
  295.     fEnabled = FALSE;
  296. #endif
  297.     return SHHtmlHelpOnDemandW(hwndCaller,
  298.                               pszFile,
  299.                               uCommand,
  300.                               dwData,
  301.                               dwCrossCodePage,
  302.                               fEnabled);
  303. }
  304. HWND
  305. MLHtmlHelpWrap(HWND hwndCaller,
  306.                LPCTSTR pszFile,
  307.                UINT uCommand,
  308.                DWORD dwData,
  309.                DWORD dwCrossCodePage)
  310. {
  311.     HWND    hwnd;
  312. #ifdef MLUI_SUPPORT
  313.     hwnd = MLHtmlHelp(hwndCaller,
  314.                       pszFile,
  315.                       uCommand,
  316.                       dwData,
  317.                       dwCrossCodePage);
  318. #else
  319.     hwnd = HtmlHelp(hwndCaller,
  320.                     pszFile,
  321.                     uCommand,
  322.                     dwData);
  323. #endif
  324.     return hwnd;
  325. }
  326. BOOL
  327. SHWinHelpOnDemandWrapA(HWND hwndCaller,
  328.                       LPCSTR lpszHelp,
  329.                       UINT uCommand,
  330.                       DWORD_PTR dwData)
  331. {
  332.     BOOL    fEnabled;
  333. #ifdef MLUI_SUPPORT
  334.     fEnabled = TRUE;
  335. #else
  336.     fEnabled = FALSE;
  337. #endif
  338.     return SHWinHelpOnDemandA(hwndCaller,
  339.                              lpszHelp,
  340.                              uCommand,
  341.                              dwData,
  342.                              fEnabled);
  343. }
  344. BOOL
  345. SHWinHelpOnDemandWrapW(HWND hwndCaller,
  346.                       LPCWSTR lpszHelp,
  347.                       UINT uCommand,
  348.                       DWORD_PTR dwData)
  349. {
  350.     BOOL    fEnabled;
  351. #ifdef MLUI_SUPPORT
  352.     fEnabled = TRUE;
  353. #else
  354.     fEnabled = FALSE;
  355. #endif
  356.     return SHWinHelpOnDemandW(hwndCaller,
  357.                              lpszHelp,
  358.                              uCommand,
  359.                              dwData,
  360.                              fEnabled);
  361. }
  362. BOOL
  363. MLWinHelpWrap(HWND hwndCaller,
  364.                    LPCTSTR lpszHelp,
  365.                    UINT uCommand,
  366.                    DWORD dwData)
  367. {
  368.     BOOL    fRet;
  369. #ifdef MLUI_SUPPORT
  370.     fRet = MLWinHelp(hwndCaller,
  371.                      lpszHelp,
  372.                      uCommand,
  373.                      dwData);
  374. #else
  375.     fRet = WinHelp(hwndCaller,
  376.                    lpszHelp,
  377.                    uCommand,
  378.                    dwData);
  379. #endif
  380.     return fRet;
  381. }
  382. HRESULT
  383. MLBuildResURLWrapA(LPSTR    pszLibFile,
  384.                    HMODULE  hModule,
  385.                    DWORD    dwCrossCodePage,
  386.                    LPSTR    pszResName,
  387.                    LPSTR    pszResURL,
  388.                    int      nBufSize,
  389.                    LPSTR    pszParentDll)
  390. {
  391.     HRESULT hr;
  392. #ifdef MLUI_SUPPORT
  393.     hr = MLBuildResURLA(pszLibFile,
  394.                         hModule,
  395.                         dwCrossCodePage,
  396.                         pszResName,
  397.                         pszResURL,
  398.                         nBufSize);
  399. #else
  400.     wnsprintfA(pszResURL, nBufSize, "res://%s/%s", pszParentDll, pszResName);
  401.     hr = S_OK;
  402. #endif
  403.     return hr;
  404. }
  405. HRESULT
  406. MLBuildResURLWrapW(LPWSTR   pszLibFile,
  407.                    HMODULE  hModule,
  408.                    DWORD    dwCrossCodePage,
  409.                    LPWSTR   pszResName,
  410.                    LPWSTR   pszResURL,
  411.                    int      nBufSize,
  412.                    LPWSTR   pszParentDll)
  413. {
  414.     HRESULT hr;
  415. #ifdef MLUI_SUPPORT
  416.     hr = MLBuildResURLW(pszLibFile,
  417.                         hModule,
  418.                         dwCrossCodePage,
  419.                         pszResName,
  420.                         pszResURL,
  421.                         nBufSize);
  422. #else
  423.     wnsprintfW(pszResURL, nBufSize, L"res://%s/%s", pszParentDll, pszResName);
  424.     hr = S_OK;
  425. #endif
  426.     return hr;
  427. }
  428. #endif  // MLUI_INIT
  429. #ifdef __cplusplus
  430. };
  431. #endif
  432. #endif  // _INC_MLUISUPP