Source code of Windows XP (NT5)
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.

365 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. cenmfpsh.cxx
  5. Abstract:
  6. Contains methods for implementing the Enumeration of session on a
  7. server. Has methods for the CFPNWFileSharesEnumVar object.
  8. Author:
  9. Ram Viswanathan (ramv) 11-28-95
  10. Revision History:
  11. --*/
  12. #include "winnt.hxx"
  13. #pragma hdrstop
  14. #if DBG
  15. DECLARE_INFOLEVEL(FPNWEnumFileShare);
  16. DECLARE_DEBUG(FPNWEnumFileShare);
  17. #define FPNWEnumFileShareDebugOut(x) FPNWEnumFileShareInlineDebugOut x
  18. #endif
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: CFPNWFileSharesEnumVar::CFPNWFileSharesEnumVar
  22. //
  23. // Synopsis:
  24. //
  25. //
  26. // Arguments:
  27. //
  28. //
  29. // Returns:
  30. //
  31. // Modifies:
  32. //
  33. // History: 11-22-95 RamV Created.
  34. //
  35. //----------------------------------------------------------------------------
  36. CFPNWFileSharesEnumVar::CFPNWFileSharesEnumVar()
  37. {
  38. _pszADsPath = NULL;
  39. _pszServerName = NULL;
  40. _pbFileShares = NULL;
  41. _cElements = 0;
  42. _lLBound = 0;
  43. _lCurrentPosition = _lLBound;
  44. _dwTotalEntries = 0;
  45. _dwResumeHandle = 0;
  46. VariantInit(&_vFilter);
  47. }
  48. //+---------------------------------------------------------------------------
  49. //
  50. // Function: CFPNWFileSharesEnumVar::~CFPNWFileSharesEnumVar
  51. //
  52. // Synopsis:
  53. //
  54. //
  55. // Arguments:
  56. //
  57. // Returns:
  58. //
  59. // Modifies:
  60. //
  61. // History: 11-22-95 RamV Created.
  62. //
  63. //----------------------------------------------------------------------------
  64. CFPNWFileSharesEnumVar::~CFPNWFileSharesEnumVar()
  65. {
  66. if(_pszADsPath){
  67. FreeADsStr(_pszADsPath);
  68. }
  69. if(_pszServerName){
  70. FreeADsStr(_pszServerName);
  71. }
  72. if(_pbFileShares){
  73. NetApiBufferFree(_pbFileShares);
  74. }
  75. VariantClear(&_vFilter);
  76. }
  77. HRESULT CFPNWFileSharesEnumVar::Create(LPTSTR pszServerName,
  78. LPTSTR pszADsPath,
  79. CFPNWFileSharesEnumVar **ppCFileSharesEnumVar,
  80. VARIANT vFilter,
  81. CWinNTCredentials& Credentials
  82. )
  83. {
  84. HRESULT hr = S_OK;
  85. BOOL fStatus = FALSE;
  86. CFPNWFileSharesEnumVar FAR* pCFileSharesEnumVar = NULL;
  87. *ppCFileSharesEnumVar = NULL;
  88. pCFileSharesEnumVar = new CFPNWFileSharesEnumVar();
  89. if (pCFileSharesEnumVar == NULL){
  90. hr = E_OUTOFMEMORY;
  91. goto error;
  92. }
  93. pCFileSharesEnumVar->_pszServerName =
  94. AllocADsStr(pszServerName);
  95. if(!(pCFileSharesEnumVar->_pszServerName)){
  96. hr = E_OUTOFMEMORY;
  97. goto error;
  98. }
  99. pCFileSharesEnumVar->_pszADsPath =
  100. AllocADsStr(pszADsPath);
  101. if(!(pCFileSharesEnumVar->_pszADsPath)){
  102. hr = E_OUTOFMEMORY;
  103. goto error;
  104. }
  105. hr = VariantCopy(&(pCFileSharesEnumVar->_vFilter), &vFilter);
  106. BAIL_ON_FAILURE(hr);
  107. pCFileSharesEnumVar->_Credentials = Credentials;
  108. hr = pCFileSharesEnumVar->_Credentials.RefServer(pszServerName);
  109. BAIL_ON_FAILURE(hr);
  110. *ppCFileSharesEnumVar = pCFileSharesEnumVar;
  111. RRETURN(hr);
  112. error:
  113. delete pCFileSharesEnumVar;
  114. RRETURN_EXP_IF_ERR(hr);
  115. }
  116. //+---------------------------------------------------------------------------
  117. //
  118. // Function: CFPNWFileSharesEnumVar::Next
  119. //
  120. // Synopsis: Returns cElements number of requested Share objects in the
  121. // array supplied in pvar.
  122. //
  123. // Arguments: [cElements] -- The number of elements requested by client
  124. // [pvar] -- ptr to array of VARIANTs to for return objects
  125. // [ulNumFetched] -- if non-NULL, then number of elements
  126. // -- actually returned is placed here
  127. //
  128. // Returns: HRESULT -- S_OK if number of elements requested are returned
  129. // -- S_FALSE if number of elements is < requested
  130. //
  131. // Modifies:
  132. //
  133. // History: 11-27-95 RamV Created.
  134. //
  135. //----------------------------------------------------------------------------
  136. STDMETHODIMP
  137. CFPNWFileSharesEnumVar::Next(ULONG ulNumElementsRequested,
  138. VARIANT FAR* pvar,
  139. ULONG FAR* pulNumFetched)
  140. {
  141. HRESULT hresult;
  142. ULONG l;
  143. ULONG lNewCurrent;
  144. ULONG lNumFetched;
  145. PNWVOLUMEINFO pVolumeInfo = NULL;
  146. VARIANT v;
  147. IDispatch * pDispatch = NULL;
  148. if (pulNumFetched != NULL){
  149. *pulNumFetched = 0;
  150. }
  151. //
  152. // Initialize the elements to be returned
  153. //
  154. for (l=0; l<ulNumElementsRequested; l++){
  155. VariantInit(&pvar[l]);
  156. }
  157. if(!_pbFileShares ||(_lCurrentPosition == _lLBound +(LONG)_cElements)){
  158. if (_pbFileShares){
  159. ADsNwApiBufferFree(_pbFileShares);
  160. _pbFileShares = NULL;
  161. }
  162. if(!(ValidateFilterValue(_vFilter))){
  163. RRETURN(S_FALSE);
  164. }
  165. hresult = FPNWEnumFileShares(_pszServerName,
  166. &_cElements,
  167. &_dwResumeHandle,
  168. &_pbFileShares);
  169. if(hresult == S_FALSE){
  170. goto cleanup;
  171. }
  172. _lLBound = 0;
  173. _lCurrentPosition = _lLBound;
  174. }
  175. //
  176. // Get each element and place it into the return array
  177. // Don't request more than we have
  178. //
  179. lNumFetched = 0;
  180. lNewCurrent = _lCurrentPosition;
  181. while((lNumFetched < ulNumElementsRequested) ||
  182. (lNewCurrent< _lLBound +_cElements ))
  183. {
  184. pVolumeInfo = (PNWVOLUMEINFO)(_pbFileShares +
  185. lNewCurrent*sizeof(NWVOLUMEINFO));
  186. if(pVolumeInfo->dwType == FPNWVOL_TYPE_DISKTREE){
  187. //
  188. // file share object
  189. //
  190. hresult = CFPNWFileShare::Create((LPTSTR)_pszADsPath,
  191. _pszServerName,
  192. FILESHARE_CLASS_NAME,
  193. pVolumeInfo->lpVolumeName,
  194. ADS_OBJECT_BOUND,
  195. IID_IDispatch,
  196. _Credentials,
  197. (void **)&pDispatch);
  198. BAIL_IF_ERROR(hresult);
  199. VariantInit(&v);
  200. V_VT(&v) = VT_DISPATCH;
  201. V_DISPATCH(&v) = pDispatch;
  202. pvar[lNumFetched] = v;
  203. lNumFetched++;
  204. }
  205. lNewCurrent++;
  206. if(lNumFetched == ulNumElementsRequested){
  207. //
  208. // we got all elements
  209. //
  210. break;
  211. }
  212. if(lNewCurrent==(_lLBound+_cElements)){
  213. //
  214. // first free our current buffer
  215. //
  216. if(_pbFileShares){
  217. ADsNwApiBufferFree(_pbFileShares);
  218. _pbFileShares = NULL;
  219. }
  220. hresult = FPNWEnumFileShares(_pszServerName,
  221. &_cElements,
  222. &_dwResumeHandle,
  223. &_pbFileShares);
  224. if(hresult == S_FALSE){
  225. break;
  226. }
  227. _lLBound = 0;
  228. _lCurrentPosition = _lLBound;
  229. lNewCurrent = _lCurrentPosition;
  230. }
  231. }
  232. //
  233. // Tell the caller how many we got (which may be less than the number
  234. // requested), and save the current position
  235. //
  236. if (pulNumFetched != NULL)
  237. *pulNumFetched = lNumFetched;
  238. _lCurrentPosition = lNewCurrent;
  239. //
  240. // If we're returning less than they asked for return S_FALSE, but
  241. // they still have the data (S_FALSE is a success code)
  242. //
  243. return (lNumFetched < ulNumElementsRequested) ?
  244. S_FALSE
  245. : S_OK;
  246. cleanup:
  247. if(_pbFileShares){
  248. ADsNwApiBufferFree(_pbFileShares);
  249. }
  250. if(FAILED(hresult)){
  251. #if DBG
  252. FPNWEnumFileShareDebugOut((DEB_TRACE,
  253. "hresult Failed with value: %ld \n", hresult ));
  254. #endif
  255. }
  256. RRETURN(S_FALSE);
  257. }
  258. HRESULT
  259. FPNWEnumFileShares(LPTSTR pszServerName,
  260. PDWORD pdwElements,
  261. PDWORD pdwResumeHandle,
  262. LPBYTE * ppMem
  263. )
  264. {
  265. NET_API_STATUS nasStatus;
  266. DWORD dwErrorCode;
  267. //
  268. // assumption: *ppMem = NULL when passed here
  269. //
  270. dwErrorCode = ADsNwVolumeEnum(pszServerName,
  271. 1,
  272. (PNWVOLUMEINFO *)ppMem,
  273. pdwElements,
  274. pdwResumeHandle);
  275. if(dwErrorCode != NERR_Success || (*ppMem == NULL )){
  276. //
  277. // NwVolumeEnum returns NERR_Success even when there
  278. // aren't any more items to enumerate
  279. //
  280. RRETURN(S_FALSE);
  281. }
  282. RRETURN(S_OK);
  283. }