C#如何导入ntdll.dll来使用NtDelayExecution和ZwSetTimerResolution?
C# How to import ntdll.dll to use NtDelayExecution and ZwSetTimerResolution?
我是 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);
}
导入它的最简单方法是使用 nuget。
- 右键单击 nuget 管理器
- 输入ntdll.dll,然后select安装
我是 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);
}
导入它的最简单方法是使用 nuget。
- 右键单击 nuget 管理器
- 输入ntdll.dll,然后select安装