Skip to content

Instantly share code, notes, and snippets.

@iljavs
Created October 17, 2020 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iljavs/712592838f5b16b761c81fadfb25d36a to your computer and use it in GitHub Desktop.
Save iljavs/712592838f5b16b761c81fadfb25d36a to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <stdio.h>
#include <winternl.h>
#include <ntstatus.h>
#define BEEPDEV L"\\Device\\Beep"
#define IOCTLCODE 0x10000
typedef NTSTATUS(CALLBACK* NTOPENFILE)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK, ULONG, ULONG);
struct dd {
unsigned int freq;
unsigned int duration;
};
int main() {
HANDLE h;
IO_STATUS_BLOCK iosb;
OBJECT_ATTRIBUTES oa;
NTSTATUS status;
NTOPENFILE NtOpenFile;
dd data;
HMODULE hmod = LoadLibraryW(L"ntdll.dll");
if (hmod == NULL) {
printf("LoadLibraryW() failed\n");
exit(0);
}
NtOpenFile = (NTOPENFILE)GetProcAddress(hmod, "NtOpenFile");
if (NtOpenFile == NULL) {
printf("GetProcAddress() failed\n");
exit(0);
}
UNICODE_STRING BeepDevice = {sizeof(BEEPDEV) - sizeof(WCHAR), sizeof(BEEPDEV), (PWSTR)BEEPDEV };
InitializeObjectAttributes(&oa, &BeepDevice, 0, NULL, NULL);
status = NtOpenFile(&h, GENERIC_READ, &oa, &iosb, 0, 0);
if (status != STATUS_SUCCESS) {
printf("NtOpenFile() failed\n");
exit(0);
}
data.duration = 1000;
data.freq = 1100000;
DWORD bytes = 0;
BOOL r = DeviceIoControl(h, IOCTLCODE, &data, sizeof(data), NULL, 0, &bytes, NULL);
printf("r: %u\n", r);
Sleep(2000);
printf("done\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment