예제 #1
0
        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }



            int    cb    = 0;
            int    ret   = GetAdaptersInfo(IntPtr.Zero, ref cb);
            IntPtr pInfo = LocalAlloc(0x40, cb); //LPTR

            ret = GetAdaptersInfo(pInfo, ref cb);
            if (ret == 0)
            {
                IP_ADAPTER_INFO info = new IP_ADAPTER_INFO(pInfo, 0);
                while (info != null)
                {
                    IP_ADDR_STRING st = info.IpAddressList;
                    //{ info.AdapterName, info.CurrentIpAddress.IpAddress.String, info.CurrentIpAddress.IpMask.String, info.GatewayList.IpAddress.String }));
                    classLogin.ReaderIP = info.CurrentIpAddress.IpAddress.String;
                    info = info.Next;
                }
            }
            LocalFree(pInfo);

            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }
        }
예제 #2
0
            public static int GetAdapterInfo(string adapter, out IP_ADAPTER_INFO info)
            {
                long   structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
                IntPtr pArray     = Marshal.AllocHGlobal(new IntPtr(structSize));

                try
                {
                    int ret = GetAdaptersInfo(pArray, ref structSize);

                    if (ret == ERROR_BUFFER_OVERFLOW)
                    {
                        pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));
                        ret    = GetAdaptersInfo(pArray, ref structSize);
                    }

                    if (ret != 0)
                    {
                        throw new System.ComponentModel.Win32Exception(ret);
                    }

                    IntPtr pEntry = pArray;

                    while (pEntry != IntPtr.Zero)
                    {
                        info = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

                        if (info.IpAddressList.IpAddress.Address == adapter)
                        {
                            return(info.IpAddressList.Context);
                        }

                        IntPtr pAddr = info.IpAddressList.Next;

                        while (pAddr != IntPtr.Zero)
                        {
                            IP_ADDR_STRING addr = (IP_ADDR_STRING)Marshal.PtrToStructure(pAddr, typeof(IP_ADDR_STRING));
                            if (addr.IpAddress.Address == adapter)
                            {
                                return(addr.Context);
                            }

                            pAddr = addr.Next;
                        }

                        pEntry = info.Next;
                    }

                    throw new InvalidOperationException("Adapter not found");
                }
                finally
                {
                    Marshal.FreeHGlobal(pArray);
                }
            }
예제 #3
0
        } // GetAdapter

        public static IP_ADAPTER_INFO GetAdapterInfo(int numAdapter)
        {
            IP_ADAPTER_INFO entry = new IP_ADAPTER_INFO();

            pEntry = pArray;
            entry  = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));
            for (int x = 0; x < numAdapter; x++)
            {
                pEntry = entry.Next;
            }
            entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));
            return(entry);



            //Marshal.FreeHGlobal(pArray);
        }
예제 #4
0
        private static List <IP_ADAPTER_INFO> GetAdapterList()
        {
            List <IP_ADAPTER_INFO> list = new List <IP_ADAPTER_INFO>();
            long   structSize           = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
            IntPtr pArray = Marshal.AllocHGlobal((int)new IntPtr(structSize));

            int ret = GetAdaptersInfo(pArray, ref structSize);

            if (ret == ERROR_BUFFER_OVERFLOW) // ERROR_BUFFER_OVERFLOW == 111
            {
                // Buffer was too small, reallocate the correct size for the buffer.
                pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));

                ret = GetAdaptersInfo(pArray, ref structSize);
            } // if

            if (ret == 0)
            {
                // Call Succeeded
                IntPtr pEntry = pArray;

                do
                {
                    // Retrieve the adapter info from the memory address
                    IP_ADAPTER_INFO entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

                    list.Add(entry);

                    // Get next adapter (if any)
                    pEntry = entry.Next;
                }while (pEntry != IntPtr.Zero);

                Marshal.FreeHGlobal(pArray);
            } // if
            else
            {
                Marshal.FreeHGlobal(pArray);
                throw new InvalidOperationException("GetAdaptersInfo failed: " + ret);
            }

            return(list);
        }
