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.

217 lines
5.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: renameui.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #include <SnapBase.h>
  12. #include "resource.h"
  13. #include "renameui.h"
  14. #include "editor.h"
  15. #include "connection.h"
  16. #ifdef DEBUG_ALLOCATOR
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. #endif
  23. BEGIN_MESSAGE_MAP(CADSIEditRenameDialog, CDialog)
  24. //{{AFX_MSG_MAP(CADsObjectDialog)
  25. // ON_CBN_EDITCHANGE(IDC_NEW_NAME_BOX, OnEditChangeName)
  26. //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28. CADSIEditRenameDialog::CADSIEditRenameDialog(CTreeNode* pCurrentNode,
  29. CADsObject* pADsObject,
  30. CString sPath,
  31. LPWSTR lpszNewName)
  32. : CDialog(IDD_RENAME_PAGE)
  33. {
  34. m_pCurrentNode = pCurrentNode;
  35. m_pADsObject = pADsObject;
  36. m_sCurrentPath = sPath;
  37. m_sNewName = lpszNewName;
  38. }
  39. CADSIEditRenameDialog::~CADSIEditRenameDialog()
  40. {
  41. }
  42. BOOL CADSIEditRenameDialog::OnInitDialog()
  43. {
  44. CDialog::OnInitDialog();
  45. CEdit* pNameBox = (CEdit*)GetDlgItem(IDC_NEW_NAME_BOX);
  46. pNameBox->SetWindowText(m_sNewName);
  47. return TRUE;
  48. }
  49. void CADSIEditRenameDialog::OnOK()
  50. {
  51. CEdit* pNameBox = (CEdit*)GetDlgItem(IDC_NEW_NAME_BOX);
  52. CString sOldPath, sNewPath;
  53. sOldPath = m_sCurrentPath;
  54. CString sNewName, sFullName;
  55. pNameBox->GetWindowText(sNewName);
  56. sFullName = m_sPrefix + sNewName;
  57. CComPtr<IADsContainer> pDestination;
  58. CADSIEditContainerNode* pContNode = dynamic_cast<CADSIEditContainerNode*>(m_pCurrentNode->GetContainer());
  59. ASSERT(pContNode != NULL);
  60. // Get the username and password from the connection node
  61. //
  62. CADSIEditConnectionNode* pConnectionNode = pContNode->GetADsObject()->GetConnectionNode();
  63. CConnectionData* pConnectData = pConnectionNode->GetConnectionData();
  64. HRESULT hr, hCredResult;
  65. CComPtr<IADs> pParentIADs;
  66. hr = OpenObjectWithCredentials(
  67. pConnectData,
  68. pConnectData->GetCredentialObject()->UseCredentials(),
  69. m_sCurrentPath,
  70. IID_IADs,
  71. (LPVOID*) &pParentIADs,
  72. GetSafeHwnd(),
  73. hCredResult
  74. );
  75. if (FAILED(hr))
  76. {
  77. if (SUCCEEDED(hCredResult))
  78. {
  79. ADSIEditErrorMessage(hr);
  80. }
  81. return;
  82. }
  83. CComBSTR bstrParentPath;
  84. hr = pParentIADs->get_Parent(&bstrParentPath);
  85. if (FAILED(hr))
  86. {
  87. ADSIEditErrorMessage(hr);
  88. return;
  89. }
  90. CString sContPath(bstrParentPath);
  91. hr = OpenObjectWithCredentials(
  92. pConnectData,
  93. pConnectData->GetCredentialObject()->UseCredentials(),
  94. sContPath,
  95. IID_IADsContainer,
  96. (LPVOID*) &pDestination,
  97. GetSafeHwnd(),
  98. hCredResult
  99. );
  100. if (FAILED(hr))
  101. {
  102. if (SUCCEEDED(hCredResult))
  103. {
  104. ADSIEditErrorMessage(hr);
  105. }
  106. return;
  107. }
  108. CString sEscapedName;
  109. hr = EscapePath(sEscapedName, sFullName);
  110. if (FAILED(hr))
  111. {
  112. ADSIEditErrorMessage(hr);
  113. return;
  114. }
  115. IDispatch* pObject;
  116. hr = pDestination->MoveHere(CComBSTR(sOldPath),
  117. CComBSTR(sEscapedName),
  118. &pObject);
  119. if (FAILED(hr))
  120. {
  121. ADSIEditErrorMessage(hr);
  122. return;
  123. }
  124. CComPtr<IADs> pIADs;
  125. hr = pObject->QueryInterface(IID_IADs, (LPVOID*)&pIADs);
  126. if (FAILED(hr))
  127. {
  128. ADSIEditErrorMessage(hr);
  129. return;
  130. }
  131. CComBSTR bstrPath;
  132. hr = pIADs->get_ADsPath(&bstrPath);
  133. if (FAILED(hr))
  134. {
  135. ADSIEditErrorMessage(hr);
  136. return;
  137. }
  138. CString szDN, szPath;
  139. szPath = bstrPath;
  140. CrackPath(szPath, szDN);
  141. m_pADsObject->SetPath(bstrPath);
  142. m_pADsObject->SetName(sFullName + m_sPostfix);
  143. m_pADsObject->SetDN(szDN);
  144. m_pCurrentNode->SetDisplayName(sFullName);
  145. CDialog::OnOK();
  146. return;
  147. }
  148. HRESULT CADSIEditRenameDialog::EscapePath(CString& sEscapedName, const CString& sName)
  149. {
  150. CComPtr<IADsPathname> pIADsPathname;
  151. HRESULT hr = ::CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  152. IID_IADsPathname, (PVOID *)&(pIADsPathname));
  153. ASSERT((S_OK == hr) && ((pIADsPathname) != NULL));
  154. CComBSTR bstrEscaped;
  155. hr = pIADsPathname->GetEscapedElement(0, //reserved
  156. (BSTR)(LPCWSTR)sName,
  157. &bstrEscaped);
  158. sEscapedName = bstrEscaped;
  159. return hr;
  160. }
  161. void CADSIEditRenameDialog::CrackPath(const CString& szPath, CString& sDN)
  162. {
  163. CComPtr<IADsPathname> pIADsPathname;
  164. HRESULT hr = ::CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  165. IID_IADsPathname, (PVOID *)&(pIADsPathname));
  166. ASSERT((S_OK == hr) && ((pIADsPathname) != NULL));
  167. hr = pIADsPathname->Set(CComBSTR(szPath), ADS_SETTYPE_FULL);
  168. if (FAILED(hr))
  169. {
  170. TRACE(_T("Set failed. %s"), hr);
  171. }
  172. // Get the leaf DN
  173. CComBSTR bstrDN;
  174. hr = pIADsPathname->Retrieve(ADS_FORMAT_X500_DN, &bstrDN);
  175. if (FAILED(hr))
  176. {
  177. TRACE(_T("Failed to get element. %s"), hr);
  178. sDN = L"";
  179. }
  180. else
  181. {
  182. sDN = bstrDN;
  183. }
  184. }