Platform SDK: Active Directory, ADSI, and Directory Services |
The following code fragment contains a function that deletes a group in a domain:
//////////////////////////////////////////////////////////////////////////////////////////////////// /* DeleteADObject() - Deletes the passed object by AdsPath Parameters LPOLESTR pwszAdsPath - AdsPath of object to delete Optional Parameters: LPOLESTR pwszUser - User Name and Password, if the parameters are NOT passed, LPOLESTER pwszPassWord - Binding will use ADsGetObject, if the parameters - Are specified, will use ADsOpenObject, passing user name and password */ HRESULT DeleteADObject(LPOLESTR pwszAdsPath, LPOLESTR pwszUser,LPOLESTR pwszPassWord) { HRESULT hr; BSTR bsParentPath; IADs * pIADsToDelete = NULL; IDirectoryObject * pIDirObjectParent= NULL; VARIANT vCNToDelete; WCHAR pwszTemp[512]; VariantInit(&vCNToDelete); OutputDebugString(pwszAdsPath); OutputDebugString(L"\r\n"); // Bind to the object being deleted assert((pwszUser==NULL && pwszPassWord == NULL) || (pwszUser && pwszPassWord)); // If a username and password are passed in, use ADsOpenObject() // otherwise use ADsGetObject() if (!pwszUser) // No user password passed, use ADsOpenObject { hr = ADsGetObject( pwszAdsPath, IID_IADs,(void **)& pIADsToDelete); } else { hr = ADsOpenObject(pwszAdsPath, pwszUser, pwszPassWord, ADS_SECURE_AUTHENTICATION,IID_IADs, (void**) & pIADsToDelete); } if (SUCCEEDED(hr)) { // Get the parent path hr = pIADsToDelete->get_Parent(&bsParentPath); // Get the CN property for the object to delete hr = pIADsToDelete->Get(L"cn",&vCNToDelete); if (SUCCEEDED(hr)) { // ************************************************************ // Now bind to the parent // If a username and password are passed in, use ADsOpenObject() // otherwise use ADsGetObject() if (!pwszUser) // No user password passed, use ADsOpenObject { hr = ADsGetObject( bsParentPath, IID_IDirectoryObject,(void **)& pIDirObjectParent); } else { hr = ADsOpenObject(bsParentPath, pwszUser, pwszPassWord, ADS_SECURE_AUTHENTICATION,IID_IDirectoryObject, (void**) & pIDirObjectParent); } if (SUCCEEDED(hr)) { // Release the object to delete pIADsToDelete->Release(); pIADsToDelete =NULL; // Put the CN property into a string beginning with CN= swprintf(pwszTemp,L"cn=%s\n",vCNToDelete.bstrVal); // Ask the parent to delete the child hr =pIDirObjectParent->DeleteDSObject(pwszTemp); // Release the Parent Object pIDirObjectParent->Release(); pIDirObjectParent = NULL; } } SysFreeString(bsParentPath); } // If we have a IADsObject- we need to release it if ( pIADsToDelete) { // Release the object to delete pIADsToDelete->Release(); pIADsToDelete =NULL; } VariantClear(&vCNToDelete); return hr; }
The following code deletes a group in domain:
Dim x as IADs Set x = GetObject("LDAP://OU=myou,DC=Microsoft,DC=com") x.Delete("group", "cn=mygroup")