예제 #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
        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);
        }