我有几个从程序中Kernel32.dll调用的函数。C#我导入的功能如下:
[System.Runtime.InteropServices.DllImport("Kernel32.dll", EntryPoint = "GetSystemTime", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[System.Runtime.InteropServices.DllImport("Kernel32.dll", EntryPoint = "SetSystemTime", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern UInt32 SetSystemTime(ref SYSTEMTIME lpSystemTime);
SYSTEMTIME这些方法接受的结构在哪里。
的文档SetSystemTime说,如果函数失败并调用GetLastError以获取有关失败的扩展信息,它将返回 0。所以我以与上述函数相同的方式GetLastError从文件中导入。Kernel32.dll现在,当我将失败模拟为:
if (SetSystemTime(ref st) == 0) {
Console.WriteLine(GetLastError() + " Error occurred: SetSystemTime returned zero.");
}
GetLastError()打印一个代码,它1314是 for A required privilege not held by the client。我也更改了Local security policy将用户添加到其中Replace a process level token,User rights assignment但我仍然得到相同的代码。我在做什么是正确的还是我错过了什么?