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.

105 lines
4.0 KiB

  1. //=--------------------------------------------------------------------------=
  2. // Util.H
  3. //=--------------------------------------------------------------------------=
  4. // Copyright 1995 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //=--------------------------------------------------------------------------=
  11. //
  12. // contains utilities that we will find useful.
  13. //
  14. #ifndef _UTIL_H_
  15. #include "globals.h"
  16. //=--------------------------------------------------------------------------=
  17. // miscellaneous [useful] numerical constants
  18. //=--------------------------------------------------------------------------=
  19. // the length of a guid once printed out with -'s, leading and trailing bracket,
  20. // plus 1 for NULL
  21. //
  22. #define GUID_STR_LEN 40
  23. //=--------------------------------------------------------------------------=
  24. // allocates a temporary buffer that will disappear when it goes out of scope
  25. // NOTE: be careful of that -- make sure you use the string in the same or
  26. // nested scope in which you created this buffer. people should not use this
  27. // class directly. use the macro(s) below.
  28. //
  29. class TempBuffer {
  30. public:
  31. TempBuffer(ULONG cBytes) {
  32. m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : HeapAlloc(g_hHeap, 0, cBytes);
  33. m_fHeapAlloc = (cBytes > 120);
  34. }
  35. ~TempBuffer() {
  36. if (m_pBuf && m_fHeapAlloc) HeapFree(g_hHeap, 0, m_pBuf);
  37. }
  38. void *GetBuffer() {
  39. return m_pBuf;
  40. }
  41. private:
  42. void *m_pBuf;
  43. // we'll use this temp buffer for small cases.
  44. //
  45. char m_szTmpBuf[120];
  46. unsigned m_fHeapAlloc:1;
  47. };
  48. //=--------------------------------------------------------------------------=
  49. // string helpers.
  50. //
  51. // given and ANSI String, copy it into a wide buffer.
  52. // be careful about scoping when using this macro!
  53. //
  54. // how to use the below two macros:
  55. //
  56. // ...
  57. // LPSTR pszA;
  58. // pszA = MyGetAnsiStringRoutine();
  59. // MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  60. // MyUseWideStringRoutine(pwsz);
  61. // ...
  62. //
  63. // similarily for MAKE_ANSIPTR_FROMWIDE. note that the first param does not
  64. // have to be declared, and no clean up must be done.
  65. //
  66. #define MAKE_WIDEPTR_FROMANSI(ptrname, ansistr) \
  67. long __l##ptrname = (lstrlen(ansistr) + 1) * sizeof(WCHAR); \
  68. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  69. MultiByteToWideChar(CP_ACP, 0, ansistr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  70. LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  71. #define MAKE_MBCSPTR_FROMWIDE(ptrname, widestr) \
  72. long __l##ptrname = (lstrlenW(widestr) + 1) * sizeof(char); \
  73. TempBuffer __TempBuffer##ptrname(__l##ptrname * 2); \
  74. WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname * 2, NULL, NULL); \
  75. LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  76. #define GET_MBCSLEN_FROMWIDE(widestr) \
  77. WideCharToMultiByte(CP_ACP, 0, widestr, -1, NULL, 0, NULL, NULL)
  78. #define STR_BSTR 0
  79. #define STR_OLESTR 1
  80. #define BSTRFROMANSI(x) (BSTR)MakeWideStrFromAnsi((LPSTR)(x), STR_BSTR)
  81. #define OLESTRFROMANSI(x) (LPOLESTR)MakeWideStrFromAnsi((LPSTR)(x), STR_OLESTR)
  82. #define BSTRFROMRESID(x) (BSTR)MakeWideStrFromResourceId(x, STR_BSTR)
  83. #define OLESTRFROMRESID(x) (LPOLESTR)MakeWideStrFromResourceId(x, STR_OLESTR)
  84. #define COPYOLESTR(x) (LPOLESTR)MakeWideStrFromWide(x, STR_OLESTR)
  85. #define COPYBSTR(x) (BSTR)MakeWideStrFromWide(x, STR_BSTR)
  86. LPWSTR MakeWideStrFromAnsi(LPSTR, BYTE bType);
  87. LPWSTR MakeWideStrFromResourceId(WORD, BYTE bType);
  88. LPWSTR MakeWideStrFromWide(LPWSTR, BYTE bType);
  89. // takes a GUID, and a pointer to a buffer, and places the string form of the
  90. // GUID in said buffer.
  91. //
  92. int StringFromGuidA(REFIID, LPSTR);
  93. #define _UTIL_H_
  94. #endif // _UTIL_H_