public static Array NetSessionEnum(string server_name, string client_name, string user_name, NetSessionEnumLevel level) { Array ret = null; IntPtr buffer = IntPtr.Zero; uint prefmaxlen = WinApiNET.MAX_PREFERRED_LENGTH; int entriesread = 0; int totalentris = 0; uint resume_handle = 0; try { int res = WinApiNET.NetSessionEnum (server_name, client_name, user_name, level, ref buffer, prefmaxlen, ref entriesread, ref totalentris, ref resume_handle); if (res != WinApiNET.NERR_Success) { throw new Win32Exception(res); } switch (level) { case NetSessionEnumLevel.INFO_0: ret = SESSION_INFO_0.FromBuffer(buffer, entriesread); break; case NetSessionEnumLevel.INFO_1: ret = SESSION_INFO_1.FromBuffer(buffer, entriesread); break; case NetSessionEnumLevel.INFO_2: ret = SESSION_INFO_2.FromBuffer(buffer, entriesread); break; case NetSessionEnumLevel.INFO_10: ret = SESSION_INFO_10.FromBuffer(buffer, entriesread); break; case NetSessionEnumLevel.INFO_502: ret = SESSION_INFO_502.FromBuffer(buffer, entriesread); break; } } finally { if (buffer != IntPtr.Zero) { WinApiNET.NetApiBufferFree(buffer); } } return(ret); }
public static Dictionary <int, string[]> EnumSessions(CredentialEntry ce, string sServer) { int entriesread = 0; int totalentries = 0; int hResumeHandle = 0; IntPtr lpBuf = IntPtr.Zero; Dictionary <int, string[]> SessionList = new Dictionary <int, string[]>(); if (!EnsureNullSession(sServer, ce)) { return(null); } try { int nRet = NetSessionEnum(sServer, null, null, 1, out lpBuf, -1, ref entriesread, ref totalentries, ref hResumeHandle); Logger.Log(String.Format("{0}: nRet={1}, entriesread={2}, totalentries={3}", "Session.EnumSessions(): NetSessionEnum()", nRet, entriesread, totalentries), Logger.netAPILogLevel); if (nRet != 0) { throw new AuthSessionException(ErrorCodes.WIN32String(nRet), null); } } catch (Exception ex) { Logger.LogException("Session.EnumSessions", ex); return(null); } // iterate through the entries IntPtr lpTemp = lpBuf; for (int i = 0; i < entriesread; i++) { SESSION_INFO_1 s1 = (SESSION_INFO_1)Marshal.PtrToStructure(lpTemp, typeof(SESSION_INFO_1)); bool bIsGuest = (s1.sesi1_user_flags & SESS_GUEST) == SESS_GUEST; string IsGuest = ""; if (bIsGuest) { IsGuest = "Yes"; } else { IsGuest = "No"; } string[] sSessionInfo = { s1.sesi1_username, s1.sesi1_cname, s1.sesi1_num_opens.ToString(), TimeSpan.FromSeconds((double)s1.sesi1_time).ToString(), TimeSpan.FromSeconds((double)s1.sesi1_idle_time).ToString(), IsGuest }; SessionList.Add(i, sSessionInfo); lpTemp = (IntPtr)((int)lpTemp + Marshal.SizeOf(s1)); } NetApiBufferFree(lpBuf); return(SessionList); }