[Windows Mobile Development and Hacking General] zzattack:
executable

Figured I might as well compile it. Should work on every widcomm enabled stack.

Also added a managed C# class to enable and disable radios on both microsoft and widcomm stack enabled devices. Creating a native one for both stacks shouldn't be hard either, but I have no need for it at all, so I didn't.

public static class BthRadio {

/// <summary>
/// Whether the device is equipped with a Widcomm bluetooth stack
/// </summary>
public static bool IsWidcomm;
/// <summary>
/// Whether the device is equipped with a Microsoft bluetooth stack
/// </summary>
public static bool IsMicrosoft;

/// <summary>
/// Determines stack properties
/// </summary>
static BthRadio() {
// determine stack type used on this device
IsWidcomm = OpenNETCF.NativeMethods.NativeLibraryExists("BtSdkCE50.dll") ||
OpenNETCF.NativeMethods.NativeLibraryExists("BtSdkCE30.dll");
IsMicrosoft = !IsWidcomm;
}

/// <summary>
/// Toggles bluetooth radio enabledness
/// </summary>
/// <returns>Whether the action was performed successfully</returns>
public static bool ToggleRadio() {
if (IsEnabled()) return DisableRadio();
else return EnableRadio();
}

/// <summary>
/// Returns whether primary radio is enabled
/// </summary>
/// <returns></returns>
public static bool IsEnabled() {
int mode = -1;
if (IsMicrosoft) {
int success = BthGetMode(ref mode);
return success == 0 && mode >= 1;
}
else {
IntPtr bt_hwnd = Win32.FindWindow("WCE_BTTRAY", null);
uint WIDCOMM_WM_GETSTACKSTATUS = Win32.RegisterWindowMessage("WIDCOMM_WM_GETSTACKSTATUS");
int radio_enabled = Win32.SendMessage(bt_hwnd, WIDCOMM_WM_GETSTACKSTATUS, 0, 0) & 1;
return radio_enabled != 0;
}
}

/// <summary>
/// Attempts to enable the bluetooth radio.
/// </summary>
/// <returns>True if operation executed successfully, otherwise false</returns>
public static bool EnableRadio() {
if (IsMicrosoft) return BthSetMode(1) == 0;
else {
uint WIDCOMM_WM_SETSTACKCONFIG = Win32.RegisterWindowMessage("WIDCOMM_WM_SETSTACKCONFIG");
int result = Win32.SendMessage(Win32.FindWindow("WCE_BTTRAY", null), WIDCOMM_WM_SETSTACKCONFIG, 1, 1);
if (result == 0) return false;
// sleep at least 3 seconds, required before change can take effect
Thread.Sleep(3000);
// now poll at most an additional 15 seconds to determine if the
// change was in fact effective
for (int i = 0; i < 15; i++) if (IsEnabled()) break;
return IsEnabled();
}
}

/// <summary>
/// Attempts to disable the bluetooth radio.
/// </summary>
/// <returns>True if operation executed successfully, otherwise false</returns>
public static bool DisableRadio() {
if (IsMicrosoft) return BthSetMode(0) == 0;
else {
uint WIDCOMM_WM_SETSTACKCONFIG = Win32.RegisterWindowMessage("WIDCOMM_WM_SETSTACKCONFIG");
int result = Win32.SendMessage(Win32.FindWindow("WCE_BTTRAY", null), WIDCOMM_WM_SETSTACKCONFIG, 0, 0);
if (result == 0) return false;
// sleep at least 3 seconds, required before change can take effect
Thread.Sleep(3000);
// now poll at most an additional 15 seconds to determine if the
// change was in fact effective
for (int i = 0; i < 15; i++) if (!IsEnabled()) break;
return !IsEnabled();
}
}

#region Bluetooth P/Invoke declarations
[DllImport("BthUtil.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int BthSetMode(int dwMode);

[DllImport("BthUtil.dll", CharSet = CharSet.Auto)]
public static extern int BthGetMode(ref int pdwMode);
#endregion
}