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.

120 lines
3.2 KiB

  1. #ifndef RES_STRING_H
  2. #define RES_STRING_H
  3. #ifdef USE_STDAFX
  4. #include "stdafx.h"
  5. #else
  6. #include <windows.h>
  7. #endif
  8. #include "TSync.hpp"
  9. #define MAX_STRING_SIZE (5000)
  10. #include "Mcs.h"
  11. #include "McsRes.h"
  12. #include "UString.hpp"
  13. #include "folders.h"
  14. using namespace nsFolders;
  15. class StringLoader
  16. {
  17. HINSTANCE m_hInstance; // handle to resources to load from
  18. TCriticalSection m_cs;
  19. WCHAR m_buffer[MAX_STRING_SIZE];
  20. DWORD rc;
  21. public:
  22. StringLoader()
  23. {
  24. WCHAR fullpath[400];
  25. DWORD lenValue = sizeof(fullpath);
  26. DWORD type;
  27. LONG lRet = 0;
  28. // first, try to load from our install directory
  29. HKEY hKey;
  30. m_hInstance = NULL;
  31. lRet = RegOpenKey((HKEY)HKEY_LOCAL_MACHINE,REGKEY_ADMT,&hKey);
  32. if ( !lRet )
  33. {
  34. lRet = RegQueryValueEx(hKey,L"Directory",0,&type,(LPBYTE)fullpath,&lenValue);
  35. if (!lRet )
  36. {
  37. if (type == REG_SZ)
  38. {
  39. // ensure NULL termination
  40. fullpath[(lenValue >= sizeof(WCHAR)) ? ((lenValue/sizeof(WCHAR))-1) : 0] = L'\0';
  41. //make sure we have enough space in the buffer
  42. if ((UStrLen(fullpath) + UStrLen(L"McsDmRes.DLL")) < (sizeof(fullpath)/sizeof(WCHAR)))
  43. {
  44. UStrCpy(fullpath+UStrLen(fullpath),L"McsDmRes.DLL");
  45. m_hInstance = LoadLibrary(fullpath);
  46. }
  47. }
  48. }
  49. RegCloseKey(hKey);
  50. }
  51. // If that fails, see if there's one anywhere in the path
  52. if ( ! m_hInstance )
  53. {
  54. m_hInstance = LoadLibrary(L"McsDmRes.DLL");
  55. }
  56. if (! m_hInstance )
  57. {
  58. MCSASSERTSZ(FALSE,"Failed to load McsDmRes.DLL");
  59. rc = GetLastError();
  60. }
  61. }
  62. WCHAR * GetString(UINT nID)
  63. {
  64. int len;
  65. WCHAR * result = NULL;
  66. m_cs.Enter();
  67. m_buffer[0] = 0;
  68. len = LoadString(m_hInstance,nID,m_buffer,MAX_STRING_SIZE);
  69. if (! len )
  70. {
  71. // DWORD rc = GetLastError();
  72. GetLastError();
  73. }
  74. result = new WCHAR[len+1];
  75. if (!result)
  76. {
  77. m_cs.Leave();
  78. _com_issue_error(E_OUTOFMEMORY);
  79. return NULL;
  80. }
  81. wcscpy(result,m_buffer);
  82. m_cs.Leave();
  83. return result;
  84. }
  85. };
  86. extern StringLoader gString;
  87. class TempString
  88. {
  89. WCHAR * m_data;
  90. public:
  91. TempString(WCHAR * data) { m_data = data; }
  92. ~TempString() { if ( m_data ) delete [] m_data; }
  93. operator WCHAR * () { return m_data; }
  94. operator WCHAR const * () { return (WCHAR const*)m_data; }
  95. };
  96. //#define GET_BSTR(nID) _bstr_t(SysAllocString(GET_STRING(nID)),false)
  97. #define GET_BSTR(nID) _bstr_t((WCHAR*)TempString(gString.GetString(nID)))
  98. #define GET_STRING(nID) GET_STRING2(gString,nID)
  99. #define GET_STRING2(strObj,nID) TempString(strObj.GetString(nID))
  100. #define GET_WSTR(nID) ((WCHAR*)TempString(gString.GetString(nID)))
  101. #endif RES_STRING_H