From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.



怎樣寫程式來查正在使用Windows的使用者有沒有系統管理員權限



主要軟體:
主要軟體版本: 7.0
主要軟體修正版本: 7.0
次要軟體: N/A

問題: 在很多程式中, 我們會需要知道使用者是否有系統管理員權限. 怎麼寫一個程式來做到?

解答: 請使用以下的程式. 此程式會回傳"True"如果使用者有系統管理員權限.

BOOL IsUserAdmin(VOID)
/*++
Routine Description: This routine returns TRUE if the caller's process
is a member of the Administrators local group. Caller is NOT expected
to be impersonating anyone and is expected to be able to open its own
process and process token.
Arguments: None.
Return Value:
TRUE - Caller has Administrators local group.
FALSE - Caller does not have Administrators local group. --
*/
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if(b)
{
if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}

return(b);
}

附加檔案中有此程式的DLL與一個使用此DLL的VI. 資源的作業系統有Windows XP, Windows 2000 Professional, 與 Windows Server 2000, 2003.


相關連結: MSDN Link

附加檔案:


Check Admin.rar - Check Admin.rar


報告日期: 06/01/2004
最後更新: 07/01/2004
文件偏號: 3A019LFE