Created
April 30, 2021 09:19
-
-
Save nefarius/880845646d177c74e94880f0568909d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
)) | |
{ | |
var buffer = Marshal.AllocHGlobal(sizeof(bool)); | |
// Enable blocking logic, if not enabled already | |
try | |
{ | |
Marshal.WriteByte(buffer, 1); | |
// Check return value for success | |
Kernel32.DeviceIoControl( | |
handle, | |
unchecked((int) IOCTL_SET_ACTIVE), | |
buffer, | |
sizeof(bool), | |
IntPtr.Zero, | |
0, | |
out _, | |
IntPtr.Zero | |
); | |
} | |
finally | |
{ | |
Marshal.FreeHGlobal(buffer); | |
} | |
// List of blocked instances | |
IList<string> instances = new List<string>(); | |
// Get existing list of blocked instances | |
// This is important to not discard entries other processes potentially made | |
// Always get the current list before altering/submitting it | |
try | |
{ | |
// Get required buffer size | |
// Check return value for success | |
Kernel32.DeviceIoControl( | |
handle, | |
unchecked((int) IOCTL_GET_BLACKLIST), | |
IntPtr.Zero, | |
0, | |
IntPtr.Zero, | |
0, | |
out var required, | |
IntPtr.Zero | |
); | |
buffer = Marshal.AllocHGlobal(required); | |
// Get actual buffer content | |
// Check return value for success | |
Kernel32.DeviceIoControl( | |
handle, | |
unchecked((int) IOCTL_GET_BLACKLIST), | |
IntPtr.Zero, | |
0, | |
buffer, | |
required, | |
out _, | |
IntPtr.Zero | |
); | |
// Store existing block-list in a more manageable "C#" fashion | |
instances = buffer.MultiSzPointerToStringArray(required).ToList(); | |
} | |
finally | |
{ | |
Marshal.FreeHGlobal(buffer); | |
} | |
// Manipulate block-list and submit it | |
try | |
{ | |
buffer = instances | |
.Concat(new[] // Add our own instance paths to the existing list | |
{ | |
// Hides wireless PlayStation 3 Controller | |
@"HID\{53f88889-1aaf-4353-a047-556b69ec6da6}&Col01\c&3945a91b&3&0000", | |
// Hides Xbox 360 Controller from XInput | |
@"USB\VID_045E&PID_028E\13FCFDC", | |
// Hides Xbox 360 Controller from DirectInput/HID-API | |
@"HID\VID_045E&PID_028E&IG_00\A&22872CBD&0&0000" | |
}) | |
.Distinct() // Remove duplicates, if any | |
.StringArrayToMultiSzPointer(out var length); // Convert to usable buffer | |
// Submit new list | |
// Check return value for success | |
Kernel32.DeviceIoControl( | |
handle, | |
unchecked((int) IOCTL_SET_BLACKLIST), | |
buffer, | |
length, | |
IntPtr.Zero, | |
0, | |
out _, | |
IntPtr.Zero | |
); | |
} | |
finally | |
{ | |
Marshal.FreeHGlobal(buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment