none
WTSEnumerateSessions working for win 10 VPCs but not win7 VPCs RRS feed

  • Question

  • I am trying to manage a pool of 150 VPCs. currently there is a mix of win 7 and win 10 machines. I need to know whether or not the machines have an active RDP session connected to them and the user name connected to the session. I am using the following code to get the session info to determine this

    IntPtr server = OpenServer(ServerName);

    List<TerminalSessionData> ret = newList<TerminalSessionData>();

    IntPtr ppSessionInfo = IntPtr.Zero;

    Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);

    Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

    Int64 current = (int)ppSessionInfo;

    The enumerate sessions and open server methods are defined as

            [DllImport("wtsapi32.dll")]      

    static extern Int32 WTSEnumerateSessions(IntPtr hServer, [MarshalAs(UnmanagedType.U4)] Int32Reserved,

                [MarshalAs(UnmanagedType.U4)] Int32Version, ref IntPtr ppSessionInfo, [MarshalAs(UnmanagedType.U4)] ref Int32 pCount);

            [DllImport("wtsapi32.dll")]       

    static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);

    This is working perfectly for my win 10 VPCs but is not finding any active sessions on the win 7 VPCs.

    This code returns a minimum of 3 sessions for the win 10 machines if there is no active or disconnected session.

    The 3 sessions that come back are  0 - services, 1 - console, and 65536 - rdp-tcp with session 2 being the active or disconnected session.

    But it returns no session information for the win 7 machines, whether or not there is an active session connected.

    Any ideas what I might be missing or doing wrong?

    Thanks

    Eli Silverman



    • Edited by siphco Tuesday, December 10, 2019 12:57 PM
    Tuesday, December 10, 2019 5:04 AM

Answers

  • On the Win7 systems you may also need to tweak the registry -

    HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\AllowRemoteRPC
    AllowRemoteRPC is a DWORD value that should be set to 1.

    • Marked as answer by siphco Thursday, December 12, 2019 8:32 PM
    Wednesday, December 11, 2019 5:45 PM

All replies

  • Hi,

    Thanks for posting here.

    Did you get any fails of the function call, and if it fails, did you get any error code(by calling GetLastError immediately after fails).

    Make sure you have got enough session permissions metioned in the document:

    https://docs.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsenumeratesessionsa

    Session permissions:  

    • To enumerate a session, you must enable the query information permission. For more information, see Remote Desktop Services Permissions.
    • To change permissions on a session, use the Remote Desktop Services Configuration administrative tool.
    • To enumerate sessions running on a virtual machine hosted on a RD Virtualization Host server, you must be a member of the Administrators group on the RD Virtualization Host server.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 11, 2019 6:25 AM
    Moderator
  • On the Win7 systems you may also need to tweak the registry -

    HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\AllowRemoteRPC
    AllowRemoteRPC is a DWORD value that should be set to 1.

    • Marked as answer by siphco Thursday, December 12, 2019 8:32 PM
    Wednesday, December 11, 2019 5:45 PM