Skip to content

Instantly share code, notes, and snippets.

View nefarius's full-sized avatar
😇
I may be slow to respond.

Benjamin Höglinger-Stelzer nefarius

😇
I may be slow to respond.
View GitHub Profile
@nefarius
nefarius / USB_CyclePort.c
Created July 18, 2024 01:19
Utility function to power cycle/restart the hub port of a USB device identified by instance ID.
#include <Windows.h>
#include <SetupAPI.h>
#include <cfgmgr32.h>
#include <usbioctl.h>
_Must_inspect_result_
_Success_(return == TRUE)
BOOL
USB_CyclePort(
_In_ PCWSTR InstanceId
// Use e.g. https://github.com/dotnet/pinvoke/
// Install-Package PInvoke.Kernel32
using (var handle = Kernel32.CreateFile("\\\\.\\HidHide",
Kernel32.ACCESS_MASK.GenericRight.GENERIC_READ,
Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.CreateFileFlags.FILE_ATTRIBUTE_NORMAL,
Kernel32.SafeObjectHandle.Null
))
{
// Use e.g. https://github.com/dotnet/pinvoke/
// Install-Package PInvoke.Kernel32
using (var handle = Kernel32.CreateFile("\\\\.\\HidHide",
Kernel32.ACCESS_MASK.GenericRight.GENERIC_READ,
Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.CreateFileFlags.FILE_ATTRIBUTE_NORMAL,
Kernel32.SafeObjectHandle.Null
))
{
/// <summary>
/// Path manipulation and volume helper methods.
/// </summary>
internal static class VolumeHelper
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetVolumePathNamesForVolumeNameW(
[MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeName,
[MarshalAs(UnmanagedType.LPWStr)] [Out]
@nefarius
nefarius / StringHelperUtil.cs
Created April 29, 2021 11:14
String manipulation helper methods.
/// <summary>
/// String manipulation helper methods.
/// </summary>
internal static class StringHelperUtil
{
/// <summary>
/// Converts an array of <see cref="string" /> into a double-null-terminated multi-byte character memory block.
/// </summary>
/// <param name="instances">Source array of strings.</param>
/// <param name="length">The length of the resulting byte array.</param>
@nefarius
nefarius / IsBluetoothRadioPresent.ps1
Created January 16, 2020 20:33
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;