How to receive event when network is connected and also when user logs in

前端 未结 6 1603
再見小時候
再見小時候 2021-02-06 02:32

I have a service that runs and I\'d like to receive notification when:

a) the network is connected.

b) when a user logs in to the machine.

How can I do

6条回答
  •  萌比男神i
    2021-02-06 03:07

    To be notified when any user logs in to the machine, call WTSRegisterSessionNotification, passing NOTIFY_FOR_ALL_SESSIONS, and listen for the WM_WTSSESSION_CHANGE message in the message loop.

    In the message, cast the wParam to the Microsoft.Win32.SessionSwitchReason enum to find out what happened, and pass the lParam to WTSQuerySessionInformation to find the username.

    [DllImport("Wtsapi32.dll", CharSet=.CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); 
    
    //Pass this value in dwFlags
    public const int NOTIFY_FOR_ALL_SESSIONS          =  0x1; 
    
    //Listen for this message
    public const int WM_WTSSESSION_CHANGE = 0x02B1;
    
    //Call this method before exiting your program
    [DllImport("Wtsapi32.dll", CharSet=.CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); 
    

提交回复
热议问题