public static void EnumSessions(string server) { IntPtr BufPtr; int res = 0; Int32 er = 0, tr = 0, resume = 0; BufPtr = (IntPtr)Marshal.SizeOf(typeof(SESSION_INFO_502)); SESSION_INFO_502[] results = new SESSION_INFO_502[0]; do { res = NetSessionEnum(server, null, null, 502, out BufPtr, -1, ref er, ref tr, ref resume); results = new SESSION_INFO_502[er]; if (res == (int)NERR.ERROR_MORE_DATA || res == (int)NERR.NERR_Success) { IntPtr p = BufPtr; for (int i = 0; i < er - 1; i++) { SESSION_INFO_502 si = (SESSION_INFO_502)Marshal.PtrToStructure(p, typeof(SESSION_INFO_502)); p = BufPtr + i * Marshal.SizeOf(typeof(SESSION_INFO_502)); Console.WriteLine(si.sesi502_cname + ",user_name:" + si.sesi502_username); } } Marshal.FreeHGlobal(BufPtr); }while (res == (int)NERR.ERROR_MORE_DATA); }
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); }
private void fill_netserver_page() { try { SERVER_INFO_102 serv_info = WinApiNETwrapper.GetServerInfo_102(resource_intern.lpRemoteName); textBoxNetserverPlatformId.Text = serv_info.sv102_platform_id.ToString(); textBoxNetserverSoftwareType.Text = IOhelper.NetserverTypeToString(serv_info.sv102_type); textBoxPlatformVersion.Text = string.Format("{0}.{1}", serv_info.GetVersionMajor(), serv_info.sv102_version_minor); textBoxNetserverMaxUsers.Text = serv_info.sv102_users.ToString(); textBoxNetserverDisconnectTime.Text = serv_info.sv102_disc.ToString(); textBoxNetserverAnnounceTime.Text = serv_info.sv102_announce.ToString(); textBoxNetserverUsersPerLicense.Text = serv_info.sv102_licenses.ToString(); textBoxNetserverUserPath.Text = serv_info.sv102_userpath; } catch (Exception ex) { errorProvider1.SetIconAlignment(textBoxNetserverPlatformId, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(textBoxNetserverPlatformId, ex.Message); } try { NetRemoteComputerSupportsFeatures features = WinApiNETwrapper.GetComputerSupports(resource_intern.lpRemoteName); textBoxNetserverFeatures.Text = features.ToString(); } catch (Exception ex) { errorProvider1.SetIconAlignment(textBoxNetserverFeatures, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(textBoxNetserverFeatures, ex.Message); } try { TIME_OF_DAY_INFO dt = WinApiNETwrapper.GetServerTime(resource_intern.lpRemoteName); textBoxNetserverDatetime.Text = string.Format("{0} {1},{2}", dt.GetCurrentDatetime().ToLongDateString(), dt.GetCurrentDatetime().ToLongTimeString(), dt.GetCurrentDatetime().Millisecond); textBoxNetserverUptime.Text = dt.GetUptime().ToString(); } catch (Exception ex) { errorProvider1.SetIconAlignment(textBoxNetserverDatetime, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(textBoxNetserverDatetime, ex.Message); } //NET_DISPLAY_GROUP[] groups = WinApiNETwrapper.QueryDisplayInfoGroup(resource_intern.lpRemoteName); //NET_DISPLAY_MACHINE[] machines = WinApiNETwrapper.QueryDisplayInfoMachine(resource_intern.lpRemoteName); //NET_DISPLAY_USER[] users = WinApiNETwrapper.QueryDisplayInfoUser(resource_intern.lpRemoteName); try { SERVER_TRANSPORT_INFO_1[] transports = WinApiNETwrapper.ServerTransportEnum_1(resource_intern.lpRemoteName); for (int i = 0; i < transports.Length; i++) { ListViewItem lvi = new ListViewItem(); lvi.Text = transports[i].svti1_transportname; lvi.SubItems.Add(transports[i].TransportAddress); lvi.SubItems.Add(transports[i].svti1_networkaddress); lvi.SubItems.Add(transports[i].svti1_domain); lvi.SubItems.Add(transports[i].svti1_numberofvcs.ToString()); listViewTransports.Items.Add(lvi); } listViewTransports.Dock = DockStyle.Fill; } catch (Exception ex) { errorProvider1.SetIconAlignment(listViewTransports, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(listViewTransports, ex.Message); } /* net sessions */ errorProvider1.SetIconAlignment(listViewSessions, ErrorIconAlignment.MiddleLeft); Array sessions = null; NetSessionEnumLevel session_level = NetSessionEnumLevel.INFO_502; try { //try level 502 sessions = WinApiNETwrapper.NetSessionEnum(resource_intern.lpRemoteName, null, null, session_level); } catch (Exception) { //if exception try level 10 session_level = NetSessionEnumLevel.INFO_10; try { sessions = WinApiNETwrapper.NetSessionEnum(resource_intern.lpRemoteName, null, null, session_level); } catch (Exception ex_10) { errorProvider1.SetError(listViewSessions, ex_10.Message); } } if (sessions != null) { listViewSessions.Dock = DockStyle.Fill; for (int i = 0; i < sessions.Length; i++) { ListViewItem lvi = new ListViewItem(); switch (session_level) { case NetSessionEnumLevel.INFO_502: SESSION_INFO_502 info_502 = (SESSION_INFO_502)sessions.GetValue(i); lvi.Text = info_502.sesi502_cname; lvi.SubItems.Add(info_502.sesi502_username); lvi.SubItems.Add(info_502.sesi502_num_opens.ToString()); lvi.SubItems.Add(info_502.TimeActive.ToString()); lvi.SubItems.Add(info_502.TimeIdle.ToString()); lvi.SubItems.Add(info_502.sesi502_user_flags.ToString()); lvi.SubItems.Add(info_502.sesi502_cltype_name); lvi.SubItems.Add(info_502.sesi502_transport); break; case NetSessionEnumLevel.INFO_10: SESSION_INFO_10 info_10 = (SESSION_INFO_10)sessions.GetValue(i); lvi.Text = info_10.sesi10_cname; lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(info_10.TimeActive.ToString()); lvi.SubItems.Add(info_10.TimeIdle.ToString()); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); break; } listViewSessions.Items.Add(lvi); } } /* end of net sessions */ /* open files */ try { Array files = WinApiNETwrapper.NetFileEnum(resource_intern.lpRemoteName, null, null, NetFileEnumLevel.INFO_3); listViewFiles.Dock = DockStyle.Fill; for (int i = 0; i < files.Length; i++) { FILE_INFO_3 f_info = (FILE_INFO_3)files.GetValue(i); ListViewItem lvi = new ListViewItem(); lvi.Text = string.Format("0x{0:X}", f_info.fi3_id); lvi.SubItems.Add(f_info.fi3_permission.ToString()); lvi.SubItems.Add(f_info.fi3_num_locks.ToString()); lvi.SubItems.Add(f_info.fi3_pathname); lvi.SubItems.Add(f_info.fi3_username); listViewFiles.Items.Add(lvi); } } catch (Exception ex_files) { errorProvider1.SetIconAlignment(listViewFiles, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(listViewFiles, ex_files.Message); } /* end of open files */ }