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.

217 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. seoutil.cpp
  5. Abstract:
  6. This module contains the implementation for various utility
  7. functions.
  8. Author:
  9. Don Dumitru (dondu@microsoft.com)
  10. Revision History:
  11. dondu 10/24/96 created
  12. --*/
  13. #include "stdafx.h"
  14. #include "seodefs.h"
  15. static IMalloc *g_piMalloc;
  16. void MyMallocTerm() {
  17. if (g_piMalloc) {
  18. g_piMalloc->Release();
  19. g_piMalloc = NULL;
  20. }
  21. }
  22. BOOL MyMallocInit() {
  23. HRESULT hrRes;
  24. IMalloc *piMalloc;
  25. if (!g_piMalloc) {
  26. hrRes = CoGetMalloc(1,&piMalloc);
  27. if (SUCCEEDED(hrRes)) {
  28. if (InterlockedCompareExchangePointer((void**)&g_piMalloc,piMalloc,NULL) != NULL) {
  29. piMalloc->Release();
  30. }
  31. }
  32. }
  33. return (g_piMalloc?TRUE:FALSE);
  34. }
  35. LPVOID MyMalloc(size_t cbBytes) {
  36. LPVOID pvRes;
  37. if (!MyMallocInit()) {
  38. return (NULL);
  39. }
  40. pvRes = g_piMalloc->Alloc(cbBytes);
  41. if (pvRes) {
  42. ZeroMemory(pvRes,g_piMalloc->GetSize(pvRes));
  43. }
  44. return (pvRes);
  45. }
  46. LPVOID MyRealloc(LPVOID pvBlock, size_t cbBytes) {
  47. size_t ulPrevSize = 0;
  48. size_t ulNewSize = 0;
  49. LPVOID pvRes;
  50. if (!MyMallocInit()) {
  51. return (NULL);
  52. }
  53. if (pvBlock) {
  54. ulPrevSize = g_piMalloc->GetSize(pvBlock);
  55. if (ulPrevSize == (size_t) -1) {
  56. ulPrevSize = 0;
  57. }
  58. }
  59. pvRes = g_piMalloc->Realloc(pvBlock,cbBytes);
  60. if (pvRes) {
  61. ulNewSize = g_piMalloc->GetSize(pvRes);
  62. if (ulNewSize == (size_t) -1) {
  63. ulNewSize = 0;
  64. }
  65. if (ulNewSize > ulPrevSize) {
  66. ZeroMemory(((LPBYTE) pvRes)+ulPrevSize,ulNewSize-ulPrevSize);
  67. }
  68. }
  69. return (pvRes);
  70. }
  71. BOOL MyReallocInPlace(LPVOID pvPtrToPtrToBlock, size_t cbBytes) {
  72. LPVOID pvRes;
  73. pvRes = MyRealloc(*((LPVOID *) pvPtrToPtrToBlock),cbBytes);
  74. if (pvRes || (*((LPVOID *) pvPtrToPtrToBlock) && !cbBytes)) {
  75. *((LPVOID *) pvPtrToPtrToBlock) = pvRes;
  76. return (TRUE);
  77. }
  78. return (FALSE);
  79. }
  80. void MyFree(LPVOID pvBlock) {
  81. if (!g_piMalloc ) {
  82. return;
  83. }
  84. FillMemory(pvBlock,g_piMalloc->GetSize(pvBlock),0xe4);
  85. g_piMalloc->Free(pvBlock);
  86. }
  87. void MyFreeInPlace(LPVOID pvPtrToPtrToBlock) {
  88. if(*((LPVOID *) pvPtrToPtrToBlock)) { // If there's something to free
  89. MyFree(*((LPVOID *) pvPtrToPtrToBlock));
  90. *((LPVOID *) pvPtrToPtrToBlock) = NULL;
  91. }
  92. }
  93. void MySysFreeStringInPlace(BSTR *pstrBlock) {
  94. if (*pstrBlock) {
  95. FillMemory(*pstrBlock,SysStringByteLen(*pstrBlock),0xe4);
  96. }
  97. SysFreeString(*pstrBlock);
  98. *pstrBlock = NULL;
  99. }
  100. // Coerce a Variant into the desired type in-place
  101. void VariantCoerce(VARIANTARG &var, VARTYPE varType) {
  102. if(var.vt != varType) { // Only if not already right type
  103. HRESULT hr = VariantChangeType(&var, &var, 0, varType);
  104. if(FAILED(hr)) VariantClear(&var);
  105. }
  106. }
  107. // Turn the IUnknown parameter into an ISEODictionary
  108. ISEODictionary *GetDictionary(IUnknown *piUnk) {
  109. if(!piUnk) return 0; // Nothing to query
  110. ISEODictionary *newBag = 0;
  111. HRESULT hr = piUnk->QueryInterface(IID_ISEODictionary, (void **) &newBag);
  112. if(FAILED(hr)) {
  113. _ASSERT(!newBag); // QI failed, so shouldn't have touched the pointer
  114. newBag = 0; // But make sure
  115. } else {
  116. _ASSERT(newBag); // Should be set, since function succeeded
  117. }
  118. return newBag;
  119. }
  120. // Read a subkey from an ISEODictionary and return it as another ISEODictionary
  121. ISEODictionary *ReadSubBag(ISEODictionary *bag, LPCSTR str) {
  122. if(!bag) return 0;
  123. TraceFunctEnter("ReadSubBag");
  124. ISEODictionary *pNewBag = 0;
  125. HRESULT hr = bag->GetInterfaceA(str, IID_ISEODictionary, (IUnknown **) &pNewBag);
  126. if(FAILED(hr)) FunctTrace(0, "No entry for %s found", str);
  127. TraceFunctLeave();
  128. return pNewBag;
  129. }
  130. // Read a string from the Dictionary.
  131. HRESULT ReadString(ISEODictionary *bag, LPCSTR property,
  132. LPSTR psBuf, LPDWORD dwCount) {
  133. if(!bag) return 0;
  134. TraceFunctEnter("ReadString");
  135. HRESULT hr = bag->GetStringA(property, dwCount, psBuf);
  136. if(FAILED(hr)) FunctTrace(0, "No %s specified", property);
  137. TraceFunctLeave();
  138. return hr;
  139. }
  140. // Given a CLSID as a string, create an object of that CLSID
  141. void *CreateFromString(LPCOLESTR str, REFIID iface) {
  142. TraceFunctEnter("CreateFromString");
  143. void *object = 0;
  144. CLSID thisCLSID;
  145. HRESULT hr = CLSIDFromString((LPOLESTR) str, &thisCLSID);
  146. if(SUCCEEDED(hr)) {
  147. hr = CoCreateInstance(thisCLSID, 0, CLSCTX_ALL, iface, &object);
  148. if(FAILED(hr)) {
  149. FunctTrace(0, "CoCreateInstance failed for CLSID: %s", str);
  150. _ASSERT(!object); // CoCreateInstance shouldn't have changed this
  151. object = 0; // Just to make sure
  152. }
  153. } else {
  154. FunctTrace(0, "Could not convert string to CLSID, for: %s", str);
  155. }
  156. TraceFunctLeave();
  157. return object;
  158. }