Ever wanted to get information about who's logged on to your machine This example shows you the interop way to get hold of active sessions on your box using System;?. Using System.Runtime.InteropServices;
Namespace NetSession {[Flags] public enum sessusrflgs {guest = 0x00000001, // session is logged on as a guest noencryption = 0x00000002 // session is not use encryption}
[StructLayoutAttribute (LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SESSION_INFO_502 {public string Computer; public string User; public int NumOpens; public int Seconds; public int IdleSeconds; public SessUsrFlgs UsrFlags; public string LmType; public string Transport; }
public class NetSessionTest {[DllImport ( "netapi32.dll", CharSet = CharSet.Unicode)] public static extern int NetSessionEnum (string srvName, string cltName, string usrName, int level, out IntPtr bufPtr, int prefmaxlen, out int entriesread, out INT TOTALENTRIES, REF INTPTR RESUME_HANDLE);
[DLLIMPORT ("Netapi32.dll", Exactspelling = true)] Public Extern Static Int NetapUfferFree (INTPTR BUFPTR);
PRIVATE const Int error_more_data = 234;
Public static void main () {Int Read, Total, S; INTPTR PTR, RHANDLE = INTPTR.ZERO; TYPE TYP = TypeOf (session_info_502); int size = marshal.sizeof (TYP); session_info_502 si;
S = NetsessionNum (NULL, NULL, NULL, 502, OUT PTR, -1, OUT Read, Out Total, Ref Rhandle); if ((S! = 0) && (s! = error_more_data) Return;
Console.writeline ("Computer: User: Opens: Seconds:"); int run = (int) PTR; for (int i = 0; i