2

I have multiple VM's that can be accessed remotely. Is there a way to determine which user is currently connected to each of those VM's? I already used WTSQuerySessionInformation to get the current user which is remotely connected, but if I use that I have to run that programm on all VM's. I would like to know if there is a way to use WTSQuerySessionInformation with an ip or something like that, so that I can get the current remote user of each VM without running a programm on each VM.

That is the Code I currently use to get a connected user.

private static string GetTerminalServerClientNameWTSAPI()
    {

        const int WTS_CURRENT_SERVER_HANDLE = -1;

        IntPtr buffer = IntPtr.Zero;
        uint bytesReturned;

        string strReturnValue = "";
        try
        {
            WTSQuerySessionInformation(IntPtr.Zero, WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSClientName, out buffer, out bytesReturned);
            strReturnValue = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(buffer);
        }

        finally
        {
            buffer = IntPtr.Zero;
        }

        return strReturnValue;
    }

    enum WTS_INFO_CLASS
    {
        WTSInitialProgram,
        WTSApplicationName,
        WTSWorkingDirectory,
        WTSOEMId,
        WTSSessionId,
        WTSUserName,
        WTSWinStationName,
        WTSDomainName,
        WTSConnectState,
        WTSClientBuildNumber,
        WTSClientName,
        WTSClientDirectory,
        WTSClientProductId,
        WTSClientHardwareId,
        WTSClientAddress,
        WTSClientDisplay,
        WTSClientProtocolType

    }

    [System.Runtime.InteropServices.DllImport("Wtsapi32.dll")]
    private static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);

Thank you for reading, I hope someone understands what I am talking about. :D

AeroBiba
  • 21
  • 2

0 Answers0