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.

123 lines
4.2 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. //
  6. //
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  14. //
  15. //
  16. //
  17. //==============================================================;
  18. #ifndef _MMC_GLOBALS_H
  19. #define _MMC_GLOBALS_H
  20. #include <tchar.h>
  21. #ifndef STRINGS_ONLY
  22. #define IDM_BUTTON1 0x100
  23. #define IDM_BUTTON2 0x101
  24. extern HINSTANCE g_hinst;
  25. extern ULONG g_uObjects;
  26. #define OBJECT_CREATED InterlockedIncrement((long *)&g_uObjects);
  27. #define OBJECT_DESTROYED InterlockedDecrement((long *)&g_uObjects);
  28. #endif
  29. //=--------------------------------------------------------------------------=
  30. // allocates a temporary buffer that will disappear when it goes out of scope
  31. // NOTE: be careful of that -- make sure you use the string in the same or
  32. // nested scope in which you created this buffer. people should not use this
  33. // class directly. use the macro(s) below.
  34. //
  35. class TempBuffer {
  36. public:
  37. TempBuffer(ULONG cBytes) {
  38. m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : HeapAlloc(GetProcessHeap(), 0, cBytes);
  39. m_fHeapAlloc = (cBytes > 120);
  40. }
  41. ~TempBuffer() {
  42. if (m_pBuf && m_fHeapAlloc) HeapFree(GetProcessHeap(), 0, m_pBuf);
  43. }
  44. void *GetBuffer() {
  45. return m_pBuf;
  46. }
  47. private:
  48. void *m_pBuf;
  49. // we'll use this temp buffer for small cases.
  50. //
  51. char m_szTmpBuf[120];
  52. unsigned m_fHeapAlloc:1;
  53. };
  54. //=--------------------------------------------------------------------------=
  55. // string helpers.
  56. //
  57. // given a _TCHAR, copy it into a wide buffer.
  58. // be careful about scoping when using this macro!
  59. //
  60. // how to use the below two macros:
  61. //
  62. // ...
  63. // LPTSTR pszT;
  64. // pszT = MyGetTStringRoutine();
  65. // MAKE_WIDEPTR_FROMSTR(pwsz, pszT);
  66. // MyUseWideStringRoutine(pwsz);
  67. // ...
  68. #ifdef UNICODE
  69. #define MAKE_WIDEPTR_FROMTSTR(ptrname, tstr) \
  70. long __l##ptrname = (lstrlenW(tstr) + 1) * sizeof(WCHAR); \
  71. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  72. lstrcpyW((LPWSTR)__TempBuffer##ptrname.GetBuffer(), tstr); \
  73. LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  74. #else // ANSI
  75. #define MAKE_WIDEPTR_FROMTSTR(ptrname, tstr) \
  76. long __l##ptrname = (lstrlenA(tstr) + 1) * sizeof(WCHAR); \
  77. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  78. MultiByteToWideChar(CP_ACP, 0, tstr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  79. LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  80. #endif
  81. #ifdef UNICODE
  82. #define MAKE_WIDEPTR_FROMTSTR_ALLOC(ptrname, tstr) \
  83. long __l##ptrname = (lstrlenW(tstr) + 1) * sizeof(WCHAR); \
  84. LPWSTR ptrname = (LPWSTR)CoTaskMemAlloc(__l##ptrname); \
  85. lstrcpyW((LPWSTR)ptrname, tstr)
  86. #else // ANSI
  87. #define MAKE_WIDEPTR_FROMTSTR_ALLOC(ptrname, tstr) \
  88. long __l##ptrname = (lstrlenA(tstr) + 1) * sizeof(WCHAR); \
  89. LPWSTR ptrname = (LPWSTR)CoTaskMemAlloc(__l##ptrname); \
  90. MultiByteToWideChar(CP_ACP, 0, tstr, -1, ptrname, __l##ptrname)
  91. #endif
  92. //
  93. // similarily for MAKE_TSTRPTR_FROMWIDE. note that the first param does not
  94. // have to be declared, and no clean up must be done.
  95. //
  96. // * 2 for DBCS handling in below length computation
  97. //
  98. #ifdef UNICODE
  99. #define MAKE_TSTRPTR_FROMWIDE(ptrname, widestr) \
  100. long __l##ptrname = (wcslen(widestr) + 1) * 2 * sizeof(TCHAR); \
  101. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  102. lstrcpyW((LPTSTR)__TempBuffer##ptrname.GetBuffer(), widestr); \
  103. LPTSTR ptrname = (LPTSTR)__TempBuffer##ptrname.GetBuffer()
  104. #else // ANSI
  105. #define MAKE_TSTRPTR_FROMWIDE(ptrname, widestr) \
  106. long __l##ptrname = (wcslen(widestr) + 1) * 2 * sizeof(TCHAR); \
  107. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  108. WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
  109. LPTSTR ptrname = (LPTSTR)__TempBuffer##ptrname.GetBuffer()
  110. #endif
  111. #endif // _MMC_GLOBALS_H