예제 #5
0
        /// <summary>
        /// 计算网卡MAC地址
        /// </summary>
        /// <returns></returns>
        private static StringCollection GetMacAddressStrs()
        {
            StringCollection V_0 = new StringCollection();

            long V_1 = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));

            IntPtr V_2 = Marshal.AllocHGlobal(new IntPtr(V_1));

            int V_3 = GetAdaptersInfo(V_2, ref V_1);

            if (V_3.Equals(0x6f))
            {
                V_2 = Marshal.ReAllocHGlobal(V_2, new IntPtr(V_1));

                V_3 = GetAdaptersInfo(V_2, ref V_1);
            }

            if (V_3.Equals(0))
            {
                IntPtr V_4 = V_2;
                do
                {
                    IP_ADAPTER_INFO V_5 = (IP_ADAPTER_INFO)Marshal.PtrToStructure(V_4, typeof(IP_ADAPTER_INFO));
                    string          V_6 = string.Empty;
                    for (int i = 0; i < V_5.AddressLength - 1; i++)
                    {
                        V_6 += string.Format("{0:X2}", V_5.Address[i]);
                    }
                    V_0.Add(string.Format("{0}{1:X2}", V_6, V_5.Address[V_5.AddressLength - 1]));
                    V_4 = V_5.Next;
                } while (V_4 != IntPtr.Zero);
            }

            Marshal.FreeHGlobal(V_2);

            return(V_0);
        }
예제 #6
0
        /// <summary>
        /// Clears and reconstructs the list of adapters,
        /// based on the current hardware connections to the
        /// device.
        /// </summary>
        unsafe void Refresh()
        {
            // Clear the existing list, if any.
            this.Clear();

            // Get the list of adapters, in the form of an
            // IP_ADAPTER_INFO object.
            IP_ADAPTER_INFO ipinfo = new IP_ADAPTER_INFO();

            // For each adapter index, get the adapter
            // information.  This is done in the fixed
            // context, as the base address is saved when
            // you do this.
            Adapter adap = ipinfo.FirstAdapter();

            while (adap != null)
            {
                // Add the new item to our list.
                this.List.Add(adap);

                // Get the next adapter information.
                adap = ipinfo.NextAdapter();
            }
        }
예제 #7
0
        private int GetAdaptersInfoDetour(IntPtr AdaptersInfo, IntPtr OutputBuffLen)
        {
            var result = GetAdaptersInfo(AdaptersInfo, OutputBuffLen);

            if (AdaptersInfo != IntPtr.Zero)
            {
                IP_ADAPTER_INFO structure = (IP_ADAPTER_INFO)Marshal.PtrToStructure(AdaptersInfo, typeof(IP_ADAPTER_INFO));
                structure.AdapterName = _guid;
                for (int i = 0; i < structure.Address.Length - 1; i = i + 2)
                {
                    var tekst = structure.Address[i].ToString("X2", System.Globalization.CultureInfo.InvariantCulture);
                    if (tekst == "00")
                    {
                        tekst = "0";
                    }

                    structure.Address[i] = Convert.ToByte(_mac.Replace("-", "")[i].ToString() + _mac.Replace("-", "")[i + 1].ToString(), 16);
                    structure.Next       = IntPtr.Zero;
                }
                structure.IpAddressList.IpAddress.Address = _address;
                Marshal.StructureToPtr(structure, AdaptersInfo, true);
            }
            return(result);
        }
