Example #1
0
        public unsafe static bool IoControl(uint code, byte[] indata, byte[] outdata)
        {
            NDISUIO ndis   = new NDISUIO();
            bool    retVal = false;

            try
            {
                try
                {
                    ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                }
                catch
                {
                    throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }

                try
                {
                    ndis.DeviceIoControl(code, indata, outdata);
                }
                catch (Exception)
                {
                    retVal = false;
                }
            }
            finally
            {
                ndis.Dispose();
            }

            return(retVal);
        }
Example #2
0
        public unsafe static bool SetOID(NDIS_OID oid, string adapterName, NDISQueryOid queryOID)
        {
            NDISUIO ndis = new NDISUIO();

            if (queryOID == null)
            {
                queryOID = new NDISQueryOid(0);
            }

            byte[] nameBytes = System.Text.Encoding.Unicode.GetBytes(adapterName + '\0');
            fixed(byte *pName = &nameBytes[0])
            {
                queryOID.ptcDeviceName = pName;

                queryOID.Oid = (uint)oid;

                try
                {
                    ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                }
                catch
                {
                    throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }
                try
                {
                    ndis.DeviceIoControl(IOCTL_NDISUIO_SET_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            ndis.Dispose();

            return(true);
        }
Example #3
0
        public unsafe static byte[] QueryOID(NDIS_OID oid, string adapterName)
        {
            if ((adapterName == null) || (adapterName == string.Empty))
            {
                // no point in even trying to call NDIS if this is the case
                return(null);
            }

            NDISUIO ndis = new NDISUIO();

            NDISQueryOid queryOID;

            byte[] result;

            try
            {
                switch (oid)
                {
                case NDIS_OID.RSSI:
                case NDIS_OID.WEP_STATUS:
                case NDIS_OID.BSSID_LIST_SCAN:
                case NDIS_OID.AUTHENTICATION_MODE:
                case NDIS_OID.INFRASTRUCTURE_MODE:
                case NDIS_OID.NETWORK_TYPE_IN_USE:
                    queryOID = new NDISQueryOid(4);         // The data is a four-byte signed int
                    break;

                case NDIS_OID.BSSID:
                    queryOID = new NDISQueryOid(36);
                    break;

                case NDIS_OID.SUPPORTED_RATES:
                    queryOID = new NDISQueryOid(8);
                    break;

                case NDIS_OID.CONFIGURATION:
                    queryOID = new NDISQueryOid(32);
                    break;

                case NDIS_OID.SSID:
                    queryOID = new NDISQueryOid(36);            // The data is a four-byte length plus 32-byte ASCII string
                    break;

                case NDIS_OID.BSSID_LIST:
                    queryOID = new NDISQueryOid(2000);
                    break;

                default:
                    throw new NotSupportedException(string.Format("'NDIS_OID_{0}' Not supported", oid.ToString()));
                }

                byte[] nameBytes = System.Text.Encoding.Unicode.GetBytes(adapterName + '\0');

                fixed(byte *pName = &nameBytes[0])
                {
                    queryOID.ptcDeviceName = pName;
                    queryOID.Oid           = (uint)oid;

                    try
                    {
                        ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                    }
                    catch
                    {
                        throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                    }
                    try
                    {
                        ndis.DeviceIoControl(IOCTL_NDISUIO_QUERY_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                    }
                    catch (Exception)
                    {
                        // if it's an attempt to get a list it might be an OOM
                        // are we trying to get a list?
                        if (oid != NDIS_OID.BSSID_LIST)
                        {
                            throw new NetworkInformationException();
                        }

                        // maybe just an OOM - try again
                        GC.Collect();
                        queryOID = new NDISQueryOid(8000);
                        queryOID.ptcDeviceName = pName;
                        queryOID.Oid           = (uint)oid;
                        ndis.DeviceIoControl(IOCTL_NDISUIO_QUERY_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                    }
                    finally
                    {
                        ndis.Close();
                    }

                    result = new byte[queryOID.Data.Length];
                    Buffer.BlockCopy(queryOID.Data, 0, result, 0, result.Length);
                }
            }
            finally
            {
                ndis.Dispose();
            }
            return(result);
        }