Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to know if there is a valid session running on the machine or not ?

How to know if there is a valid session running on the machine or not ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kushagra Tiwari
    wrote on last edited by
    #1

    Hello All, I have a service and I want to spawn a process through it but only at the time when I found no user logged on to the machine. i.e I have a function which is executed on a timely basis and it should check the number of active user sessions on that machine and if the count comes to be zero it should spawn the process in system account . Regards, Kushagra

    L 1 Reply Last reply
    0
    • K Kushagra Tiwari

      Hello All, I have a service and I want to spawn a process through it but only at the time when I found no user logged on to the machine. i.e I have a function which is executed on a timely basis and it should check the number of active user sessions on that machine and if the count comes to be zero it should spawn the process in system account . Regards, Kushagra

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi Kushagra, You could use the NetWkstaUserEnum Function[^] to enumerate logged on users. Best Wishes, -David Delaune

      K 2 Replies Last reply
      0
      • L Lost User

        Hi Kushagra, You could use the NetWkstaUserEnum Function[^] to enumerate logged on users. Best Wishes, -David Delaune

        K Offline
        K Offline
        Kushagra Tiwari
        wrote on last edited by
        #3

        Thanks man , That is waht I was looking for :) Kushagra

        C 1 Reply Last reply
        0
        • L Lost User

          Hi Kushagra, You could use the NetWkstaUserEnum Function[^] to enumerate logged on users. Best Wishes, -David Delaune

          K Offline
          K Offline
          Kushagra Tiwari
          wrote on last edited by
          #4

          NetWkstaUserEnum The NetWkstaUserEnum function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons. I want only the list of interactive users and not service and batch logons , can you please let me know the way to do it ???

          C L 2 Replies Last reply
          0
          • K Kushagra Tiwari

            Thanks man , That is waht I was looking for :) Kushagra

            C Offline
            C Offline
            Covean
            wrote on last edited by
            #5

            Beside the solution provided by the poster before, here is an alternative idea. Catch the SERVICE_CONTROL_SESSIONCHANGE event in your service (only with HandlerEx possible). This event is triggered every time a user logs on / logs off / remote log on / remote log off etc. Combined with the solution before you will be able to start your app at the log off of the last user and stops or pauses if an user logs in. Greetings Covean

            1 Reply Last reply
            0
            • K Kushagra Tiwari

              NetWkstaUserEnum The NetWkstaUserEnum function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons. I want only the list of interactive users and not service and batch logons , can you please let me know the way to do it ???

              C Offline
              C Offline
              Covean
              wrote on last edited by
              #6

              Maybe try for every logged-in-user you enumurate NetUserGetInfo with level 23 where flag is UF_NORMAL_ACCOUNT.

              L K 2 Replies Last reply
              0
              • C Covean

                Maybe try for every logged-in-user you enumurate NetUserGetInfo with level 23 where flag is UF_NORMAL_ACCOUNT.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                How is that suppose to work? Nearly all user accounts are UF_NORMAL_ACCOUNT regardless of session ID. Best Wishes, -David Delaune

                C 1 Reply Last reply
                0
                • C Covean

                  Maybe try for every logged-in-user you enumurate NetUserGetInfo with level 23 where flag is UF_NORMAL_ACCOUNT.

                  K Offline
                  K Offline
                  Kushagra Tiwari
                  wrote on last edited by
                  #8

                  Can you explain this with an example ? My goal is simple I want to count the number of active sessions on a server or machine which will be my own base machine , if count comes 0 I will execute my script else I wont. Kushagra

                  1 Reply Last reply
                  0
                  • K Kushagra Tiwari

                    NetWkstaUserEnum The NetWkstaUserEnum function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons. I want only the list of interactive users and not service and batch logons , can you please let me know the way to do it ???

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    If you need to filter out the remote sessions then you will be better off using: LsaEnumerateLogonSessions Function[^] to enumerate the sessions. You will then need to use the LsaGetLogonSessionData Function[^] to populate a SECURITY_LOGON_SESSION_DATA Structure[^] and check for LogonType of RemoteInteractive. You should filter out types except Interactive. Best Wishes, -David Delaune

                    K 1 Reply Last reply
                    0
                    • L Lost User

                      How is that suppose to work? Nearly all user accounts are UF_NORMAL_ACCOUNT regardless of session ID. Best Wishes, -David Delaune

                      C Offline
                      C Offline
                      Covean
                      wrote on last edited by
                      #10

                      Sorry but I just hoped that this will work. I hadn't the time to test. But as I saw you had an better idea I hope that works. Regardless of this, I also would think about the idea I posted at first. Greetings Covean

                      1 Reply Last reply
                      0
                      • L Lost User

                        If you need to filter out the remote sessions then you will be better off using: LsaEnumerateLogonSessions Function[^] to enumerate the sessions. You will then need to use the LsaGetLogonSessionData Function[^] to populate a SECURITY_LOGON_SESSION_DATA Structure[^] and check for LogonType of RemoteInteractive. You should filter out types except Interactive. Best Wishes, -David Delaune

                        K Offline
                        K Offline
                        Kushagra Tiwari
                        wrote on last edited by
                        #11

                        Finally figured it how it has to be done : Posting my anwer here : #include <windows.h> #include <stdio.h> // The following constant may be defined by including NtStatus.h. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) // The LSA authentication functions are available in Unicode only. #define UNICODE int _cdecl main() { PLUID sessions; ULONG count; NTSTATUS retval; int i; retval = LsaEnumerateLogonSessions(&count, &sessions); if (retval != STATUS_SUCCESS) { wprintf (L"LsaEnumerate failed %lu\n", LsaNtStatusToWinError(retval)); return 1; } wprintf (L"Enumerate sessions received %lu sessions.\n", count); // Process the array of session LUIDs... for (i =0;i < (int) count; i++) { GetSessionData (&sessions[i]); } // Free the array of session LUIDs allocated by the LSA. LsaFreeReturnBuffer(sessions); return 0; } it works ffine...SOme body please mark this answer and close the threda as I cant :) Kushagra

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups