none
Obter o MacAddress das maquinas conectadas remotamente no Terminal Server 2003 RRS feed

  • Pergunta

  • Boa tarde a todos.

    Estou montando uma rotina para obter os dados das maquinas conectadas ao TS2003.

    Consigo obter os dados utilizando a seguinte rotina

    [DllImport("iphlpapi.dll", ExactSpelling = true)]
            static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen); 
                   [DllImport("Wtsapi32.dll")]   
            private static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId,         
           WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
          
            /// <summary>
            /// Remote Desktop Services API Enumeration Types
            /// </summary>       
            enum WTS_INFO_CLASS
            {
                WTSInitialProgram,
                WTSApplicationName,
                WTSWorkingDirectory,
                WTSOEMId,
                WTSSessionId,
                WTSUserName,
                WTSWinStationName,
                WTSDomainName,
                WTSConnectState,
                WTSClientBuildNumber,
                WTSClientName,
                WTSClientDirectory,
                WTSClientProductId,
                WTSClientHardwareId,
                WTSClientAddress,
                WTSClientDisplay,
                WTSClientProtocolType
            }
          
            public string ClientMACAddress()
            {
                try
                {   //gets name of user that is logged in                       
                    //WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                    //MessageBox.Show(wp.Identity.Name); // Username
    
                    System.Net.IPHostEntry host = new System.Net.IPHostEntry();
                    host = System.Net.Dns.GetHostEntry(GetTerminalServerClientNameWTSAPI());
                    IPAddress HostAddress=null;
    
                    //search for IPV4 ip addresses
                    foreach (IPAddress ip in host.AddressList)
                    {
                        if (ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            HostAddress=ip;                     
                        }
                    }
                        return GetMacAddress(HostAddress).ToString();                             
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }
    
           /// <summary>
          /// Gets the MAC address (<see cref="PhysicalAddress"/>) associated with the specified IP.
            /// </summary>
            /// <param name="ipAddress">The remote IP address.</param>
            /// <returns>The remote machine's MAC address.</returns>
            private static PhysicalAddress GetMacAddress(IPAddress ipAddress)
            {
                const int MacAddressLength = 6;
                int length = MacAddressLength;
                var macBytes = new byte[MacAddressLength];
                SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
                return new PhysicalAddress(macBytes);
            }
    
            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;
            }
    

    Funciona quando a maquina conectada está na mesma rede do TS, porém com acesso remoto (fora da rede) ele não obtém o erro.

    Alguém poderia me ajudar?

    sexta-feira, 29 de maio de 2015 17:57

Respostas

Todas as Respostas