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.

156 lines
4.0 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. LocRes.cpp
  5. Abstract:
  6. This file contains the implementation of functions to ease localization.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 06/17/2000
  9. created
  10. ******************************************************************************/
  11. #include "stdafx.h"
  12. ////////////////////////////////////////////////////////////////////////////////
  13. #define ENSURE_MODULE() \
  14. if(g_hModule == NULL) \
  15. { \
  16. HRESULT hr; \
  17. \
  18. if(FAILED(hr = LocalizeInit())) return hr; \
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////
  21. static HINSTANCE g_hModule;
  22. HRESULT MPC::LocalizeInit( LPCWSTR szFile )
  23. {
  24. g_hModule = ::LoadLibraryW( szFile ? szFile : L"HCAppRes.dll" );
  25. if(g_hModule == NULL)
  26. {
  27. return HRESULT_FROM_WIN32(::GetLastError());
  28. }
  29. return S_OK;
  30. }
  31. HRESULT MPC::LocalizeString( /*[in]*/ UINT uID ,
  32. /*[in]*/ LPSTR lpBuf ,
  33. /*[in]*/ int nBufMax ,
  34. /*[in]*/ bool fMUI )
  35. {
  36. MPC::Impersonation imp;
  37. ENSURE_MODULE();
  38. if(fMUI)
  39. {
  40. if(SUCCEEDED(imp.Initialize())) imp.Impersonate();
  41. }
  42. if(::LoadStringA( g_hModule, uID, lpBuf, nBufMax ) == 0) return E_FAIL;
  43. return S_OK;
  44. }
  45. HRESULT MPC::LocalizeString( /*[in]*/ UINT uID ,
  46. /*[in]*/ LPWSTR lpBuf ,
  47. /*[in]*/ int nBufMax ,
  48. /*[in]*/ bool fMUI )
  49. {
  50. MPC::Impersonation imp;
  51. ENSURE_MODULE();
  52. if(fMUI)
  53. {
  54. if(SUCCEEDED(imp.Initialize())) imp.Impersonate();
  55. }
  56. if(::LoadStringW( g_hModule, uID, lpBuf, nBufMax ) == 0) return E_FAIL;
  57. return S_OK;
  58. }
  59. HRESULT MPC::LocalizeString( /*[in ]*/ UINT uID ,
  60. /*[out]*/ MPC::string& szStr ,
  61. /*[in ]*/ bool fMUI )
  62. {
  63. CHAR rgTmp[512];
  64. HRESULT hr;
  65. if(SUCCEEDED(hr = LocalizeString( uID, rgTmp, MAXSTRLEN(rgTmp), fMUI )))
  66. {
  67. szStr = rgTmp;
  68. }
  69. return hr;
  70. }
  71. HRESULT MPC::LocalizeString( /*[in ]*/ UINT uID ,
  72. /*[out]*/ MPC::wstring& szStr ,
  73. /*[in ]*/ bool fMUI )
  74. {
  75. WCHAR rgTmp[512];
  76. HRESULT hr;
  77. if(SUCCEEDED(hr = LocalizeString( uID, rgTmp, MAXSTRLEN(rgTmp), fMUI )))
  78. {
  79. szStr = rgTmp;
  80. }
  81. return hr;
  82. }
  83. HRESULT MPC::LocalizeString( /*[in ]*/ UINT uID ,
  84. /*[out]*/ CComBSTR& bstrStr ,
  85. /*[in ]*/ bool fMUI )
  86. {
  87. WCHAR rgTmp[512];
  88. HRESULT hr;
  89. if(SUCCEEDED(hr = LocalizeString( uID, rgTmp, MAXSTRLEN(rgTmp), fMUI )))
  90. {
  91. bstrStr = rgTmp;
  92. }
  93. return hr;
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. int MPC::LocalizedMessageBox( UINT uID_Title, UINT uID_Msg, UINT uType )
  97. {
  98. MPC::wstring szTitle; MPC::LocalizeString( uID_Title, szTitle );
  99. MPC::wstring szMsg; MPC::LocalizeString( uID_Msg , szMsg );
  100. return ::MessageBoxW( NULL, szMsg.c_str(), szTitle.c_str(), uType );
  101. }
  102. int MPC::LocalizedMessageBoxFmt( UINT uID_Title, UINT uID_Msg, UINT uType, ... )
  103. {
  104. MPC::wstring szTitle; MPC::LocalizeString( uID_Title, szTitle );
  105. MPC::wstring szMsg; MPC::LocalizeString( uID_Msg , szMsg );
  106. WCHAR rgLine[512];
  107. va_list arglist;
  108. //
  109. // Format the log line.
  110. //
  111. va_start( arglist, uID_Msg );
  112. StringCchVPrintfW( rgLine, ARRAYSIZE(rgLine), szMsg.c_str(), arglist );
  113. va_end( arglist );
  114. return ::MessageBoxW( NULL, rgLine, szTitle.c_str(), uType );
  115. }