Platform SDK: Active Directory, ADSI, and Directory Services |
The following code fragment contains a function that moves a group (or any object) to another location in the domain:
//////////////////////////////////////////////////////////////////////////////////////////////////// /* MoveObject() - Moves an object in the directory from the IDirectoryObject passed The Object at the NEW location is Queried for an updated IDirectoryObject interface, and the passed PTR is updated.. Parameters IDirectoryObject *pIDODestination - Place to move the object to IDirectoryObject **ppIDOToMove - Object to move (note returned is a different PTR (but it points to the moved object) */ HRESULT MoveObject(IDirectoryObject * pIDODestination,IDirectoryObject **ppIDOToMove) { IADsContainer * pIADsContainerDestination = NULL; IADs * pIADsToMove; HRESULT hr; BSTR bsPathToMove; // QI a IADs ptr from the ToMove object hr =(*ppIDOToMove)->QueryInterface(IID_IADs,(void**)&pIADsToMove); if (SUCCEEDED(hr)) { //The ADS path of the object to MOVE hr =pIADsToMove->get_ADsPath(&bsPathToMove); if (SUCCEEDED(hr)) { //printf("\n%s\n",QueryAllInterfaces( pIDODestination)); // Get an IADsContainer * from the Destination hr =pIDODestination->QueryInterface(IID_IADsContainer ,(void**)&pIADsContainerDestination ); if (SUCCEEDED(hr)) { IDispatch * pIDispatchNewObject = NULL; // Actually MOVE the object hr = pIADsContainerDestination->MoveHere(bsPathToMove,NULL,&pIDispatchNewObject ); if (SUCCEEDED(hr)) { IDirectoryObject * pIDONewOneToReturn = NULL; // Now get a IDirectoryObject * from // the IDispatch returned from MoveHere() hr =pIDispatchNewObject->QueryInterface(IID_IDirectoryObject ,(void**)pIDONewOneToReturn ); if (SUCCEEDED(hr)) { // Now take the PASSED interface. FREE it and reassign the new ptr // (note if there are ANY outstanding refs to the com object behind the ptr- they will still be // valid (from a COM point of view), but this object will return errors if you try to do anything // with it) (*ppIDOToMove)->Release(); // Copy the ptr from the QI'd one to the return for this function ppIDOToMove = &pIDONewOneToReturn; } pIDispatchNewObject->Release(); pIDispatchNewObject = NULL; } pIADsContainerDestination->Release(); pIADsContainerDestination = NULL; } SysFreeString(bsPathToMove); } pIADsToMove->Release(); pIADsToMove = NULL; } return hr; }
The following code fragment moves a group (or any object) to another location in the domain:
Dim oDirObjTo As IADsContainer Dim oNewObj As IADs Set oDirObjTo = GetObject(txtTo.Text) Set oNewObj = oDirObjTo.MoveHere(txtFrom.Text, vbNullString) MsgBox "New Object has been moved to " & oNewObj.ADsPath Set oDirObjTo = Nothing Set oNewObj = Nothing