preferred方式获得客户的名字从终端服务器会话

preferred方式获得客户的名字从终端服务器会话

本文介绍了preferred方式获得客户的名字从终端服务器会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从用户的终端服务器会话获取潜在的客户端PC名称。

I need to get the underlying client PC name from a user's terminal server session.

我知道它生活在 HKEY_CURRENT_USER \ Volatile环境\ CLIENTNAME ,但有另一种(preferably本机.NET)得到它的方法是什么?

I know it lives in HKEY_CURRENT_USER\Volatile Environment\CLIENTNAME but is there another (preferably native .net) method of getting it?

推荐答案

我没有看到这样的托管API。唯一的基于API的方式,我可以看到,在得到这些信息将通过WMI或Windows中的本地终端服务API。

I didn't see a managed API for this. The only API-based ways I could see for getting at this information would be through WMI or the native Terminal Services API in Windows.

下面是返回使用 WTSQuerySessionInformation API客户端名称的例子:

Here is an example that returns the client name using the WTSQuerySessionInformation API:

namespace com.stackoverflow
{
    using System;
    using System.Runtime.InteropServices;

    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetTerminalServicesClientName());
        }

        /// <summary>
        /// Gets the name of the client system.
        /// </summary>
        internal static string GetTerminalServicesClientName()
        {
            IntPtr buffer = IntPtr.Zero;

            string clientName = null;
            int bytesReturned;

            bool success = NativeMethods.WTSQuerySessionInformation(
                NativeMethods.WTS_CURRENT_SERVER_HANDLE,
                NativeMethods.WTS_CURRENT_SESSION,
                NativeMethods.WTS_INFO_CLASS.WTSClientName,
                out buffer,
                out bytesReturned);

            if (success)
            {
                clientName = Marshal.PtrToStringUni(
                    buffer,
                    bytesReturned / 2 /* Because the DllImport uses CharSet.Unicode */
                    );
                NativeMethods.WTSFreeMemory(buffer);
            }

            return clientName;
        }
    }

    public static class NativeMethods
    {
        public static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
        public const int WTS_CURRENT_SESSION = -1;

        public enum WTS_INFO_CLASS
        {
            WTSClientName = 10
        }

        [DllImport("Wtsapi32.dll", CharSet = CharSet.Unicode)]
        public static extern bool WTSQuerySessionInformation(
            IntPtr hServer,
            Int32 sessionId,
            WTS_INFO_CLASS wtsInfoClass,
            out IntPtr ppBuffer,
            out Int32 pBytesReturned);

        /// <summary>
        /// The WTSFreeMemory function frees memory allocated by a Terminal
        /// Services function.
        /// </summary>
        /// <param name="memory">Pointer to the memory to free.</param>
        [DllImport("wtsapi32.dll", ExactSpelling = true, SetLastError = false)]
        public static extern void WTSFreeMemory(IntPtr memory);
    }
}

这篇关于preferred方式获得客户的名字从终端服务器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:03