예제 #8
0
        public bool IsAssociated(char[] pszSSID, uint dwLen)
        {
            bool fStatus = true;

            if (0 == m_Intf.bInitialized)
            {
                return(false);
            }
            if (0 == (m_dwOutFlags & NativeConstants.INTF_BSSID))
            {
                return(false);
            }
            IntPtr pAdapterInfo = IntPtr.Zero;

            try
            {
                pAdapterInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IP_ADAPTER_INFO)) * 10);
                //IP_ADAPTER_INFO[] adapterInfo = new IP_ADAPTER_INFO[10];
                uint uLen = (uint)Marshal.SizeOf(typeof(IP_ADAPTER_INFO)) * 10;
                uint ret  = NativeMethods.GetAdaptersInfo(pAdapterInfo, ref uLen);
                for (int i = 0; i < uLen / Marshal.SizeOf(typeof(IP_ADAPTER_INFO)); i++)
                {
                    IP_ADAPTER_INFO adapterInfo   = (IP_ADAPTER_INFO)Marshal.PtrToStructure(new IntPtr(pAdapterInfo.ToInt32() + i * Marshal.SizeOf(typeof(IP_ADAPTER_INFO))), typeof(IP_ADAPTER_INFO));
                    bool            isThisAdapter = true;
                    for (int j = 0; adapterInfo.AdapterName[j] != 0 && m_szWirelessCard1[j] != '\0'; j++)
                    {
                        if ((char)adapterInfo.AdapterName[j] != m_szWirelessCard1[j])
                        {
                            isThisAdapter = false;
                            break;
                        }
                    }
                    if (isThisAdapter)
                    {
                        IP_ADDR_STRING CurrentIPAddress = (IP_ADDR_STRING)Marshal.PtrToStructure(new IntPtr(adapterInfo.CurrentIpAddress.ToInt32()), typeof(IP_ADDR_STRING));
                        char[]         zero             = "0.0.0.0".ToCharArray();
                        bool           iszero           = true;
                        for (int j = 0; j < zero.Length; j++)
                        {
                            if ((char)CurrentIPAddress.IpAddress.Data4[j] != zero[j])
                            {
                                iszero = false;
                                break;
                            }
                        }
                        if (iszero)
                        {
                            return(false);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                if (0 == m_Intf.rdSSID.dwDataLen)
                {
                    return(false);
                }
                if (dwLen < NativeConstants.NDIS_802_11_LENGTH_SSID + 1)
                {
                    return(false);
                }
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[]        ssidData = new byte[m_Intf.rdSSID.dwDataLen + 1];
                for (int i = 0; i < m_Intf.rdSSID.dwDataLen; i++)
                {
                    pszSSID[i] = (char)(ssidData[i] = (byte)Marshal.PtrToStructure(new IntPtr(m_Intf.rdSSID.pData.ToInt32() + i), typeof(byte)));
                }
                pszSSID[m_Intf.rdSSID.dwDataLen] = '\0';
            }
            catch
            {
                MessageBox.Show("IsAssociated exception");
                fStatus = false;
            }
            finally
            {
                if (pAdapterInfo != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pAdapterInfo);
                }
            }
            return(fStatus);
        }
