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.

144 lines
4.1 KiB

  1. // ObjCopy.cpp: implementation of the CObjCopy class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "GetPropTest.h"
  6. #include "ObjCopy.h"
  7. #import "../McsAdsClassProp.tlb" no_namespace
  8. #import "C:\\bin\\mcsvarsetmin.tlb" no_namespace
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. CObjCopy::CObjCopy(CString a_strContainer) : m_strCont(a_strContainer)
  18. {
  19. }
  20. CObjCopy::~CObjCopy()
  21. {
  22. }
  23. HRESULT CObjCopy::CopyObject(CString a_strSource, CString a_strSrcDomain, CString a_strTarget, CString a_strTgtDomain)
  24. {
  25. WCHAR sAdsPath[255];
  26. WCHAR sNC[255];
  27. HRESULT hr;
  28. IADs * pAds;
  29. IADsContainer * pCont;
  30. IDispatch * pDisp;
  31. IVarSetPtr pVarset(__uuidof(VarSet));
  32. IObjPropBuilderPtr pObjProps(__uuidof(ObjPropBuilder));
  33. IUnknown * pUnk;
  34. _variant_t var;
  35. _bstr_t sClassName;
  36. // Find the naming convention for the Source domain.
  37. wcscpy(sAdsPath, L"LDAP://");
  38. wcscat(sAdsPath, a_strSrcDomain);
  39. wcscat(sAdsPath, L"/rootDSE");
  40. hr = ADsGetObject(sAdsPath, IID_IADs, (void**)&pAds);
  41. if ( FAILED(hr))
  42. return hr;
  43. hr = pAds->Get(L"defaultNamingContext",&var);
  44. if ( SUCCEEDED( hr) )
  45. wcscpy(sNC, var.bstrVal);
  46. pAds->Release();
  47. // Now build a path to your source object.
  48. wsprintf(sAdsPath, L"LDAP://%s/%s,%s", a_strSrcDomain, a_strSource, sNC);
  49. // Get the class type of the property
  50. hr = ADsGetObject(sAdsPath, IID_IADs, (void**)&pAds);
  51. if ( FAILED(hr) )
  52. return hr;
  53. // Get the name of the class for the source object so we can use that to create the new object.
  54. WCHAR * sClass;
  55. hr = pAds->get_Name(&sClass);
  56. hr = pAds->get_Class(&sClass);
  57. pAds->Release();
  58. if ( FAILED(hr) )
  59. return hr;
  60. // Now that we have the classname we can go ahead and create an object in the target domain.
  61. // First we need to get IAdsContainer * to the domain.
  62. wcscpy(sAdsPath, m_strCont);
  63. hr = ADsGetObject(sAdsPath, IID_IADsContainer, (void**)&pCont);
  64. if ( FAILED(hr) )
  65. return (hr);
  66. // Call the create method on the container.
  67. WCHAR sTarget[255];
  68. wcscpy(sTarget, a_strTarget);
  69. hr = pCont->Create(sClass, sTarget, &pDisp);
  70. pCont->Release();
  71. if ( FAILED(hr) )
  72. return hr;
  73. // Get the IADs interface to get the path to newly created object.
  74. hr = pDisp->QueryInterface(IID_IADs, (void**)&pAds);
  75. pDisp->Release();
  76. if ( FAILED(hr) )
  77. return hr;
  78. _variant_t varT;
  79. _bstr_t strName;
  80. int d = a_strTarget.Find(',');
  81. if (d == -1)
  82. varT = a_strTarget.Mid(3);
  83. else
  84. varT = a_strTarget.Mid(3,d - 3);
  85. hr = pAds->Put(L"sAMAccountName", varT);
  86. hr = pAds->SetInfo();
  87. WCHAR * sTgtPath;
  88. hr = pAds->get_ADsPath(&sTgtPath);
  89. if ( FAILED(hr) )
  90. return hr;
  91. // Get the IUnknown * to the varset to pass it around
  92. hr = pVarset->QueryInterface(IID_IUnknown, (void**)&pUnk);
  93. if ( FAILED(hr) )
  94. return hr;
  95. // Now lets get a mapping of the properties between the two domains
  96. _bstr_t sSrcDomain = a_strSrcDomain;
  97. _bstr_t sTgtDomain = a_strTgtDomain;
  98. _bstr_t sSource = a_strSource;
  99. hr = pObjProps->MapProperties(sClass, sSrcDomain, sClass, sTgtDomain, &pUnk);
  100. if ( FAILED(hr) )
  101. return hr;
  102. _variant_t varX;
  103. /* pVarset->Clear();
  104. pVarset->put("telephoneNumber", varX);
  105. pVarset->put("Description", varX);
  106. pVarset->put("userPassword", varX);
  107. pVarset->put("userPrincipalName", varX);
  108. pVarset->put("userParameters", varX);
  109. pVarset->put("wbemPath", varX);
  110. pVarset->put("telephoneNumber", varX);
  111. */
  112. // Copy the mapped properties from Source to Target object.
  113. hr = pObjProps->CopyProperties(sSource, sSrcDomain, sTgtPath, sTgtDomain, pUnk);
  114. if ( FAILED(hr) )
  115. return hr;
  116. pUnk->Release();
  117. hr = pAds->SetInfo();
  118. pAds->Release();
  119. return S_OK;
  120. }