Knowledge Base

How To Enumerate Network Sessions with ADSI

Article ID: 198813

Article Last Modified on 9/28/2007


APPLIES TO


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)

Keywords: kbdswmanage2003swept kbapi kbhowto KB198813