예제 #9
0
        public static void GetAdapters()
        {
            //Int64 structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
            //IntPtr pArray = Marshal.AllocHGlobal(structSize);

            Int64 structSize = 0;
            int   ret        = GetAdaptersInfo(IntPtr.Zero, ref structSize);

            //if (ret == ERROR_BUFFER_OVERFLOW) // ERROR_BUFFER_OVERFLOW == 111
            //{
            //    // Buffer was too small, reallocate the correct size for the buffer.
            //    pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));

            //    ret = GetAdaptersInfo(pArray, ref structSize);
            //} // if

            IntPtr pArray        = Marshal.AllocHGlobal((int)structSize);
            var    ipAdapterInfo = new IP_ADAPTER_INFO
            {
                AdapterDescription = string.Empty,
                Address            = new byte[MAX_ADAPTER_ADDRESS_LENGTH]
            };


            Marshal.StructureToPtr(ipAdapterInfo, pArray, true);
            ret = GetAdaptersInfo(pArray, ref structSize);


            if (ret == 0)
            {
                // Call Succeeded
                IntPtr pEntry = pArray;

                do
                {
                    // Retrieve the adapter info from the memory address
                    var entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

                    // ***Do something with the data HERE!***
                    Console.WriteLine("\n");
                    Console.WriteLine("Index: {0}", entry.Index.ToString());

                    // Adapter Type
                    string tmpString = string.Empty;
                    switch (entry.Type)
                    {
                    case MIB_IF_TYPE_ETHERNET: tmpString = "Ethernet"; break;

                    case MIB_IF_TYPE_TOKENRING: tmpString = "Token Ring"; break;

                    case MIB_IF_TYPE_FDDI: tmpString = "FDDI"; break;

                    case MIB_IF_TYPE_PPP: tmpString = "PPP"; break;

                    case MIB_IF_TYPE_LOOPBACK: tmpString = "Loopback"; break;

                    case MIB_IF_TYPE_SLIP: tmpString = "Slip"; break;

                    default: tmpString = "Other/Unknown"; break;
                    } // switch
                    Console.WriteLine("Adapter Type: {0}", tmpString);

                    Console.WriteLine("Name: {0}", entry.AdapterName);
                    Console.WriteLine("Desc: {0}\n", entry.AdapterDescription);

                    Console.WriteLine("DHCP Enabled: {0}", (entry.DhcpEnabled == 1) ? "Yes" : "No");

                    if (entry.DhcpEnabled == 1)
                    {
                        //Console.WriteLine("DHCP Server : {0}", entry.DhcpServer.IpAddress.Address);

                        // Lease Obtained (convert from "time_t" to C# DateTime)
                        //DateTime pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseObtained).ToLocalTime();
                        //Console.WriteLine("Lease Obtained: {0}", pdatDate.ToString());

                        // Lease Expires (convert from "time_t" to C# DateTime)
                        //pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseExpires).ToLocalTime();
                        //Console.WriteLine("Lease Expires : {0}\n", pdatDate.ToString());
                    } // if DhcpEnabled

                    //Console.WriteLine("IP Address     : {0}", entry.IpAddressList.IpAddress.Address);
                    //Console.WriteLine("Subnet Mask    : {0}", entry.IpAddressList.IpMask.Address);
                    //Console.WriteLine("Default Gateway: {0}", entry.GatewayList.IpAddress.Address);

                    // MAC Address (data is in a byte[])
                    //tmpString = string.Empty;
                    //for (int i = 0; i < entry.AddressLength - 1; i++)
                    //{
                    //    tmpString += string.Format("{0:X2}-", entry.Address[i]);
                    //}
                    //Console.WriteLine("MAC Address    : {0}{1:X2}\n", tmpString, entry.Address[entry.AddressLength - 1]);

                    //Console.WriteLine("Has WINS: {0}", entry.HaveWins ? "Yes" : "No");
                    if (entry.HaveWins)
                    {
                        //Console.WriteLine("Primary WINS Server  : {0}", entry.PrimaryWinsServer.IpAddress.Address);
                        //Console.WriteLine("Secondary WINS Server: {0}", entry.SecondaryWinsServer.IpAddress.Address);
                    } // HaveWins

                    // Get next adapter (if any)
                    pEntry = entry.Next;
                }while (pEntry != IntPtr.Zero);

                Marshal.FreeHGlobal(pArray);
            } // if
            else
            {
                Marshal.FreeHGlobal(pArray);
                throw new InvalidOperationException("GetAdaptersInfo failed: " + ret);
            }
        } // GetAdapters
