Skip to content

Instantly share code, notes, and snippets.

@hydranix
Created April 16, 2017 14:35
Show Gist options
  • Save hydranix/b8af0c197a1cc11429da600c33c15418 to your computer and use it in GitHub Desktop.
Save hydranix/b8af0c197a1cc11429da600c33c15418 to your computer and use it in GitHub Desktop.
[Undocumented WinAPI] Timer Resolution
// From Oskar Dahlberg's post at:
// http://stackoverflow.com/a/31411628/4725495
#include <Windows.h>
static NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS(__stdcall*)(BOOL, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution");
static NTSTATUS(__stdcall *ZwSetTimerResolution)(IN ULONG RequestedResolution, IN BOOLEAN Set, OUT PULONG ActualResolution) = (NTSTATUS(__stdcall*)(ULONG, BOOLEAN, PULONG)) GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwSetTimerResolution");
static void SleepShort(float milliseconds) {
static bool once = true;
if (once) {
ULONG actualResolution;
ZwSetTimerResolution(1, true, &actualResolution);
once = false;
}
LARGE_INTEGER interval;
interval.QuadPart = -1 * (int)(milliseconds * 10000.0f);
NtDelayExecution(false, &interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment