Created
October 26, 2018 13:48
-
-
Save DownWithUp/59c15f5b93ac35423fdc140eb34ac244 to your computer and use it in GitHub Desktop.
Use an IOCTL to create a beep from Beep.sys
This file contains hidden or 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
#include <Windows.h> | |
#include <stdio.h> | |
#include <winternl.h> | |
typedef struct _BEEP_SETTINGS { | |
ULONG ulFrequency; | |
ULONG ulDuration; | |
} BEEP_SETTINGS; | |
void main() { | |
// Prep string | |
UNICODE_STRING uszDeviceName; | |
OBJECT_ATTRIBUTES BeepObjectAttributes; | |
RtlInitUnicodeString(&uszDeviceName, L"\\Device\\Beep"); | |
// Prep object | |
InitializeObjectAttributes(&BeepObjectAttributes, &uszDeviceName, 0, NULL, NULL); | |
// Get a handle on beep.sys | |
PIO_STATUS_BLOCK pOutStatus = 0; | |
HANDLE hDriver; | |
IO_STATUS_BLOCK IoStatus; | |
NTSTATUS ntOut = NtCreateFile(&hDriver, 0x3, &BeepObjectAttributes, &IoStatus, NULL, 0, 0x3, 0x3, 0, 0, 0); | |
if (ntOut == 0x0) { | |
DWORD dwIOCTL_BEEP = CTL_CODE(FILE_DEVICE_BEEP, 0, METHOD_BUFFERED, FILE_ANY_ACCESS); | |
BEEP_SETTINGS BeepSettings; | |
BeepSettings.ulDuration = 10000; | |
BeepSettings.ulFrequency = 500; | |
DWORD dwReturned = 0; | |
DeviceIoControl(hDriver, dwIOCTL_BEEP, &BeepSettings, sizeof(BEEP_SETTINGS), NULL, 0, dwReturned, NULL); | |
} // You should add an else statement | |
ExitProcess(0); | |
} | |
// I know this is super useful, so you're welcome! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reviewed code to work in Windows 11 (with VS 2022):