Directory Services

IADsSecurityDescriptor::CopySecurityDescriptor

The IADsSecurityDescriptor::CopySecurityDescriptor method copies an ADSI security descriptor object that holds security data about an object.

HRESULT CopySecurityDescriptor( 
  IDispatch** ppSecurityDescriptor
);

Parameters

ppSecurityDescriptor
[out] Pointer to a pointer to a security descriptor object.

Return Values

This method returns the standard return values, including E_INVALIDARG, E_OUTOFMEMORY, E_UNEXPECTED, and E_FAIL, as well as S_OK. For more information about other return values, see ADSI Error Codes.

Example Code [Visual Basic]

The following code example copies the security descriptor of one object and applies it to the second object.

Dim ou1 As IADs
Dim ou2 As IADs
Dim sd As IADsSecurityDescriptor
Dim sdNew As IADsSecurityDescriptor
On Error GoTo Cleanup
 
Set ou1 = GetObject("LDAP://OU=Sales,DC=Fabrikam,DC=com")
Set ou2 = GetObject("LDAP://OU=Finance,DC=Fabrikam,DC=com")
Set sd = ou1.Get("ntSecurityDescriptor")
Set sdNew = sd.CopySecurityDescriptor
ou2.Put "ntSecurityDescriptor", sdNew
ou2.SetInfo

Cleanup:
	If (Err.Number<>0) Then
		MsgBox("An error has occurred. " & Err.Number)
	End If
	Set ou1 = Nothing
	Set ou2 = Nothing
	Set sd = Nothing
	Set sdNew = Nothing

Example Code [C++]

The following code example copies the security descriptor of one object and applies it to the target object.

HRESULT CopySD(IADs *pSource, IADs *pTarget)
{
	IADsSecurityDescriptor *pSourceSD = NULL;
	IADsSecurityDescriptor *pTargetSD = NULL; 
	IDispatch *pDisp = NULL;

	HRESULT hr = S_OK;
	VARIANT varSource,varTarget;

	VariantInit(&varSource);
	VariantInit(&varTarget);

	if((pSource==NULL) || (pTarget==NULL))
	{
		return E_FAIL;
}

	hr = pSource->Get(CComBSTR("ntSecurityDescriptor"), &varSource);
	if(FAILED(hr))
	{
		goto Cleanup;
}


	hr = V_DISPATCH(&varSource)->QueryInterface(IID_IADsSecurityDescriptor,(void**)&pSourceSD);
	if(FAILED(hr))
	{
		goto Cleanup;
}

	hr = pSourceSD->CopySecurityDescriptor(&pDisp);
	if(FAILED(hr))
	{
		goto Cleanup;
}

	V_DISPATCH(&varTarget) = pDisp;
	V_VT(&varTarget) = VT_DISPATCH;

	hr = pTarget->Put(CComBSTR("ntSecurityDescriptor"), varTarget);
	hr = pTarget->SetInfo();
	
Cleanup:
	VariantClear(&varSource);
	VariantClear(&varTarget);
	if(pSourceSD) 
	{
		pSourceSD->Release();
}
	if(pDisp) 
	{
		pDisp->Release();
}
	return hr;
}

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Iads.h.

See Also

IADsAccessControlEntry, IADsAccessControlList