How To Enumerate Network Sessions with ADSI (198813)



The information in this article applies to:

  • Microsoft Active Directory Service Interfaces 2.0
  • Microsoft Active Directory Service Interfaces 2.5

This article was previously published under Q198813

SUMMARY

Active Directory Service Interfaces (ADSI) can be used to enumerate network sessions programmatically to receive the information that is displayed by Microsoft Windows NT Server Manager.

MORE INFORMATION

The following code example demonstrates how to enumerate the connected users and how to define the computer from which they are connecting.

With Visual C++:
// This code assumes that CoInitialize() has been called
IADsFileServiceOperations *pFSOperations; 
IADsSession *pSession;
IADsCollection *pCollection;
IEnumVARIANT *pEnum;
LPUNKNOWN pUnk;
VARIANT var;
IDispatch *pDisp;
BSTR bstrUser;
BSTR bstrComputer;
ULONG lFetch;
HRESULT hr;
	
// Bind to the target server service
ADsGetObject(L"WinNT://DOMAIN/SERVER/lanmanserver", 
	IID_IADsFileServiceOperations, (void**) &pFSOperations );
	
// Retrieve the active sessions
pFSOperations->Sessions(&pCollection);
pFSOperations->Release();
	
// Get an enumerator
pCollection->get__NewEnum( &pUnk );
pCollection->Release();
	
pUnk->QueryInterface( IID_IEnumVARIANT, (void**) &pEnum );
pUnk->Release();
		
// Now Enumerate 
	
(HRESULT) hr = pEnum->Next( 1, &var, &lFetch );
	
while( hr == S_OK )
		
{
		
	if ( lFetch == 1 )
			
	{
			
		pDisp = V_DISPATCH(&var);
			
		pDisp->QueryInterface( IID_IADsSession, (void**)&pSession); 
			pSession->get_Computer(&bstrComputer);
			pSession->get_User(&bstrUser); 
			wprintf(L"User: %s  Computer: %s\n",(WCHAR*)bstrUser,(WCHAR*)bstrComputer);    
			pSession->Release();
			SysFreeString(bstrComputer);
			SysFreeString(bstrUser);
	}
		
	VariantClear(&var);
	hr = pEnum->Next( 1, &var, &lFetch );
		
};
	
pEnum->Release();
				
With Visual Basic:
Dim adsFSOps As IADsFileServiceOperations
Dim adsSession As IADsSession
Dim adsSessions As IADsCollection
' Replace DOMAIN & SERVER with the appropriate domain and server names
Set adsFSOps = GetObject("WinNT://DOMAIN/SERVER/lanmanserver")
' Enumerate sessions
Set adsSessions = adsFSOps.Sessions
For Each adsSession In adsSessions
  Debug.Print "User: " & adsSession.User & "   Computer:  " & adsSession.Computer
Next adsSession
				

REFERENCES

For additional information about Active Directory Services Interfaces, see the following Microsoft Web site at:
For additional information about the use of Printjobs, Sessions, and Resources with ADSI, click the article number below to view the article in the Microsoft Knowledge Base:

201033 How To Access ADSI PrintJob, Session, and Resource Objects

For additional information about manipulation of fileshares with ADSI, click the article number below to view the article in the Microsoft Knowledge Base:

169398 How To Manipulate File Shares with ADSI (VB Sample)


Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbDSWManage2003Swept kbAPI kbhowto KB198813