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.

353 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990, 1991 **/
  4. /**********************************************************************/
  5. /*
  6. mprenum.cxx
  7. Contains EnumerateShow.
  8. FILE HISTORY:
  9. Yi-HsinS 04-Mar-1993 Separated from mprbrows.cxx
  10. */
  11. #define INCL_NETERRORS
  12. #define INCL_DOSERRORS
  13. #define INCL_WINDOWS
  14. #define INCL_NETLIB
  15. #define INCL_NETWKSTA
  16. #include <lmui.hxx>
  17. #define INCL_BLT_DIALOG
  18. #define INCL_BLT_CONTROL
  19. #define INCL_BLT_MSGPOPUP
  20. #include <blt.hxx>
  21. #include <string.hxx>
  22. #include <uitrace.hxx>
  23. #include <dbgstr.hxx>
  24. #include <uibuffer.hxx>
  25. #include <mprbrows.hxx>
  26. #define MPRDBG(x) { ; }
  27. #define MPRDBGEOL(x) { ; }
  28. // prototype
  29. APIERR EnumerateShowHelp(
  30. HANDLE hEnum,
  31. MPR_LBI **ppmprlbiParent,
  32. MPR_LBI_CACHE *plbicache,
  33. LPNETRESOURCE lpNetResource,
  34. MPR_LBI *pmprlbi,
  35. MPR_HIER_LISTBOX *pmprlb,
  36. BOOL fDeleteChildren,
  37. BOOL *pfSearchDialogUsed );
  38. /*******************************************************************
  39. NAME: ::EnumerateShow
  40. SYNOPSIS: Enumerate the requested data
  41. (providers, containers or connectable items).
  42. (1) If the listbox passed in is not NULL,
  43. then the data is added in the listbox
  44. (2) If the listbox is NULL,
  45. then return the data in the MPR_LBI_CACHE
  46. ENTRY: uiScope - Scope for enumeration (see WNetOpenEnum)
  47. uiType - Type of enumeration (see WNetOpenEnum)
  48. uiUsage - Usage of enumeration (see WNetOpenEnum)
  49. lpNetResource - Net resource (see WNetOpenEnum)
  50. pmprlbi - Parent LBI to add enumeration to
  51. pmprlb - Pointer to the MPR_HIER_LISTBOX
  52. fDeleteChildren - TRUE if we need to delete the children
  53. pfSearchDialogUsed
  54. ppmprlbicache
  55. EXIT:
  56. RETURNS:
  57. NOTES:
  58. HISTORY:
  59. Johnl 22-Jan-1992 Broke out from dialog, commented, fixed
  60. YiHsinS 04-Mar-1993 Support multithread
  61. ********************************************************************/
  62. APIERR EnumerateShow( HWND hwndOwner,
  63. UINT uiScope,
  64. UINT uiType,
  65. UINT uiUsage,
  66. LPNETRESOURCE lpNetResource,
  67. MPR_LBI *pmprlbi,
  68. MPR_HIER_LISTBOX *pmprlb,
  69. BOOL fDeleteChildren,
  70. BOOL *pfSearchDialogUsed,
  71. MPR_LBI_CACHE **ppmprlbicache )
  72. {
  73. UIASSERT( WN_SUCCESS == NERR_Success );
  74. if ( ppmprlbicache != NULL )
  75. *ppmprlbicache = NULL;
  76. APIERR err = NERR_Success;
  77. MPR_LBI *pmprlbiParent = pmprlbi;
  78. MPR_LBI_CACHE *plbicache = new MPR_LBI_CACHE();
  79. if ( ( plbicache == NULL )
  80. || (( err = plbicache->QueryError()) != NERR_Success )
  81. )
  82. {
  83. if ( err != NERR_Success )
  84. delete plbicache;
  85. return (err? err : ERROR_NOT_ENOUGH_MEMORY);
  86. }
  87. HANDLE hEnum;
  88. err = WNetOpenEnum(uiScope, uiType, uiUsage, lpNetResource, &hEnum);
  89. if ((err != WN_SUCCESS) && (hwndOwner != NULL))
  90. {
  91. //
  92. // If hwndOwner is NULL, we shouldn't put any UI.
  93. //
  94. //
  95. // If it failed because you are not authenticated yet,
  96. // we need to let the user loggin to this network resource.
  97. //
  98. if (err == WN_NOT_AUTHENTICATED
  99. || err == ERROR_LOGON_FAILURE
  100. || err == WN_BAD_PASSWORD
  101. || err == WN_ACCESS_DENIED
  102. )
  103. {
  104. // Retry with password dialog box.
  105. err = WNetAddConnection3(hwndOwner, lpNetResource, NULL, NULL,
  106. CONNECT_TEMPORARY | CONNECT_INTERACTIVE);
  107. if (err == WN_SUCCESS)
  108. {
  109. // Retry WNetOpenEnum.
  110. err = WNetOpenEnum(uiScope, uiType, uiUsage, lpNetResource, &hEnum);
  111. }
  112. }
  113. }
  114. if (err == WN_SUCCESS)
  115. {
  116. err = EnumerateShowHelp(
  117. hEnum,
  118. &pmprlbiParent,
  119. plbicache,
  120. lpNetResource,
  121. pmprlbi,
  122. pmprlb,
  123. fDeleteChildren,
  124. pfSearchDialogUsed );
  125. WNetCloseEnum( hEnum );
  126. }
  127. else
  128. {
  129. // probable errors: WN_NO_NETWORK, WN_EXTENDED_ERROR, WN_BAD_VALUE,
  130. // WN_NOT_CONTAINER
  131. MPRDBGEOL( SZ("EnumerateShow OpenEnum > ") << (ULONG) err );
  132. }
  133. //
  134. // Add all of the items in a single block to avoid the n^2 sort that would
  135. // be done if they were added individually
  136. //
  137. if ( plbicache->QueryCount() > 0 )
  138. {
  139. plbicache->Sort();
  140. if ( pmprlb != NULL )
  141. {
  142. pmprlb->SetRedraw( FALSE );
  143. pmprlb->AddSortedItems( (HIER_LBI**) plbicache->QueryPtr(),
  144. plbicache->QueryCount(),
  145. pmprlbiParent );
  146. pmprlb->SetRedraw( TRUE );
  147. pmprlb->Invalidate();
  148. delete plbicache;
  149. plbicache = NULL;
  150. }
  151. else
  152. {
  153. UIASSERT( ppmprlbicache != NULL );
  154. *ppmprlbicache = plbicache;
  155. }
  156. }
  157. else
  158. {
  159. // nothing there and it isn't returned; delete the cache.
  160. delete plbicache;
  161. plbicache = NULL;
  162. }
  163. return err;
  164. } // ::EnumerateShow
  165. /*******************************************************************
  166. NAME: ::EnumerateShowHelp
  167. SYNOPSIS: A helper function for EnumerateShow. It handles the
  168. actual enumeration.
  169. ENTRY:
  170. hEnum - enumeration handle from WNetOpenEnum
  171. ppmprlbiParent - parent LBI
  172. plbicache - the cache to put elements in
  173. lpNetResource - Net resource of container (see WNetOpenEnum)
  174. pmprlbi - Parent LBI to add enumeration to
  175. pmprlb - Pointer to the MPR_HIER_LISTBOX
  176. fDeleteChildren - TRUE if we need to delete the children
  177. pfSearchDialogUsed
  178. EXIT:
  179. RETURNS:
  180. NOTES:
  181. HISTORY:
  182. BruceFo 13-Sep-1995 Split from EnumerateShow for readability
  183. ********************************************************************/
  184. APIERR EnumerateShowHelp(
  185. HANDLE hEnum,
  186. MPR_LBI **ppmprlbiParent,
  187. MPR_LBI_CACHE *plbicache,
  188. LPNETRESOURCE lpNetResource,
  189. MPR_LBI *pmprlbi,
  190. MPR_HIER_LISTBOX *pmprlb,
  191. BOOL fDeleteChildren,
  192. BOOL *pfSearchDialogUsed )
  193. {
  194. BUFFER buf( 1024 );
  195. DWORD err;
  196. DWORD dwEnumErr = buf.QueryError() ;
  197. while ( dwEnumErr == NERR_Success )
  198. {
  199. DWORD dwCount = 0xffffffff; // Return as many as possible
  200. DWORD dwBuffSize = buf.QuerySize() ;
  201. dwEnumErr = WNetEnumResource( hEnum,
  202. &dwCount,
  203. buf.QueryPtr(),
  204. &dwBuffSize );
  205. MPRDBGEOL( SZ("EnumerateShow EnumResource >") << (ULONG) dwEnumErr ) ;
  206. switch ( dwEnumErr )
  207. {
  208. case WN_SUCCESS:
  209. case WN_NO_MORE_ENTRIES: // Success code, map below
  210. if ( fDeleteChildren )
  211. {
  212. UIASSERT( pmprlb != NULL );
  213. pmprlb->SetRedraw( FALSE );
  214. fDeleteChildren = FALSE;
  215. pmprlb->DeleteChildren( pmprlbi );
  216. MPR_LBI * pmprlbiTemp = new MPR_LBI( lpNetResource );
  217. if ( ( pmprlbiTemp == NULL )
  218. || ( err = pmprlbiTemp->QueryError() )
  219. )
  220. {
  221. delete pmprlbiTemp;
  222. pmprlbiTemp = NULL;
  223. dwEnumErr = err ? err : ERROR_NOT_ENOUGH_MEMORY;
  224. break;
  225. }
  226. if ( pmprlb->AddItem( pmprlbiTemp, pmprlbi ) < 0 )
  227. {
  228. // AddItem has already deleted the item we're trying
  229. // to add, if it returns -1: don't delete it again.
  230. pmprlbiTemp = NULL;
  231. dwEnumErr = ERROR_NOT_ENOUGH_MEMORY;
  232. break;
  233. }
  234. *ppmprlbiParent = pmprlbiTemp;
  235. pmprlb->SetRedraw( TRUE );
  236. }
  237. if ( dwEnumErr == WN_SUCCESS )
  238. {
  239. //
  240. // Add the Entries to the listbox with parent pmprlbi
  241. //
  242. MPRDBGEOL( SZ("EnumerateShow ")
  243. << (ULONG) dwCount << SZ(" Entries returned "));
  244. NETRESOURCE *pnetres = (NETRESOURCE * )buf.QueryPtr();
  245. for (INT i = 0; dwCount ; dwCount--, i++ )
  246. {
  247. if ( err = plbicache->AppendItem(
  248. new MPR_LBI( &(pnetres[i])))
  249. )
  250. {
  251. dwEnumErr = err ;
  252. break ;
  253. }
  254. // if we are at top (lpNetResource==NULL) and
  255. // we've been given a valid pfSearchDialogUsed
  256. // pointer, then we go figure out if we need
  257. // to display the "Search..." button.
  258. if (lpNetResource == NULL &&
  259. pfSearchDialogUsed != NULL)
  260. {
  261. if (::WNetGetSearchDialog(
  262. pnetres[i].lpProvider) != NULL)
  263. {
  264. *pfSearchDialogUsed = TRUE ;
  265. }
  266. }
  267. }
  268. }
  269. break ;
  270. /* The buffer wasn't big enough for even one entry,
  271. * resize it and try again.
  272. */
  273. case WN_MORE_DATA:
  274. if ( dwEnumErr = buf.Resize( buf.QuerySize()*2 ))
  275. break;
  276. /*
  277. * Continue looping
  278. */
  279. dwEnumErr = WN_SUCCESS;
  280. break;
  281. case WN_EXTENDED_ERROR:
  282. case WN_NO_NETWORK:
  283. break;
  284. default:
  285. MPRDBGEOL(SZ("EnumerateConnections - Unexpected return code from WNetEnumResource"));
  286. break;
  287. } // switch
  288. } // while
  289. UIASSERT( sizeof(APIERR) == sizeof( DWORD ));
  290. return (dwEnumErr==WN_NO_MORE_ENTRIES ) ? NERR_Success : dwEnumErr;
  291. } // ::EnumerateShowHelp