我有几个从程序中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
但我仍然得到相同的代码。我在做什么是正确的还是我错过了什么?