Use C # to get idle time for Terminal Services (Terminal services) sessions

Source: Internet
Author: User
Tags session id

Use C # to get idle time for Terminal Services (Terminal services) sessions

Tuuzed (Aberdeen) Posted: February 29, 2008
Copyright notice: Can be reproduced arbitrarily, please be sure to use hyperlinks in the form of the original source of the article and the author's information and this statement.
Http://www.cppblog.com/tuuzed/archive/2008/02/29/43424.html


Original: Guy Teverovsky
Translation: Aberdeen tuuzed
Source: Querying TS session idle time with C #
Original url:http://blogs.microsoft.co.il/blogs/guyt/archive/2007/10/06/querying-ts-session-idle-time-with-c.aspx
This article Special statement: The original copyright belongs to the original author Guy Teverovsky All, this translation can be reproduced arbitrarily, when reproduced, please be sure to use hyperlinks in the form of the original source, translation source, translator and this statement.

The beginning is a challenge. My colleague is writing a program to get idle time for a remote computer Terminal Services session. At first, we thought of using WMI (Wmi:windows Management Instrumentation Management specification), but there was no valuable information, so we started in the Win32 API (api:application Programming Interface Application Interface). First, the word "Terminal Services API" makes us feel hopeful, while the inside of the Wtsquerysessioninformation function also caught my attention, but, next to Wts_info_ The comments for class enum types have to make us return to Google again:

Later, I found a seemingly interesting API function: Winstationqueryinformationw. But there are some problems with this function:

1. This is an intrinsic function , which is implemented within the Winsta.dll, unlike other exposed WTS API functions that are inside the wtsapi32.dll;
2. return value of the function (a structure): Winstationqueryinformationw, the code for this structure does not have any information on MSDN;
3. The data on MSDN says that the handle to the query (handle) is usually only servername_current. This means that you cannot query a remote Terminal server (however, by using Depends.exe (the tool that comes with Visual Studio) to view the EXE file for Terminal Services Manager (Terminal Services Manager), This function is also used extensively by the manager).

I decided to try and see if I could solve the above problems:

1. "This is an intrinsic function"--well ... No way, there is no other way to get the information I want, this is the only option.
2. "The Code of the WINSTATIONQUERYINFORMATIONW structure is not public"-this is the toughest problem. After looking at its definition in the Winternl.h file of the Platform SDK, I was quite disappointed:


Obviously, this is not enough ... I managed to find the C + + code for this part of the structure, and switch to C # as defined below:


3. "The information on MSDN says that we can only use this function on our local machine"--but there are some clues ... This function is documented with the WTS API, and you can get a Terminal Services (TS) handle (handle) through a function: Wtsopenserver. So what I'm going to do next is take the Wtsopenserver () function to get the handle to the remote Terminal Service (handle) and use the WINSTATIONQUERYINFORMATIONW () function to get the information I want.

make a general statement of the above :

Required structure (structures)/enum type (enums):


DLL file import (dllimports):


We will get easy-to-understand information:


To convert a hard-filetime type to a datetime type:


Useful code:


Simply call Wtsquerysessioninfo ("servername", <session id>) on the line.

Summary:

As you can see, after all the steps have been completed, we still don't get the "session idle time" we want. In fact, it can be easily calculated by following the simple rules:

※ If the session is disconnected (disconnected) state, idle time = current time-disconnect time (idle times = currenttime-disconnecttime)
※ If the session is active (alive) state, idle time = current time-Last input time (idle times = currenttime-lastinputtime)

Use C # to get idle time for Terminal Services (Terminal services) sessions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.