Включение WiFI
От: MerlinK  
Дата: 09.05.08 14:17
Оценка:
Подскажите как программно включить WIFI в КПК из dotNET )
уже все перепробовал (
Re: Включение WiFI
От: Serginio1 СССР https://habrahabr.ru/users/serginio1/topics/
Дата: 16.05.08 14:09
Оценка:
Здравствуйте, MerlinK, Вы писали:

MK>Подскажите как программно включить WIFI в КПК из dotNET )

MK>уже все перепробовал (
Зависит от конкретной модели, иногда API не поддерживает включение отключение, но можно запустить приложение которое включает отключает WiFi и через собщение нажать нужную галку.
Например

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using OpenNETCF.Net;
using Microsoft.Win32;
using OpenNETCF.Win32;

using System.Threading;

namespace Revisia
{
   public class WiFiAndWin32
    {

        public class ProcessInfo
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public Int32 ProcessId;
            public Int32 ThreadId;
        }

        [DllImport("CoreDll.DLL", SetLastError = true)]
        private extern static
            int CreateProcess(String imageName,
            String cmdLine,
            IntPtr lpProcessAttributes,
            IntPtr lpThreadAttributes,
            Int32 boolInheritHandles,
            Int32 dwCreationFlags,
            IntPtr lpEnvironment,
            IntPtr lpszCurrentDir,
            byte[] si,
            ProcessInfo pi);

        [DllImport("CoreDll.dll")]
        private extern static
            Int32 GetLastError();

        [DllImport("CoreDll.dll")]
        private extern static
            Int32 WaitForSingleObject(IntPtr Handle,
            Int32 Wait);


        public static bool CreateProcess(String ExeName, String CmdLine, ProcessInfo pi)
        {
            Int32 INFINITE;
            unchecked { INFINITE = (int)0xFFFFFFFF; }

            if (pi == null)
                pi = new ProcessInfo();
            byte[] si = new byte[128];
            CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,
                0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);
            WaitForSingleObject(pi.hProcess, INFINITE);
            return true;
        }

        public static string WiFiAdapterName()
        {

            AdapterCollection adapterCollection = Networking.GetAdapters();
            foreach (Adapter adapter in adapterCollection)
            {
                if (adapter.IsWireless)
                    return adapter.Name;


            }
            return "";
        }

        public static Adapter WiFiAdapter()
        {

            AdapterCollection adapterCollection = Networking.GetAdapters();
            foreach (Adapter adapter in adapterCollection)
            {
                if (adapter.IsWireless)
                    return adapter;


            }
            return null;
        }




        public static void emulPress(IntPtr hEdit)
        {

            int t1 = 13; // Scan-код символа "9"
            int nul = 0;
            int intNul = 0;
            IntPtr retValue;

  
            Win32Window.SendMessage(hEdit, (int)WM.LBUTTONDOWN, (int)WM.MBUTTONDOWN, nul);
            Win32Window.SendMessage(hEdit, (int)WM.LBUTTONUP, (int)WM.MBUTTONUP, nul);

        }

        public static bool isChecked(IntPtr hwndCheck)
        {
            int BM_GETCHECK = 240;
            int BST_CHECKED = 1;
            int BST_UNCHECKED = 0;
            IntPtr res = Win32Window.SendMessage(hwndCheck, BM_GETCHECK, 0, 0);
            // Если галочка стоит.
            if ((int)res == BST_CHECKED)
            {
                return true;
            }
            // Если галочка не стоит.
            if ((int)res == BST_UNCHECKED)
            {
                return false;
            }
            return false;
        }
        public static void SetWLANState(bool state)
        {
            //SetWifi(true);

            //  WiFiAdapter().


            String ExeName = "ctlpnl.exe";
            String CmdLine = "\\Windows\\WLanPower.cpl,0";
            ProcessInfo pi = new ProcessInfo();

            //  CreateProcess(ExeName,CmdLine , pi); 
            Int32 INFINITE;
            unchecked { INFINITE = (int)0xFFFFFFFF; }

            if (pi == null)
                pi = new ProcessInfo();
            byte[] si = new byte[128];
            CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,
                0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);

   //         Thread.Sleep(1000);


            IntPtr hWnd = Win32Window.FindWindow(null, "Set WLAN Power");
            while (hWnd == IntPtr.Zero)
            {
                Thread.Sleep(100);
                hWnd = Win32Window.FindWindow(null, "Set WLAN Power");
            }

            IntPtr gg;
            IntPtr OKHandle = IntPtr.Zero;
            IntPtr WLHandle = IntPtr.Zero;
            IntPtr CancelHandle = IntPtr.Zero;
            //  MessageBox.Show(Win32Window.GetWindowText(hEdit));
            gg = Win32Window.GetWindow(hWnd, GW.CHILD).Handle;
            while (gg != IntPtr.Zero)
            {
                if (Win32Window.GetWindowText(gg) == "OK")
                    OKHandle = gg;

                if (Win32Window.GetWindowText(gg) == "WirelessLAN ON")
                    WLHandle = gg;

                if (Win32Window.GetWindowText(gg) == "Cancel")
                    CancelHandle = gg;
                //     MessageBox.Show(Win32Window.GetWindowText(gg));
                //   MessageBox.Show(gg.Text);
                gg = Win32Window.GetWindow(gg, GW.HWNDNEXT).Handle;
                // gg = Win32Window.GetWindow(gg, GW.CHILD).Handle;
            }

//            MessageBox.Show(isChecked(WLHandle).ToString());
            if (isChecked(WLHandle)!=state)
            {
            emulPress(WLHandle);
            emulPress(OKHandle);
                
            }
            else
                emulPress(CancelHandle);    

            WaitForSingleObject(pi.hProcess, INFINITE);
           


        }

        public static string getWLANStatus()
        {
            string result = "Нет сигнала";

            Adapter ad = WiFiAdapter();
            if (ad == null)
            {
                return "Не подключен WiFi";
            }
           AccessPointCollection APCollection = ad.NearbyAccessPoints;

            if (APCollection.Count == 0)
                return "Нет точек подключения";

            int SignaldB = -10000;
            foreach (AccessPoint AP in APCollection)
            {
                //  info += indent + "Assoc.AP: " + AP.Name + "\n";
                //indent = "     ";
                //info += indent + "Mode: " + AP.InfrastructureMode.ToString() + "\n";
                //info += indent + "Rates: " + getRates(AP.SupportedRates) + "\n";
                //info += indent + "Mac: " + ToHexString(AP.MacAddress, 6) + "\n";
                //                    info+=indent+"RS: "+AP.SignalStrengthInDecibels+"\n";  // Not yet in class
                SignalStrength signal = AP.SignalStrength;
                if (signal.Decibels > SignaldB)
                {
                    SignaldB = signal.Decibels;
                    int delta = SignaldB + 90;
                    int proc =  delta*2;
                    result = string.Format("Сигнал:{0}  {1}dB {2}%",AP.SignalStrength,SignaldB,proc) ;

                }
            }
            return result;


        }

[DllImport("coredll.dll", EntryPoint="SHCreateShortcut")]
public static extern bool SHCreateShortcut(string szShortcut, string szTarget);



        const int BM_SETSTATE = 243;
        const int WM_LBUTTONDOWN = 513;
        const int WM_LBUTTONUP = 514;
        const int WM_KEYDOWN = 256;
        const int WM_CHAR = 258;
        const int WM_KEYUP = 257;
        const int WM_SETFOCUS = 7;
        const int WM_SYSCOMMAND = 274;
        const int SC_MINIMIZE = 32;
        
        

    }
}
и солнце б утром не вставало, когда бы не было меня
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.