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.

219 lines
6.4 KiB

  1. #include "stdafx.h"
  2. #include "browsedlg.h"
  3. #include "resource.h"
  4. #include "wuiids.h"
  5. CBrowseDlg* CBrowseDlg::m_pThis = NULL;
  6. CBrowseDlg::CBrowseDlg(HWND hWndOwner, HINSTANCE hInst) : m_hWnd(hWndOwner), m_hInst(hInst)
  7. {
  8. m_pThis = this;
  9. _pBrowseSrvCtl = NULL;
  10. _tcscpy( m_szServer, _T(""));
  11. }
  12. CBrowseDlg::~CBrowseDlg()
  13. {
  14. }
  15. INT_PTR
  16. CBrowseDlg::DoModal()
  17. {
  18. INT_PTR retVal;
  19. //
  20. // Init owner draw servers list box
  21. //
  22. _pBrowseSrvCtl = new CBrowseServersCtl(m_hInst);
  23. ASSERT(_pBrowseSrvCtl);
  24. if(!_pBrowseSrvCtl)
  25. {
  26. return 0;
  27. }
  28. _pBrowseSrvCtl->AddRef();
  29. retVal = DialogBox( m_hInst,MAKEINTRESOURCE(IDD_DIALOGBROWSESERVERS), m_hWnd, StaticDlgProc);
  30. //Object self deletes when refcount reaches 0
  31. //done so object is still around if list box population thread is still running
  32. _pBrowseSrvCtl->Release();
  33. return retVal;
  34. }
  35. INT_PTR CALLBACK CBrowseDlg::StaticDlgProc(HWND hDlg,UINT uMsg, WPARAM wParam, LPARAM lParam)
  36. {
  37. //
  38. // need access to class variables so redirect to non-static version of callback
  39. //
  40. return m_pThis->DlgProc(hDlg,uMsg,wParam,lParam);
  41. }
  42. INT_PTR
  43. CBrowseDlg::DlgProc(HWND hwndDlg,UINT uMsg, WPARAM wParam, LPARAM lParam)
  44. {
  45. BOOL rc = FALSE;
  46. static ServerListItem *plbi = NULL;
  47. static HANDLE hThread = NULL;
  48. static DCUINT DomainCount = 0;
  49. switch (uMsg)
  50. {
  51. case WM_INITDIALOG:
  52. {
  53. //Set the parent dialog handle of the browse for servers listbox
  54. _pBrowseSrvCtl->SetDialogHandle( hwndDlg);
  55. _pBrowseSrvCtl->Init( hwndDlg );
  56. if(hwndDlg)
  57. {
  58. DWORD dwResult = 0, dwThreadId;
  59. LPVOID lpMsgBuf = NULL;
  60. _bLBPopulated = FALSE;
  61. //create an event to signal the worker thread
  62. //auto reset and initial state is nonsignalled
  63. _hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  64. if(!_hEvent)
  65. {
  66. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  67. FORMAT_MESSAGE_FROM_SYSTEM,
  68. NULL,
  69. GetLastError(),
  70. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  71. (LPTSTR) & lpMsgBuf, 0, NULL);
  72. // TRC_ERR((TB, "CreateEvent returned %s", lpMsgBuf));
  73. }
  74. else
  75. {
  76. //Set the event handle for notification
  77. //the _BrowseSrvListBox will CloseHandle the event when it is done
  78. _pBrowseSrvCtl->SetEventHandle(_hEvent);
  79. /* Create a worker thread to do the browsing for servers */
  80. hThread = CreateThread(NULL, 0, &CBrowseServersCtl::UIStaticPopListBoxThread,
  81. _pBrowseSrvCtl, 0, &dwThreadId);
  82. }
  83. if(lpMsgBuf)
  84. {
  85. LocalFree(lpMsgBuf);
  86. }
  87. if(NULL == hThread)
  88. {
  89. /*Since the CreateThread failed, populate the list box directly*/
  90. _pBrowseSrvCtl->LoadLibraries();
  91. plbi = _pBrowseSrvCtl->PopulateListBox(hwndDlg, &DomainCount);
  92. }
  93. else
  94. {
  95. //
  96. // Add a reference to the list box object for the new thread
  97. // so the object doesn't get deleted before the thread is done
  98. // the Release() is in the function called on this new thread
  99. //
  100. _pBrowseSrvCtl->AddRef();
  101. CloseHandle(hThread);
  102. }
  103. }
  104. rc = TRUE;
  105. }
  106. break;
  107. //message captured here to check if the populating of listbox is completed by the
  108. //worker thread
  109. case UI_LB_POPULATE_END:
  110. {
  111. _bLBPopulated = TRUE;
  112. }
  113. break;
  114. case WM_CLOSE:
  115. {
  116. EndDialog(hwndDlg, IDCANCEL);
  117. }
  118. break;
  119. case WM_NOTIFY:
  120. {
  121. if(UI_IDC_SERVERS_TREE == wParam &&
  122. _bLBPopulated)
  123. {
  124. LPNMHDR pnmh = (LPNMHDR) lParam;
  125. if(pnmh)
  126. {
  127. if(pnmh->code == NM_DBLCLK)
  128. {
  129. //
  130. // If the current selection is a server
  131. // then we're done
  132. //
  133. if(_pBrowseSrvCtl->GetServer( m_szServer,
  134. sizeof(m_szServer)/sizeof(TCHAR)))
  135. {
  136. EndDialog( hwndDlg, IDOK );
  137. rc = TRUE;
  138. }
  139. else
  140. {
  141. _tcscpy( m_szServer, _T(""));
  142. }
  143. }
  144. }
  145. return _pBrowseSrvCtl->OnNotify( hwndDlg, wParam, lParam );
  146. }
  147. }
  148. break;
  149. case WM_COMMAND:
  150. {
  151. switch(DC_GET_WM_COMMAND_ID(wParam))
  152. {
  153. case IDCANCEL:
  154. {
  155. EndDialog(hwndDlg, IDCANCEL);
  156. rc = TRUE;
  157. }
  158. break;
  159. case IDOK:
  160. {
  161. if(_pBrowseSrvCtl->GetServer( m_szServer,
  162. sizeof(m_szServer)/sizeof(TCHAR) ))
  163. {
  164. EndDialog(hwndDlg, IDOK);
  165. }
  166. else
  167. {
  168. EndDialog(hwndDlg, IDCANCEL);
  169. }
  170. rc = TRUE;
  171. }
  172. break;
  173. }
  174. }
  175. break;
  176. case WM_DESTROY:
  177. {
  178. /* Since we are in WM_DESTROY signal to the worker thread to discontinue. */
  179. if(_hEvent)
  180. {
  181. SetEvent(_hEvent);
  182. }
  183. rc = FALSE;
  184. }
  185. break;
  186. }
  187. return(rc);
  188. }