예제 #10
0
        public static void GetAdapters(out string IP, out string MAC)
        {
            StringBuilder s = new StringBuilder();

            long   structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
            IntPtr pArray     = Marshal.AllocHGlobal(new IntPtr(structSize));

            int ret = WinAPIDeclarations.GetAdaptersInfo(pArray, ref structSize);

            if (ret == (int)AdaptersConsts.ERROR_BUFFER_OVERFLOW)  // ERROR_BUFFER_OVERFLOW == 111
            {
                // Buffer was too small, reallocate the correct size for the buffer.
                pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));

                ret = WinAPIDeclarations.GetAdaptersInfo(pArray, ref structSize);
            }

            string ip = string.Empty, mac = string.Empty;

            if (ret == 0)
            {
                // Call Succeeded
                IntPtr pEntry = pArray;

                do
                {
                    // Retrieve the adapter info from the memory address
                    IP_ADAPTER_INFO entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

                    // ***Do something with the data HERE!***
                    //Console.WriteLine("\n");
                    //Console.WriteLine("Index: {0}", entry.Index.ToString());

                    // Adapter Type
                    string tmpString = string.Empty;
                    switch (entry.Type)
                    {
                    case (uint)AdaptersConsts.MIB_IF_TYPE_ETHERNET:
                        tmpString = "Ethernet";
                        break;

                    case (uint)AdaptersConsts.MIB_IF_TYPE_TOKENRING:
                        tmpString = "Token Ring";
                        break;

                    case (uint)AdaptersConsts.MIB_IF_TYPE_FDDI:
                        tmpString = "FDDI";
                        break;

                    case (uint)AdaptersConsts.MIB_IF_TYPE_PPP:
                        tmpString = "PPP";
                        break;

                    case (uint)AdaptersConsts.MIB_IF_TYPE_LOOPBACK:
                        tmpString = "Loopback";
                        break;

                    case (uint)AdaptersConsts.MIB_IF_TYPE_SLIP:
                        tmpString = "Slip";
                        break;

                    default:
                        tmpString = "Other/Unknown";
                        break;
                    } // switch
                      //Console.WriteLine("Adapter Type: {0}", tmpString);

                    //Console.WriteLine("Name: {0}", entry.AdapterName);
                    //Console.WriteLine("Desc: {0}\n", entry.AdapterDescription);

                    //Console.WriteLine("DHCP Enabled: {0}", (entry.DhcpEnabled == 1) ? "Yes" : "No");

                    if (entry.DhcpEnabled == 1)
                    {
                        //Console.WriteLine("DHCP Server : {0}", entry.DhcpServer.IpAddress.Address);

                        // Lease Obtained (convert from "time_t" to C# DateTime)
                        DateTime pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseObtained).ToLocalTime();
                        //Console.WriteLine("Lease Obtained: {0}", pdatDate.ToString());

                        // Lease Expires (convert from "time_t" to C# DateTime)
                        pdatDate = new DateTime(1970, 1, 1).AddSeconds(entry.LeaseExpires).ToLocalTime();
                        //Console.WriteLine("Lease Expires : {0}\n", pdatDate.ToString());
                    } // if DhcpEnabled

                    //Console.WriteLine("IP Address     : {0}", entry.IpAddressList.IpAddress.Address);
                    //Console.WriteLine("Subnet Mask    : {0}", entry.IpAddressList.IpMask.Address);
                    //Console.WriteLine("Default Gateway: {0}", entry.GatewayList.IpAddress.Address);

                    ip = entry.IpAddressList.IpAddress.Address;
                    if (ip != "0.0.0.0")
                    {
                        //s.Append(ip + " ");
                        tmpString = string.Empty;
                        for (int i = 0; i < entry.AddressLength - 1; i++)
                        {
                            tmpString += string.Format("{0:X2}-", entry.Address[i]);
                        }
                        //s.Append(string.Format("{0}{1:X2}", tmpString, entry.Address[entry.AddressLength - 1]));
                        mac = string.Format("{0}{1:X2}", tmpString, entry.Address[entry.AddressLength - 1]);
                        break;
                    }

                    // MAC Address (data is in a byte[])
                    //tmpString = string.Empty;
                    //for (int i = 0; i < entry.AddressLength - 1; i++)
                    //{
                    //    tmpString += string.Format("{0:X2}-", entry.Address[i]);
                    //}
                    //Console.WriteLine("MAC Address    : {0}{1:X2}\n", tmpString, entry.Address[entry.AddressLength - 1]);

                    //Console.WriteLine("Has WINS: {0}", entry.HaveWins ? "Yes" : "No");
                    //if (entry.HaveWins)
                    //{
                    //    Console.WriteLine("Primary WINS Server  : {0}", entry.PrimaryWinsServer.IpAddress.Address);
                    //    Console.WriteLine("Secondary WINS Server: {0}", entry.SecondaryWinsServer.IpAddress.Address);
                    //} // HaveWins

                    // Get next adapter (if any)
                    pEntry = entry.Next;
                } while (pEntry != IntPtr.Zero || (ip == string.Empty && mac == string.Empty));

                Marshal.FreeHGlobal(pArray);
            }
            else
            {
                Marshal.FreeHGlobal(pArray);
                throw new InvalidOperationException("GetAdaptersInfo failed: " + ret);
            }

            IP  = ip;
            MAC = mac;
        }