Directory Services

IADsCollection::GetObject

The IADsCollection::GetObject method retrieves an item of the collection.

HRESULT GetObject( 
  BSTR bstrName,
  VARIANT pvarItem
);

Parameters

bstrName
[in] The null-terminated Unicode string that specifies the name of the item. This is the same name passed to IADsCollection::Add when the item is added to the collection.
pvarItem
[in] Current value of the item. For an object, this corresponds to the IDispatch interface pointer on the object.

Return Values

This method supports the standard return values, including S_OK. For more information and other return values, see ADSI Error Codes.

Remarks

If you know the name of a session in the Sessions collection, call the IADsCollection::GetObject method explicitly to retrieve the session object.

Example Code [Visual Basic]

The following Visual Basic code example shows how to retrieve a named session object (mySession) from a collection of active file service sessions.

Dim fso As IADsFileServiceOperations 
Dim ses As IADsSession
Dim coll As IADsCollection
Dim mySessionName As String

Set fso = GetObject("WinNT://myComputer/FabrikamServer") 
Set coll = fso.Sessions

' Insert code to set mySessionName to the name of mySession.
 
' The following statement invokes IADsCollection::GetObject.
Set ses = coll.GetObject(mySessionName)

Example Code [C++]

The following C++ code example shows how to retrive a named session object (mySession) from a collection of active file service sessions.

HRESULT GetASessionObjectFromCollection(BSTR mySession)
{
	LPWSTR adspath = L"WinNT://myComputer/FabrikamServer";
	IUnknown *pUnk=NULL;
	HRESULT hr = S_OK;
	IADsCollection *pColl = NULL;
	IADsFileServiceOperations *pFso = NULL;
	IADs *pADsObj = NULL;
	VARIANT varObj;
	BSTR bstrObj = NULL;

	VariantInit(&varObj);
	hr = ADsGetObject(adspath, 
					IID_IADsFileServiceOperations,
					(void**)&pFso);
	if(FAILED(hr)) {goto Cleanup;}

	hr = pFso->Sessions(&pColl);
	if(FAILED(hr)) {goto Cleanup;}

	hr = pColl->GetObject(mySession, &varObj);
	V_DISPATCH(&varObj)->QueryInterface(IID_IADs,(void**)&pADsObj);
	hr = pADsObj->get_Class(&bstrObj);
	printf("Class of the object obtained from GetObject: %S\n",
			 bstrObj);

Cleanup:
	if(bstrObj) SysFreeString(bstrObj);
	if(pFso) pFso->Release();
	VariantClear(&varObj);
	if(pADsObj) pADsObj->Release();
	if(pColl) pColl->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

ADSI Error Codes, IADsCollection::Add, IDispatch, IEnumVARIANT