Skip to content

Instantly share code, notes, and snippets.

@nefarius
Created January 16, 2020 20:33
Show Gist options
  • Save nefarius/d70c62035539454f5a251d06237ad176 to your computer and use it in GitHub Desktop.
Save nefarius/d70c62035539454f5a251d06237ad176 to your computer and use it in GitHub Desktop.
PowerShell snippet to check for presence of a Bluetooth radio using P/Invoke
Add-Type -TypeDefinition @"
namespace PInvoke {
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct BLUETOOTH_FIND_RADIO_PARAMS
{
public UInt32 dwSize;
public void Init()
{
this.dwSize = (UInt32)Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAMS));
}
}
public static class BluetoothApis
{
[DllImport("BluetoothApis.dll", SetLastError=true)]
public static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, out IntPtr phRadio);
}
}
"@;
$pbtfrp = New-Object PInvoke.BLUETOOTH_FIND_RADIO_PARAMS
$pbtfrp.Init()
$phRadio = [System.IntPtr]::Zero
exit ([PInvoke.BluetoothApis]::BluetoothFindFirstRadio([ref] $pbtfrp, [ref] $phRadio) -eq [System.IntPtr]::Zero)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment