Directory Services

ADsBuildVarArrayStr

The ADsBuildVarArrayStr function builds a variant array from an array of Unicode strings.

HRESULT ADsBuildVarArrayStr(
  LPWSTR* lppPathNames,
  DWORD dwPathNames,
  VARIANT* pVar
);

Parameters

lppPathNames
[in] Array of null-terminated Unicode strings.
dwPathNames
[in] Number of Unicode entries in the given array.
pVar
[out] Pointer to the resulting variant array.

Return Values

This method supports the standard return values, as well as the following.

Return Code Description
S_OK The array was built successfully.
E_FAIL The array cannot be created.

For more information about other return values, see ADSI Error Codes.

Remarks

To support Automation, use the ADsBuildVarArrayStr function to convert Unicode strings to a variant array of strings.

Example Code [C++]

The following code example shows how to use the ADsBuildVarArrayStr function to convert object class names from Unicode strings to a variant array of strings.

[C++]
HRESULT EnumObject(LPWSTR pszADsPath,
				 LPWSTR * lppClsNames,
				 DWORD dwClsNames)
{
	ULONG ulFetched = 0L;
	IEnumVARIANT * pEnumVar = NULL;
	VARIANT varFilter, varArray[MAX_ADS_ENUM];
	HRESULT hr;
	IADsContainer * pADsContainer = NULL;
	DWORD dwObjects = 0, dwEnumCount=0, i=0;
	BSTR bstrName;
	BOOL fContinue=TRUE;
 
	hr = ADsGetObject(pszADsPath,
					 IID_IADsContainer,
					 (void**)&pADsContainer);
	if (FAILED(hr)) goto cleanup;
 
	// Create a string array of class names as search filters.
	VariantInit(&varFilter);
	hr = ADsBuildVarArrayStr(lppClsNames, dwClsNames, &varFilter);
	if (FAILED(hr)) goto cleanup;
 
	// Apply filters to objects in the container.
	hr = pADsContainer->put_Filter(varFilter);
	if(FAILED(hr)) goto cleanup;
 
	// Create an enumerator.
	hr = ADsBuildEnumerator(pADsContainer, &pEnumVar);
	if(FAILED(hr)) goto cleanup;
 
	// Enumerate the objects and print the names.
	while(fContinue) {
		IADs* pObject;
		hr = ADsEnumerateNext(pEnumVar, MAX_ADS_ENUM,
							varArray, &ulFetched);
		if(hr == S_FALSE) fContinue = FALSE;
		dwEEnumCount++;
 
		for (i=0; i<ulFetched; i++) {
			IDispatch *pDispatch = NULL;
			pDispatch = varArray[I].pDispVal;
			hr = pDispatch->QueryInterface(IID_IADs,
										(void**) &pObject);
			if (FAILED(hr)) goto cleanup;
 
			hr = pObject->get_Name(&bstrName);
			if(FAILED(hr)) goto cleanup;
			printf(" Object name: %S\n",bstrName);
 
			// Release the ADSI object.
			SysFreeString(bstrname);
			pObject->Release();
			pDispatch->Release();
	}
		memset(varArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
		dwObjects += ulFetched;
}
	hr = S_OK;
 
cleanup:
	if(bstrName) SysFreeString(bstrName);
	if(pEnumvar) ADsFreeEnumerator(pEnumVar);
	if(pADsContainer) pADsContainer->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 Adshlp.h.
Library: Use ActiveDS.lib.

See Also

ADSI Error Codes, ADSI Functions, ADsBuildVarArrayInt