操作系統:Windows XP(嵌入式) 語言:C#使用SetSystemTime以受限用戶帳戶
問題: 隨着受限用戶帳戶,我試圖改變Windows XP中的日期和時間編程,由使用函數SetSystemTime()但它返回false並且錯誤代碼是5:訪問被拒絕。
閱讀MSDN文章後,我通過使用LogonUser()和Impersonate()函數將模擬有限用戶帳戶給管理員用戶(屬於管理員組並具有更改系統時間的權限),並在SetSystemTime()但結果與以前一樣。
我嘗試在調用AdjustTokenPrivileges()之前模擬它,然後返回無錯誤,但結果與以前相同之後,將特權「SeSystemtimePrivilege」授予有限用戶帳戶。
代碼:
const int SE_PRIVILEGE_ENABLED = 2;
// Starting with limited user account
intPtr userToken = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query).Token;
IntPtr tokenDuplicate = IntPtr.Zero;
IntPtr token = IntPtr.Zero;
LogonUser("administrator", domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
DuplicateToken(token, 2, ref tokenDuplicate);
WindowsImpersonationContext wic = (new WindowsIdentity(tokenDuplicate)).Impersonate();
bool enabled = true;
TOKEN_PRIVILEGES tokenPrivilege = new TOKEN_PRIVILEGES();
tokenPrivilege.PrivilegeCount = 1;
tokenPrivilege.Privileges = new LUID_AND_ATTRIBUTES[tokenPrivilege.PrivilegeCount];
tokenPrivilege.Privileges[0].Attributes = (UInt32)(enabled ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_DISABLED);
if (LookupPrivilegeValue(null, "SeSystemtimePrivilege", out tokenPrivilege.Privileges[0].Luid))
AdjustTokenPrivileges(userToken, false, ref tokenPrivilege, (UInt32)Marshal.SizeOf(typeof(TOKEN_PRIVILEGES)), IntPtr.Zero, IntPtr.Zero);
// Return to limited user account
wic.Undo();
if(!SetSystemTime(systemTime)) // systemTime in UTC time
.... Error code here, 5 if I let administrator impersonate or 1314
你有一個想法如何解決我的問題呢?
謝謝您的回答, 阿蘭
阿蘭,有問題的代碼可能值得一看。 – 2011-05-23 11:01:20
目前還不清楚,爲什麼你調用'DuplicateToken',以及爲什麼你試圖設置系統時間*在*之後停止模擬。 – Gabe 2011-05-23 12:07:26
嗨Gabe,DuplicateToken用於因爲我可以保持wic,但在我的例子中,這是沒有必要的。我在SetSystemTime之前停止模仿,因爲impersonate僅用於啓用有限用戶accout的「SeSystemtimePrivilege」特權。我試圖使用模擬,所以SetSystemTime被稱爲管理員特權,但它不起作用。 – Alain 2011-05-23 12:24:32