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.

253 lines
5.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: testclean.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // code to delete the contents of a container
  11. //
  12. //
  13. #ifdef UNICODE
  14. #define _UNICODE 1
  15. #endif
  16. //#include <sys/types.h>
  17. //#include <sys/stat.h>
  18. //
  19. // CRunTime Includes
  20. //
  21. //#include <limits.h>
  22. //#include <io.h>
  23. #include <stdio.h>
  24. #include <objbase.h>
  25. #include <activeds.h>
  26. #include <rpc.h>
  27. #include <ole2.h>
  28. #include "oledb.h"
  29. #include "oledberr.h"
  30. #include "msdadc.h"
  31. #define RETURN_ON_FAILURE(hr) \
  32. if (FAILED(hr)) { \
  33. return hr; \
  34. }
  35. #define _MAX_LENGTH 500
  36. #define MAX_ADS_ENUM 10
  37. #define CLASSNAME L"objectClass"
  38. HRESULT
  39. GetNextEnum(
  40. IEnumVARIANT * pEnum,
  41. IADs **ppADs
  42. )
  43. {
  44. HRESULT hr;
  45. VARIANT VariantArray[MAX_ADS_ENUM];
  46. IDispatch *pDispatch = NULL;
  47. hr = ADsEnumerateNext(
  48. pEnum,
  49. 1,
  50. VariantArray,
  51. NULL
  52. );
  53. if (hr == S_FALSE)
  54. return hr;
  55. RETURN_ON_FAILURE(hr);
  56. pDispatch = VariantArray[0].pdispVal;
  57. memset(VariantArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
  58. hr = pDispatch->QueryInterface(IID_IADs, (void **) ppADs) ;
  59. pDispatch->Release();
  60. return(S_OK);
  61. }
  62. HRESULT GetPropertyListAlloc(IADs *pADs, LPOLESTR pszPropName,
  63. DWORD *pCount, LPOLESTR **ppList)
  64. {
  65. LONG dwSLBound = 0;
  66. LONG dwSUBound = 0;
  67. VARIANT v;
  68. LONG i;
  69. HRESULT hr = S_OK;
  70. VARIANT var;
  71. *pCount = 0;
  72. VariantInit(&var);
  73. hr = pADs->Get(pszPropName, &var);
  74. if (hr == E_ADS_PROPERTY_NOT_FOUND)
  75. {
  76. return S_OK;
  77. }
  78. RETURN_ON_FAILURE(hr);
  79. if(!((V_VT(&var) & VT_VARIANT)))
  80. {
  81. return(E_FAIL);
  82. }
  83. //
  84. // The following is a work around for the package detail field
  85. //
  86. if (!V_ISARRAY(&var))
  87. {
  88. (*ppList) = (LPOLESTR *) CoTaskMemAlloc(sizeof(LPOLESTR));
  89. *pCount = 1;
  90. *(*ppList) = (LPOLESTR) CoTaskMemAlloc (sizeof(WCHAR) * (wcslen(var.bstrVal)+1));
  91. wcscpy (*(*ppList), var.bstrVal);
  92. VariantClear(&var);
  93. return S_OK;
  94. }
  95. //
  96. // Check that there is only one dimension in this array
  97. //
  98. if ((V_ARRAY(&var))->cDims != 1)
  99. {
  100. return E_FAIL;
  101. }
  102. //
  103. // Check that there is atleast one element in this array
  104. //
  105. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0)
  106. {
  107. return S_OK; // was E_FAIL;
  108. }
  109. //
  110. // We know that this is a valid single dimension array
  111. //
  112. hr = SafeArrayGetLBound(V_ARRAY(&var),
  113. 1,
  114. (long FAR *)&dwSLBound
  115. );
  116. RETURN_ON_FAILURE(hr);
  117. hr = SafeArrayGetUBound(V_ARRAY(&var),
  118. 1,
  119. (long FAR *)&dwSUBound
  120. );
  121. RETURN_ON_FAILURE(hr);
  122. (*ppList) = (LPOLESTR *) CoTaskMemAlloc(sizeof(LPOLESTR)*(dwSUBound-dwSLBound+1));
  123. for (i = dwSLBound; i <= dwSUBound; i++) {
  124. VariantInit(&v);
  125. hr = SafeArrayGetElement(V_ARRAY(&var),
  126. (long FAR *)&i,
  127. &v
  128. );
  129. if (FAILED(hr)) {
  130. continue;
  131. }
  132. if (i <= dwSUBound)
  133. {
  134. (*ppList)[*pCount] = (LPOLESTR) CoTaskMemAlloc
  135. (sizeof (WCHAR) * (wcslen(v.bstrVal) + 1));
  136. wcscpy ((*ppList)[*pCount], v.bstrVal);
  137. VariantClear(&v);
  138. (*pCount)++;
  139. }
  140. }
  141. VariantClear(&var);
  142. return(S_OK);
  143. }
  144. void EnumerateAndDelete(WCHAR *szContainerName)
  145. {
  146. HRESULT hr=S_OK;
  147. IADsContainer *pADsContainer=NULL, *pADssubContainer=NULL;
  148. IEnumVARIANT *pEnum=NULL;
  149. IADs *pADs=NULL;
  150. WCHAR *szFullName=NULL, *szName=NULL;
  151. WCHAR **ppswClassNameList=NULL;
  152. ULONG sz=0;
  153. hr = ADsGetObject(szContainerName, IID_IADsContainer, (void **)&pADsContainer);
  154. if (SUCCEEDED(hr))
  155. hr = ADsBuildEnumerator(pADsContainer, &pEnum);
  156. if (SUCCEEDED(hr))
  157. {
  158. for (;;)
  159. {
  160. hr = GetNextEnum(pEnum, &pADs);
  161. if (hr != S_OK)
  162. break;
  163. hr = pADs->get_ADsPath(&szFullName);
  164. if (FAILED(hr))
  165. continue;
  166. wprintf(L"Deleting %s \n", szFullName);
  167. hr = pADs->QueryInterface(IID_IADsContainer, (void **)&pADssubContainer);
  168. if (SUCCEEDED(hr))
  169. {
  170. pADssubContainer->Release();
  171. EnumerateAndDelete(szFullName);
  172. }
  173. hr = GetPropertyListAlloc(pADs, CLASSNAME, &sz, &ppswClassNameList);
  174. if (FAILED(hr))
  175. continue;
  176. SysFreeString(szFullName);
  177. hr = pADs->get_Name(&szName);
  178. if (FAILED(hr))
  179. continue;
  180. pADsContainer->Delete(ppswClassNameList[sz-1], szName);
  181. SysFreeString(szName);
  182. }
  183. }
  184. if (pADsContainer)
  185. pADsContainer->Release();
  186. return;
  187. }
  188. void __cdecl main(int argc, char *argv[])
  189. {
  190. WCHAR szContainer [_MAX_LENGTH+1];
  191. if (argc != 2)
  192. {
  193. printf("Usage: %s <Full ContainerName that has to be emptied>", argv[0]);
  194. printf("\tThe container itself will remain\n");
  195. return;
  196. }
  197. CoInitialize(NULL);
  198. MultiByteToWideChar(CP_ACP, 0, argv[1], strlen(argv[1]) + 1,
  199. szContainer, _MAX_LENGTH);
  200. EnumerateAndDelete(szContainer);
  201. CoUninitialize();
  202. }