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.

216 lines
5.5 KiB

  1. //=--------------------------------------------------------------------------=
  2. // Util.C
  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 routines that we will find useful.
  13. //
  14. #include "stdafx.h" // not really used here, but NT Build env. doesn't like
  15. // some files in a dir to have pre-comp hdrs & some not
  16. #include "IPServer.H"
  17. #include "Globals.H"
  18. #include "Util.H"
  19. SZTHISFILE
  20. //=---------------------------------------------------------------------------=
  21. // overloaded new
  22. //=---------------------------------------------------------------------------=
  23. // for the retail case, we'll just use the win32 Local* heap management
  24. // routines for speed and size
  25. //
  26. // Parameters:
  27. // size_t - [in] what size do we alloc
  28. //
  29. // Output:
  30. // VOID * - new memoery.
  31. //
  32. // Notes:
  33. //
  34. void * _cdecl operator new
  35. (
  36. size_t size
  37. )
  38. {
  39. return malloc(size);
  40. }
  41. //=---------------------------------------------------------------------------=
  42. // overloaded delete
  43. //=---------------------------------------------------------------------------=
  44. // retail case just uses win32 Local* heap mgmt functions
  45. //
  46. // Parameters:
  47. // void * - [in] free me!
  48. //
  49. // Notes:
  50. //
  51. void _cdecl operator delete ( void *ptr)
  52. {
  53. free(ptr);
  54. }
  55. //=--------------------------------------------------------------------------=
  56. // MakeWideFromAnsi
  57. //=--------------------------------------------------------------------------=
  58. // given a string, make a BSTR out of it.
  59. //
  60. // Parameters:
  61. // LPSTR - [in]
  62. // BYTE - [in]
  63. //
  64. // Output:
  65. // LPWSTR - needs to be cast to final desired result
  66. //
  67. // Notes:
  68. //
  69. LPWSTR MakeWideStrFromAnsi
  70. (
  71. LPSTR psz,
  72. BYTE bType
  73. )
  74. {
  75. LPWSTR pwsz;
  76. int i;
  77. // arg checking.
  78. //
  79. if (!psz)
  80. return NULL;
  81. // compute the length of the required BSTR
  82. //
  83. i = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
  84. if (i <= 0) return NULL;
  85. // allocate the widestr, +1 for terminating null
  86. //
  87. switch (bType) {
  88. case STR_BSTR:
  89. pwsz = (LPWSTR) SysAllocStringLen(NULL, i);
  90. break;
  91. case STR_OLESTR:
  92. pwsz = (LPWSTR) g_pMalloc->Alloc(i * sizeof(WCHAR));
  93. break;
  94. default:
  95. FAIL("Bogus String Type. Somebody needs to learn how to program");
  96. }
  97. if (!pwsz) return NULL;
  98. MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i);
  99. pwsz[i - 1] = 0;
  100. return pwsz;
  101. }
  102. //=--------------------------------------------------------------------------=
  103. // MakeWideStrFromResId
  104. //=--------------------------------------------------------------------------=
  105. // given a resource ID, load it, and allocate a wide string for it.
  106. //
  107. // Parameters:
  108. // WORD - [in] resource id.
  109. // BYTE - [in] type of string desired.
  110. //
  111. // Output:
  112. // LPWSTR - needs to be cast to desired string type.
  113. //
  114. // Notes:
  115. //
  116. /*LPWSTR MakeWideStrFromResourceId
  117. (
  118. WORD wId,
  119. BYTE bType
  120. )
  121. {
  122. int i;
  123. char szTmp[512];
  124. // load the string from the resources.
  125. //
  126. i = LoadString(GetResourceHandle(), wId, szTmp, 512);
  127. if (!i) return NULL;
  128. return MakeWideStrFromAnsi(szTmp, bType);
  129. }
  130. */
  131. //=--------------------------------------------------------------------------=
  132. // MakeWideStrFromWide
  133. //=--------------------------------------------------------------------------=
  134. // given a wide string, make a new wide string with it of the given type.
  135. //
  136. // Parameters:
  137. // LPWSTR - [in] current wide str.
  138. // BYTE - [in] desired type of string.
  139. //
  140. // Output:
  141. // LPWSTR
  142. //
  143. // Notes:
  144. //
  145. LPWSTR MakeWideStrFromWide
  146. (
  147. LPWSTR pwsz,
  148. BYTE bType
  149. )
  150. {
  151. LPWSTR pwszTmp;
  152. int i;
  153. if (!pwsz) return NULL;
  154. // just copy the string, depending on what type they want.
  155. //
  156. switch (bType) {
  157. case STR_OLESTR:
  158. i = lstrlenW(pwsz);
  159. pwszTmp = (LPWSTR)g_pMalloc->Alloc((i * sizeof(WCHAR)) + 1);
  160. if (!pwszTmp) return NULL;
  161. memcpy(pwszTmp, pwsz, (sizeof(WCHAR) * i) + 1);
  162. break;
  163. case STR_BSTR:
  164. pwszTmp = (LPWSTR)SysAllocString(pwsz);
  165. break;
  166. }
  167. return pwszTmp;
  168. }
  169. //=--------------------------------------------------------------------------=
  170. // StringFromGuidA
  171. //=--------------------------------------------------------------------------=
  172. // returns an ANSI string from a CLSID or GUID
  173. //
  174. // Parameters:
  175. // REFIID - [in] clsid to make string out of.
  176. // LPSTR - [in] buffer in which to place resultant GUID.
  177. //
  178. // Output:
  179. // int - number of chars written out.
  180. //
  181. // Notes:
  182. //
  183. int StringFromGuidA
  184. (
  185. REFIID riid,
  186. LPSTR pszBuf
  187. )
  188. {
  189. return wsprintf((char *)pszBuf, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", riid.Data1,
  190. riid.Data2, riid.Data3, riid.Data4[0], riid.Data4[1], riid.Data4[2],
  191. riid.Data4[3], riid.Data4[4], riid.Data4[5], riid.Data4[6], riid.Data4[7]);
  192. }