Article ID: 198813 - Last Review: September 28, 2007 - Revision: 2.3

How To Enumerate Network Sessions with ADSI

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q198813
Expand all | Collapse all

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:
http://msdn2.microsoft.com/library/aa772170.aspx (http://msdn2.microsoft.com/library/aa772170.aspx)

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  (http://support.microsoft.com/kb/201033/EN-US/ ) 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  (http://support.microsoft.com/kb/169398/EN-US/ ) How To Manipulate File Shares with ADSI (VB Sample)

APPLIES TO
  • Microsoft Active Directory Service Interfaces 2.0
  • Microsoft Active Directory Service Interfaces 2.5
Keywords: 
kbdswmanage2003swept kbapi kbhowto KB198813
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.