Microsoft KB Archive/198813

{|
 * width="100%"|

HOWTO: Enumerate Network Sessions with ADSI

 * }

Q198813

-

The information in this article applies to:


 * Microsoft Active Directory Service Interfaces, versions 2.0, 2.5

-

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