0

I am running a remote application on w2008r2 and would like to retrieve the virtual ip it is running from. Already tried cassia, but it gives me the client ip, not the ip handed out by the dhcp server. Looking up the address through dns lookup up doesn't work either; it returns the servers IP address instead of the virtual IP address

I have setup Remote Desktop IP Virtualization per program as described here

Ace Indy
  • 109
  • 1
  • 4
  • This article, [Configuring Remote Desktop IP Virtualization: Part 1](https://blogs.technet.microsoft.com/enterprisemobility/2009/07/10/configuring-remote-desktop-ip-virtualization-part-1/) mentions a part 3 of the article (which apparently never surfaced) that would show how to configure ip virtualization using the RDS provider for powershell. Have you checked this, whatever that is (sorry, I have little experience with powershell). – Lasse V. Karlsen Jun 30 '17 at 08:13
  • If you've set it up per-program, are you checking which ip the program is using *from within the program*? I'm pretty sure ip virtualization is transparent, in that a program running with a virtual ip won't see other ip's and a program not running under it won't see which virtual ip's other programs might have. – Lasse V. Karlsen Jun 30 '17 at 08:14
  • Yes, I need the program to check it's own virtual IP, not for other programs – Ace Indy Jun 30 '17 at 08:28
  • Found it... The answer is to use WTS_INFO_CLASS --> WTSSessionAddressV4 (see answer below) – Ace Indy Jun 30 '17 at 13:20

2 Answers2

1

Seems I get results I needed with WTS_INFO_CLASS --> WTSSessionAddressV4

together with solution from C# Get RDC/RDP and “Console” Session information ( which lacked the full enumeration on WTS_INFO_CLASS)

using System;
using PreEmptive.SoSAttributes;
using Cassia;
using System.Runtime.InteropServices;
using System.Net;

Namespace SomeNameSpace
{
    internal static class SomeProgram
    {
        //Check if we have a virtual IP address
        TerminalSessionInfo SessionInfo = TermServicesManager.GetSessionInfo(Dns.GetHostName(), _manager.CurrentSession.SessionId);

        string LocalIPAddress = "127.0.0.1";
        if (SessionInfo.ClientAddress != null)
        {
                LocalIPAddress = SessionInfo.ClientAddress;
        }
        MessageBox.Show(LocalIPAddress);
    }

    #region Get TerminalServer info
    public class TermServicesManager
    {

        [DllImport("wtsapi32.dll")]
        static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);

        [DllImport("wtsapi32.dll")]
        static extern void WTSCloseServer(IntPtr hServer);

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

        [DllImport("wtsapi32.dll")]
        static extern void WTSFreeMemory(IntPtr pMemory);

        [StructLayout(LayoutKind.Sequential)]
        public struct WTS_SESSION_ADDRESS
        {
            public uint AddressFamily;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public byte[] Address;
        }

        public enum WTS_INFO_CLASS
        {
            InitialProgram = 0,
            ApplicationName = 1,
            WorkingDirectory = 2,
            OEMId = 3,
            SessionId = 4,
            UserName = 5,
            WinStationName = 6,
            DomainName = 7,
            ConnectState = 8,
            ClientBuildNumber = 9,
            ClientName = 10,
            ClientDirectory = 11,
            ClientProductId = 12,
            ClientHardwareId = 13,
            ClientAddress = 14,
            ClientDisplay = 15,
            ClientProtocolType = 16,
            WTSIdleTime = 17,
            WTSLogonTime = 18,
            WTSIncomingBytes = 19,
            WTSOutgoingBytes = 20,
            WTSIncomingFrames = 21,
            WTSOutgoingFrames = 22,
            WTSClientInfo = 23,
            WTSSessionInfo = 24,
            WTSSessionInfoEx = 25,
            WTSConfigInfo = 26,
            WTSValidationInfo = 27,
            WTSSessionAddressV4 = 28,
            WTSIsRemoteSession = 29
        }

        private static IntPtr OpenServer(string Name)
        {
            IntPtr server = WTSOpenServer(Name);
            return server;
        }

        private static void CloseServer(IntPtr ServerHandle)
        {
            WTSCloseServer(ServerHandle);
        }

        public static TerminalSessionInfo GetSessionInfo(string ServerName, int SessionId)
        {
            IntPtr server = IntPtr.Zero;
            server = OpenServer(ServerName);
            System.IntPtr buffer = IntPtr.Zero;
            uint bytesReturned;
            TerminalSessionInfo data = new TerminalSessionInfo();

            try
            {
                bool worked = WTSQuerySessionInformation(server, SessionId,
                    WTS_INFO_CLASS.WTSSessionAddressV4, out buffer, out bytesReturned);

                if (!worked)
                    return data;

                WTS_SESSION_ADDRESS si = (WTS_SESSION_ADDRESS)Marshal.PtrToStructure((System.IntPtr)buffer, typeof(WTS_SESSION_ADDRESS));
                data.ClientAddress = si.Address[2] + "." +si.Address[3] + "." + si.Address[4] + "." + si.Address[5];

            }
            finally
            {
                WTSFreeMemory(buffer);
                buffer = IntPtr.Zero;
                CloseServer(server);
            }

            return data;
        }

    }

    public class TerminalSessionInfo
    {
        public string ClientAddress;
    }
}
#endregion
Ace Indy
  • 109
  • 1
  • 4
0

You mean you want to get the local IP, right? If yes, here is how to do it:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }

    throw new Exception("Local IP Address Not Found!");
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16