locked
Turn radio on RRS feed

  • Question

  • I guys  i have a big problem, i need to develop an application on my windows ce device..

     

    I work with .net c# (csharp)

    My application must turn on the wifi automatically  and  connect to my preferred wifi.. 

    i read some articles i try (opennetcf  but doesnt work on windows ce)

     

    Thanks 

     

    Paolo

     

     


    Chilean guy
    Monday, November 22, 2010 3:59 PM

Answers

  • The Automatic Configuration Service API (ZeroConfig) will do that.

    http://msdn.microsoft.com/en-us/library/ms895312.aspx

    You will need to p/invoke the native functions in your C# application

     

    -PaulH

    ----------

    Edit:

    WZC won't turn the radio on and off. For that, you'll need to use something like the Power Management API.

    http://www.codeproject.com/KB/windows/RadioPower.aspx

     

    • Marked as answer by Bin-ze Zhao Friday, November 26, 2010 6:57 AM
    Monday, November 22, 2010 9:52 PM
  • I implemented such function using P/Invoke in C#.

     

    Please see my code:

    CEDevicePowerState.cs

    namespace MobileAutomation.PhoneCore.NativeStructures
    {
      /// <summary>
      /// Enumeration definition for CEDEVICE_POWER_STATE.
      /// Link: http://msdn.microsoft.com/en-us/library/bb201981.aspx
      /// </summary>
      public enum CEDevicePowerState : int
      {
        /// <summary>System state is unspecified.</summary>
        PwrDeviceUnspecified = -1,
    
        /// <summary>Full On: full power, full functionality.</summary>
        D0 = 0,
    
        /// <summary>Low Power On: fully functional at low power/performance.</summary>
        D1,
    
        /// <summary>Standby: partially powered with automatic wake.</summary>
        D2,
    
        /// <summary>Sleep: partially powered with device initiated wake.</summary>
        D3,
    
        /// <summary>Off: Power Off.</summary>
        D4,
    
        /// <summary>Valid device power state values are greater than PwrDeviceUnspecified and less than PwrDeviceMaximum.</summary>
        PwrDeviceMaximum
      }
    }
    
    

     

    2. P/Invoke for coredll.dll

    namespace MobileAutomation.PhoneCore.NativeMethods
    {
      using System;
      using System.Runtime.InteropServices;
      using System.Text;
      using MobileAutomation.PhoneCore.NativeStructures;
    
      /// <summary>
      /// Helper class for Native Methods in coredll.dll
      /// </summary>
      public static class CoreDll
      {
    
        /// <summary>
        /// Returns the current power state for a device.
        /// Link: http://msdn.microsoft.com/en-us/library/aa447831.aspx
        /// </summary>
        /// <param name="device">[in] PVOID pvDevice: Specifies the device. Must be a valid LPWSTR device name</param>
        /// <param name="deviceFlags">[in] DWORD dwDeviceFlags: Set to powerName to specify the name of the device that is requesting the power request.</param>
        /// <param name="deviceState">[out] PCEDEVICE_POWER_STATE pDeviceState: Returns the device power state in CEDEVICE_POWER_STATE.</param>
        /// <returns>ERROR_SUCCESS, or the other error code</returns>
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern uint GetDevicePower(string device, uint deviceFlags, ref CEDevicePowerState deviceState);
    
        /// <summary>
        /// Sets the device power state for a device.
        /// Link: http://msdn.microsoft.com/en-us/library/aa918190.aspx
        /// </summary>
        /// <param name="device">[in] PVOID pvDevice: Specifies the device. Must be a valid LPWSTR device name</param>
        /// <param name="deviceFlags">[in] DWORD dwDeviceFlags: Set to powerName to specify the name of the device that is requesting the power request.</param>
        /// <param name="deviceState">[in] CEDEVICE_POWER_STATE DeviceState: Specifies the desired device power state.</param>
        /// <returns>ERROR_SUCCESS, or the other error code</returns>
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern uint SetDevicePower(string device, uint deviceFlags, CEDevicePowerState deviceState);
    
      }
    }
    

    3. WifiPowerManagement.cs

    namespace MobileAutomation.PhoneCore.PowerManagement
    {
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using Microsoft.Win32;
      using MobileAutomation.PhoneCore;
      using MobileAutomation.PhoneCore.NativeMethods;
      using MobileAutomation.PhoneCore.NativeStructures;
    
      /// <summary>
      /// Helper class for WIFI Power Management.
      /// </summary>
      public class WifiPowerManagement
      {
        /// <summary>the SUBKEY storing WIFI devices.</summary>
        private const string SubKey = @"\System\CurrentControlSet\Control\Power\State\";
        
        /// <summary>the value keyword of WIFI devices</summary>
        private const string ValueKey = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}";
    
        /// <summary>the Power Name of WIFI device</summary>
        private const int PowerName = 0x00000001;
    
        /// <summary>
        /// Get WIFI power state
        /// </summary>
        /// <returns>CEDevicePowerState value</returns>
        public static CEDevicePowerState GetWifiPowerState()
        {
          CEDevicePowerState powserState = CEDevicePowerState.PwrDeviceUnspecified;
          CoreDll.GetDevicePower(WifiPowerManagement.GetUniqueWifiDeviceName(), PowerName, ref powserState);
    
          return powserState;
        }
    
        /// <summary>
        /// Set WIFI power state
        /// </summary>
        /// <param name="powserState">CEDevicePowerState value</param>
        /// <returns>Error code as WinError</returns>
        public static WinError SetWifiPowerState(CEDevicePowerState powserState)
        {
          uint result = CoreDll.SetDevicePower(WifiPowerManagement.GetUniqueWifiDeviceName(), PowerName, powserState);
          return (WinError)result;
        }
    
        /// <summary>
        /// Get unique WIFI device name in RegistryKey
        /// </summary>
        /// <returns>WIFI device name</returns>
        public static string GetUniqueWifiDeviceName()
        {
          string[] wifiDeviceNames = WifiPowerManagement.GetWifiDeviceNames();
          if (wifiDeviceNames.Length == 1)
          {
            return wifiDeviceNames[0];
          }
          else
          {
            throw new RankException(string.Format("Expected Wifi devices count is 1, Actual: {0}", wifiDeviceNames.Length));
          }
        }
        
        /// <summary>
        /// Search for all the WIFI devices in RegistryKey
        /// </summary>
        /// <returns>Array of the WIFI device name</returns>
        public static string[] GetWifiDeviceNames()
        {
          string[] valueNames = null;
          try
          {
            using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(SubKey))
            {
              valueNames = regKey.GetValueNames();
              regKey.Close();
            }
          }
          catch (Exception e)
          {
            throw new Exception(
              string.Format("Error when open Registry under {0}", SubKey),
              e.InnerException);
          }
    
          List<string> wifiDeviceNames = new List<string>();
          foreach (string wlName in valueNames)
          {
            if (wlName.StartsWith(ValueKey))
            {
              wifiDeviceNames.Add(wlName);
            }
          }
    
          return wifiDeviceNames.ToArray<string>();
        }
      }
    }
    
    

    • Marked as answer by Bin-ze Zhao Friday, November 26, 2010 6:57 AM
    Wednesday, November 24, 2010 9:47 AM

