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.

304 lines
7.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2001.
  5. //
  6. // File: SelServc.cpp
  7. //
  8. // Contents: Implementation of Service Selection setup page
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "SelServc.h"
  13. USE_HANDLE_MACROS("CERTMGR(SelServc.cpp)")
  14. #ifdef _DEBUG
  15. #ifndef ALPHA
  16. #define new DEBUG_NEW
  17. #endif
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CSelectServiceAccountPropPage property page
  23. //IMPLEMENT_DYNCREATE(CSelectServiceAccountPropPage, CAutoDeletePropPage)
  24. CSelectServiceAccountPropPage::CSelectServiceAccountPropPage(
  25. CString* pszManagedService,
  26. CString* pszManagedServiceDisplayName,
  27. const CString& szManagedMachine) :
  28. CAutoDeletePropPage(CSelectServiceAccountPropPage::IDD),
  29. m_pszManagedService (pszManagedService),
  30. m_pszManagedServiceDisplayName (pszManagedServiceDisplayName),
  31. m_szManagedMachine (szManagedMachine),
  32. m_savedManagedMachineName (_T("$uninitialized machine name$"))
  33. {
  34. ASSERT (pszManagedService);
  35. //{{AFX_DATA_INIT(CSelectServiceAccountPropPage)
  36. //}}AFX_DATA_INIT
  37. }
  38. CSelectServiceAccountPropPage::~CSelectServiceAccountPropPage()
  39. {
  40. }
  41. void CSelectServiceAccountPropPage::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CAutoDeletePropPage::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CSelectServiceAccountPropPage)
  45. DDX_Control(pDX, IDC_ACCT_NAME, m_acctNameList);
  46. DDX_Control(pDX, IDC_INSTRUCTIONS, m_instructionsText);
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CSelectServiceAccountPropPage, CAutoDeletePropPage)
  50. //{{AFX_MSG_MAP(CSelectServiceAccountPropPage)
  51. ON_CBN_SELCHANGE(IDC_ACCT_NAME, OnSelchangeAcctName)
  52. ON_WM_DESTROY()
  53. //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CSelectServiceAccountPropPage message handlers
  57. BOOL CSelectServiceAccountPropPage::OnSetActive()
  58. {
  59. BOOL bResult = CAutoDeletePropPage::OnSetActive();
  60. ASSERT (bResult);
  61. if ( IsLocalComputername (m_szManagedMachine) )
  62. {
  63. CString text;
  64. VERIFY (text.LoadString (IDS_SELECT_SERVICE_ON_LOCAL_MACHINE));
  65. m_instructionsText.SetWindowText (text);
  66. }
  67. else
  68. {
  69. CString text;
  70. size_t len = m_instructionsText.GetWindowTextLength () + 1;
  71. m_instructionsText.GetWindowText (text.GetBufferSetLength ((int) len), (int) len);
  72. text.FormatMessage (text, m_szManagedMachine);
  73. m_instructionsText.SetWindowText (text);
  74. }
  75. if ( m_savedManagedMachineName != m_szManagedMachine )
  76. {
  77. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  78. PSWIZB_BACK | PSWIZB_DISABLEDFINISH);
  79. m_savedManagedMachineName = m_szManagedMachine;
  80. FreeDataPointers ();
  81. EnumerateServices ();
  82. }
  83. else
  84. {
  85. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  86. PSWIZB_BACK | PSWIZB_FINISH);
  87. }
  88. return bResult;
  89. }
  90. BOOL CSelectServiceAccountPropPage::OnInitDialog()
  91. {
  92. CAutoDeletePropPage::OnInitDialog();
  93. return TRUE; // return TRUE unless you set the focus to a control
  94. // EXCEPTION: OCX Property Pages should return FALSE
  95. }
  96. HRESULT CSelectServiceAccountPropPage::EnumerateServices()
  97. {
  98. HRESULT hResult = S_OK;
  99. DWORD cbBytesNeeded = 0;
  100. DWORD entriesRead = 0;
  101. DWORD resumeHandle = 0;
  102. DWORD dwErr = 0;
  103. LPCWSTR lpMachineName = (LPCWSTR) (m_szManagedMachine);
  104. SC_HANDLE hScManager = ::OpenSCManager (lpMachineName,
  105. SERVICES_ACTIVE_DATABASE,
  106. SC_MANAGER_ENUMERATE_SERVICE);
  107. if ( hScManager )
  108. {
  109. LPENUM_SERVICE_STATUS enumBuffer = new ENUM_SERVICE_STATUS[32];
  110. DWORD cbBufSize = sizeof (ENUM_SERVICE_STATUS) * 32;
  111. BOOL bResult = FALSE;
  112. if ( enumBuffer )
  113. {
  114. int nIndex = 0;
  115. //
  116. // Enumerate the ServiceStatus
  117. //
  118. dwErr = NO_ERROR;
  119. do
  120. {
  121. bResult = ::EnumServicesStatus (
  122. hScManager,
  123. SERVICE_WIN32,
  124. SERVICE_STATE_ALL,
  125. enumBuffer,
  126. cbBufSize,
  127. &cbBytesNeeded,
  128. &entriesRead,
  129. &resumeHandle);
  130. if ( bResult || GetLastError() == ERROR_MORE_DATA )
  131. {
  132. for (UINT i = 0; i < entriesRead && SUCCEEDED (hResult); i++)
  133. {
  134. nIndex = m_acctNameList.AddString (
  135. enumBuffer[i].lpDisplayName);
  136. ASSERT (nIndex >= 0);
  137. switch ( nIndex )
  138. {
  139. case LB_ERR:
  140. hResult = E_FAIL;
  141. break;
  142. case LB_ERRSPACE:
  143. hResult = E_OUTOFMEMORY;
  144. break;
  145. default:
  146. nIndex = m_acctNameList.SetItemDataPtr (nIndex,
  147. new CString (enumBuffer[i].lpServiceName));
  148. ASSERT (nIndex != LB_ERR);
  149. if ( nIndex == LB_ERR )
  150. hResult = E_FAIL;
  151. break;
  152. }
  153. }
  154. }
  155. else
  156. {
  157. dwErr = GetLastError();
  158. DisplaySystemError (m_hWnd, dwErr);
  159. hResult = HRESULT_FROM_WIN32 (dwErr);
  160. break;
  161. }
  162. } while (SUCCEEDED (hResult) && resumeHandle > 0);
  163. int nCnt = m_acctNameList.GetCount ();
  164. ASSERT (nCnt != LB_ERR);
  165. if ( nCnt > 0 )
  166. {
  167. nIndex = m_acctNameList.SetCurSel (0);
  168. ASSERT (nIndex != LB_ERR);
  169. if ( nIndex != LB_ERR )
  170. {
  171. CopyCurrentString ();
  172. if ( m_pszManagedService && m_pszManagedService->IsEmpty () )
  173. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  174. PSWIZB_BACK | PSWIZB_DISABLEDFINISH);
  175. else
  176. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  177. PSWIZB_BACK | PSWIZB_FINISH);
  178. }
  179. }
  180. delete [] enumBuffer;
  181. }
  182. else
  183. {
  184. hResult = E_OUTOFMEMORY;
  185. }
  186. bResult = ::CloseServiceHandle (hScManager);
  187. ASSERT (bResult);
  188. if ( !bResult )
  189. {
  190. dwErr = GetLastError ();
  191. DisplaySystemError (m_hWnd, dwErr);
  192. hResult = HRESULT_FROM_WIN32 (dwErr);
  193. }
  194. }
  195. else
  196. {
  197. dwErr = GetLastError ();
  198. DisplaySystemError (m_hWnd, dwErr);
  199. hResult = HRESULT_FROM_WIN32 (dwErr);
  200. /*
  201. switch (dwErr)
  202. {
  203. case ERROR_ACCESS_DENIED: // The requested access was denied.
  204. break;
  205. case ERROR_DATABASE_DOES_NOT_EXIST: // The specified database does not exist.
  206. break;
  207. case ERROR_INVALID_PARAMETER:
  208. break;
  209. case RPC_S_SERVER_UNAVAILABLE:
  210. break;
  211. }
  212. */
  213. }
  214. return hResult;
  215. }
  216. void CSelectServiceAccountPropPage::OnSelchangeAcctName()
  217. {
  218. CopyCurrentString ();
  219. ASSERT (m_pszManagedService);
  220. if ( m_pszManagedService && m_pszManagedService->IsEmpty () )
  221. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  222. PSWIZB_BACK | PSWIZB_DISABLEDFINISH);
  223. else
  224. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0,
  225. PSWIZB_BACK | PSWIZB_FINISH);
  226. }
  227. void CSelectServiceAccountPropPage::FreeDataPointers()
  228. {
  229. CString* pStr = 0;
  230. int nCnt = m_acctNameList.GetCount ();
  231. for (int i = nCnt-1; i >= 0; i--)
  232. {
  233. pStr = (CString*) m_acctNameList.GetItemDataPtr (i);
  234. ASSERT (pStr != (CString*) LB_ERR);
  235. ASSERT (pStr);
  236. if ( pStr && pStr != (CString*) LB_ERR )
  237. delete pStr;
  238. m_acctNameList.SetItemDataPtr (i, 0);
  239. m_acctNameList.DeleteString (i);
  240. }
  241. }
  242. void CSelectServiceAccountPropPage::OnDestroy()
  243. {
  244. CAutoDeletePropPage::OnDestroy();
  245. FreeDataPointers ();
  246. }
  247. void CSelectServiceAccountPropPage::CopyCurrentString()
  248. {
  249. ASSERT (m_pszManagedService);
  250. if ( m_pszManagedService )
  251. {
  252. int nIndex = m_acctNameList.GetCurSel ();
  253. ASSERT (LB_ERR != nIndex);
  254. if ( LB_ERR != nIndex )
  255. {
  256. CString* pStr = (CString*) m_acctNameList.GetItemDataPtr (nIndex);
  257. ASSERT (pStr && pStr != (CString*) LB_ERR);
  258. if ( pStr && pStr != (CString*) LB_ERR )
  259. *m_pszManagedService = *pStr;
  260. m_acctNameList.GetText (nIndex,
  261. *m_pszManagedServiceDisplayName);
  262. }
  263. }
  264. }