我是 C# 新手,我正在嘗試使用 ntdll.dll 中的 NtDelayExecution 和 ZwSetTimerResolution 函式來創建自定義睡眠計時器。(Thread.sleep 對我的應用程式來說不夠精確)。我嘗試了許多不同的方法,但我不確定如何匯入 ntdll.dll 以使用這兩個函式。兩條靜態 NTSTATUS 行是我試圖轉換為 C# 的部分 c 。到目前為止我已經嘗試過:
public unsafe static void GoSleep(float ms, float resolution = 0.5f)
{
if (resolution < 0.5) resolution = 0.5f;
if (ms < resolution) ms = resolution;
nuint res = (nuint)(resolution * 10000);
nuint actualRes = 0;
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPWStr)] in string lpModuleName);
static NTSTATUS(__stdcall* NtDelayExecution)(bool Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS(__stdcall*)(bool, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle(X("ntdll.dll")), X("NtDelayExecution"));
static NTSTATUS(__stdcall* ZwSetTimerResolution)(IN nuint RequestedResolution, IN bool Set, OUT pnuint ActualResolution) = (NTSTATUS(__stdcall*)(ULONG, BOOLEAN, PULONG)) GetProcAddress(GetModuleHandle(X("ntdll.dll")), X("ZwSetTimerResolution"));
ZwSetTimerResolution(res, true, &actualRes);
long LargeInteger = -1 * (long)(ms * 10000.0f);
NtDelayExecution(false, &LargeInteger);
ZwSetTimerResolution(res, false, &actualRes);
}
uj5u.com熱心網友回復:
匯入它的最簡單方法是使用 nuget。
- 右鍵單擊nuget管理器

- 輸入ntdll.dll,然后選擇安裝

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/481572.html