All replies

  • The Automatic Configuration Service API (ZeroConfig) will do that.

    http://msdn.microsoft.com/en-us/library/ms895312.aspx

    You will need to p/invoke the native functions in your C# application

     

    -PaulH

    ----------

    Edit:

    WZC won't turn the radio on and off. For that, you'll need to use something like the Power Management API.

    http://www.codeproject.com/KB/windows/RadioPower.aspx

     

    • Marked as answer by Bin-ze Zhao Friday, November 26, 2010 6:57 AM
    Monday, November 22, 2010 9:52 PM
  • I implemented such function using P/Invoke in C#.

     

    Please see my code:

    CEDevicePowerState.cs

    namespace MobileAutomation.PhoneCore.NativeStructures
    {
      /// <summary>
      /// Enumeration definition for CEDEVICE_POWER_STATE.
      /// Link: http://msdn.microsoft.com/en-us/library/bb201981.aspx
      /// </summary>
      public enum CEDevicePowerState : int
      {
        /// <summary>System state is unspecified.</summary>
        PwrDeviceUnspecified = -1,
    
        /// <summary>Full On: full power, full functionality.</summary>
        D0 = 0,
    
        /// <summary>Low Power On: fully functional at low power/performance.</summary>
        D1,
    
        /// <summary>Standby: partially powered with automatic wake.</summary>
        D2,
    
        /// <summary>Sleep: partially powered with device initiated wake.</summary>
        D3,
    
        /// <summary>Off: Power Off.</summary>
        D4,
    
        /// <summary>Valid device power state values are greater than PwrDeviceUnspecified and less than PwrDeviceMaximum.</summary>
        PwrDeviceMaximum
      }
    }
    
    

     

    2. P/Invoke for coredll.dll

    namespace MobileAutomation.PhoneCore.NativeMethods
    {
      using System;
      using System.Runtime.InteropServices;
      using System.Text;
      using MobileAutomation.PhoneCore.NativeStructures;
    
      /// <summary>
      /// Helper class for Native Methods in coredll.dll
      /// </summary>
      public static class CoreDll
      {
    
        /// <summary>
        /// Returns the current power state for a device.
        /// Link: http://msdn.microsoft.com/en-us/library/aa447831.aspx
        /// </summary>
        /// <param name="device">[in] PVOID pvDevice: Specifies the device. Must be a valid LPWSTR device name</param>
        /// <param name="deviceFlags">[in] DWORD dwDeviceFlags: Set to powerName to specify the name of the device that is requesting the power request.</param>
        /// <param name="deviceState">[out] PCEDEVICE_POWER_STATE pDeviceState: Returns the device power state in CEDEVICE_POWER_STATE.</param>
        /// <returns>ERROR_SUCCESS, or the other error code</returns>
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern uint GetDevicePower(string device, uint deviceFlags, ref CEDevicePowerState deviceState);
    
        /// <summary>
        /// Sets the device power state for a device.
        /// Link: http://msdn.microsoft.com/en-us/library/aa918190.aspx
        /// </summary>
        /// <param name="device">[in] PVOID pvDevice: Specifies the device. Must be a valid LPWSTR device name</param>
        /// <param name="deviceFlags">[in] DWORD dwDeviceFlags: Set to powerName to specify the name of the device that is requesting the power request.</param>
        /// <param name="deviceState">[in] CEDEVICE_POWER_STATE DeviceState: Specifies the desired device power state.</param>
        /// <returns>ERROR_SUCCESS, or the other error code</returns>
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern uint SetDevicePower(string device, uint deviceFlags, CEDevicePowerState deviceState);
    
      }
    }
    

    3. WifiPowerManagement.cs

    namespace MobileAutomation.PhoneCore.PowerManagement
    {
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using Microsoft.Win32;
      using MobileAutomation.PhoneCore;
      using MobileAutomation.PhoneCore.NativeMethods;
      using MobileAutomation.PhoneCore.NativeStructures;
    
      /// <summary>
      /// Helper class for WIFI Power Management.
      /// </summary>
      public class WifiPowerManagement
      {
        /// <summary>the SUBKEY storing WIFI devices.</summary>
        private const string SubKey = @"\System\CurrentControlSet\Control\Power\State\";
        
        /// <summary>the value keyword of WIFI devices</summary>
        private const string ValueKey = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}";
    
        /// <summary>the Power Name of WIFI device</summary>
        private const int PowerName = 0x00000001;
    
        /// <summary>
        /// Get WIFI power state
        /// </summary>
        /// <returns>CEDevicePowerState value</returns>
        public static CEDevicePowerState GetWifiPowerState()
        {
          CEDevicePowerState powserState = CEDevicePowerState.PwrDeviceUnspecified;
          CoreDll.GetDevicePower(WifiPowerManagement.GetUniqueWifiDeviceName(), PowerName, ref powserState);
    
          return powserState;
        }
    
        /// <summary>
        /// Set WIFI power state
        /// </summary>
        /// <param name="powserState">CEDevicePowerState value</param>
        /// <returns>Error code as WinError</returns>
        public static WinError SetWifiPowerState(CEDevicePowerState powserState)
        {
          uint result = CoreDll.SetDevicePower(WifiPowerManagement.GetUniqueWifiDeviceName(), PowerName, powserState);
          return (WinError)result;
        }
    
        /// <summary>
        /// Get unique WIFI device name in RegistryKey
        /// </summary>
        /// <returns>WIFI device name</returns>
        public static string GetUniqueWifiDeviceName()
        {
          string[] wifiDeviceNames = WifiPowerManagement.GetWifiDeviceNames();
          if (wifiDeviceNames.Length == 1)
          {
            return wifiDeviceNames[0];
          }
          else
          {
            throw new RankException(string.Format("Expected Wifi devices count is 1, Actual: {0}", wifiDeviceNames.Length));
          }
        }
        
        /// <summary>
        /// Search for all the WIFI devices in RegistryKey
        /// </summary>
        /// <returns>Array of the WIFI device name</returns>
        public static string[] GetWifiDeviceNames()
        {
          string[] valueNames = null;
          try
          {
            using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(SubKey))
            {
              valueNames = regKey.GetValueNames();
              regKey.Close();
            }
          }
          catch (Exception e)
          {
            throw new Exception(
              string.Format("Error when open Registry under {0}", SubKey),
              e.InnerException);
          }
    
          List<string> wifiDeviceNames = new List<string>();
          foreach (string wlName in valueNames)
          {
            if (wlName.StartsWith(ValueKey))
            {
              wifiDeviceNames.Add(wlName);
            }
          }
    
          return wifiDeviceNames.ToArray<string>();
        }
      }
    }
    
    

    • Marked as answer by Bin-ze Zhao Friday, November 26, 2010 6:57 AM
    Wednesday, November 24, 2010 9:47 AM