Article ID: 198813
Article Last Modified on 9/28/2007
// 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
201033 How To Access ADSI PrintJob, Session, and Resource Objects
169398 How To Manipulate File Shares with ADSI (VB Sample)
Keywords: kbdswmanage2003swept kbapi kbhowto KB198813