Directory Services

Example Code for Adding a Member to a Group

The following C++ code example adds an existing member to a group:

[C++]
//////////////////////////////////////////////////////////////////////////////////////
/*  AddMemberToGroup() - Adds the passed directory object as a member of passed group
 
	Parameters
 
		IADsGroup * pGroup - Group to hold the new IDirectoryObject.
		IADs* pIADsNewMember - Object which will become a member of the group.
							 Object can be a user, contact, or group.
*/
HRESULT AddMemberToGroup(IADsGroup * pGroup, IADs* pIADsNewMember)
{
	HRESULT hr = E_INVALIDARG;
	if ((!pGroup)||(!pIADsNewMember))
		return hr;
 
	// Use the IADs::get_ADsPath() member to get the ADsPath.
	// When the ADsPath string is returned, to add the new
	// member to the group, call the IADsGroup::Add() member,
	// passing in the ADsPath string.
	// Query the new member for it's AdsPath.
	// This is a fully qualified LDAP path to the object to add.
	BSTR bsNewMemberPath;
	hr = pIADsNewMember->get_ADsPath(&bsNewMemberPath); 
	if (SUCCEEDED(hr))
	{
 
		// Use the IADsGroup interface to add the new member.
		// Pass the LDAP path to the 
		// new member to the IADsGroup::Add() member
		hr = pGroup->Add(bsNewMemberPath);
 
		// Free the string returned from IADs::get_ADsPath()
		SysFreeString(bsNewMemberPath);
}
	return hr;
}

The following Visual Basic code example adds an existing member called Jeff Smith to a the Users group.

[Visual Basic]
Dim oGroup As IADsGroup
On Error GoTo CleanUp
Set oGroup = GetObject("LDAP://CN=Users,CN=Builtin,DC=fabrikam,DC=com")
oGroup.Add ("LDAP://CN=Jeff Smith,CN=Users,DC=fabrikam,DC=com")
Set oGroup = Nothing
Exit Sub
   
CleanUp:
   MsgBox ("An error occurred.")
   Set oGroup = Nothing

The following code example demonstrates how to convert an objectSid into a bindable string.

[C++]
HRESULT VariantArrayToBytes(VARIANT Variant, LPBYTE *ppBytes, DWORD *pdwBytes);

/***************************************************************************

	GetLDAPSidBindStringFromVariantSID()

	Converts a SID in VARIANT form, such as an objectSid value, and converts 
	it into a bindale string in the form:

	LDAP://<SID=xxxxxxx...>

	The returned string is allocated with AllocADsMem and must be freed by 
	the caller with FreADsMem.

***************************************************************************/

LPWSTR GetLDAPSidBindStringFromVariantSID(VARIANT vSID)
{
	LPWSTR pwszReturn = NULL;

	if(vSID.vt != VT_EMPTY) 
	{
		HRESULT hr;
		LPBYTE pByte;
		DWORD dwBytes = 0;

		hr = VariantArrayToBytes(vSID, &pByte, &dwBytes);
		if(S_OK == hr)
		{
			// Convert the BYTE array into a string of hex characters.
			CComBSTR sbstrTemp = "LDAP://<SID=";

			for(DWORD i = 0; i < dwBytes; i++)
			{
				WCHAR wszByte[3];

				wsprintfW(wszByte, L"%02x", pByte[i]);
				sbstrTemp += wszByte;
		}

			sbstrTemp += ">";
			pwszReturn = (LPWSTR)AllocADsMem((sbstrTemp.Length() + 1) * sizeof(WCHAR));
			if(pwszReturn)
			{
				lstrcpyW(pwszReturn, sbstrTemp.m_str);
		}

			FreeADsMem(pByte);
	}
}

	return pwszReturn;
}

/***************************************************************************

	VariantArrayToBytes()

	This function converts a VARIANT array into an array of BYTES. This 
	function allocates the buffer using AllocADsMem. It is the caller's 
	responsibility to free this memory with FreeADsMem when it is no longer 
	needed.

***************************************************************************/

HRESULT VariantArrayToBytes(VARIANT Variant, LPBYTE *ppBytes, DWORD *pdwBytes)
{
	if(!(Variant.vt & VT_ARRAY) ||
		!Variant.parray ||
		!ppBytes ||
		!pdwBytes)
	{
		return E_INVALIDARG;
}

	*ppBytes = NULL;
	*pdwBytes = 0;

	HRESULT hr = E_FAIL;
	SAFEARRAY *pArrayVal = NULL;
	CHAR HUGEP *pArray = NULL;

	// Retrieve the safe array.
	pArrayVal = Variant.parray;
	DWORD dwBytes = pArrayVal->rgsabound[0].cElements;
	*ppBytes = (LPBYTE)AllocADsMem(dwBytes);
	if(NULL == *ppBytes) 
	{
		return E_OUTOFMEMORY;
}

	hr = SafeArrayAccessData(pArrayVal, (void HUGEP * FAR *) &pArray);
	if(SUCCEEDED(hr))
	{
		//Copy the bytes to the safe array.
		CopyMemory(*ppBytes, pArray, dwBytes);
		SafeArrayUnaccessData( pArrayVal );
		*pdwBytes = dwBytes;
}

	return hr;
}