我的Vista應用程序需要知道用戶是以「管理員」身份(提升)還是以標準用戶身份(未提升)啓動它。我如何在運行時檢測?如何檢測我的進程是否正在運行UAC提升?
回答
下面的C++函數可以做到這一點:
HRESULT GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet);
/*
Parameters:
ptet
[out] Pointer to a variable that receives the elevation type of the current process.
The possible values are:
TokenElevationTypeDefault - This value indicates that either UAC is disabled,
or the process is started by a standard user (not a member of the Administrators group).
The following two values can be returned only if both the UAC is enabled
and the user is a member of the Administrator's group:
TokenElevationTypeFull - the process is running elevated.
TokenElevationTypeLimited - the process is not running elevated.
Return Values:
If the function succeeds, the return value is S_OK.
If the function fails, the return value is E_FAIL. To get extended error information, call GetLastError().
Implementation:
*/
HRESULT GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet)
{
if (!IsVista())
return E_FAIL;
HRESULT hResult = E_FAIL; // assume an error occurred
HANDLE hToken = NULL;
if (!::OpenProcessToken(
::GetCurrentProcess(),
TOKEN_QUERY,
&hToken))
{
return hResult;
}
DWORD dwReturnLength = 0;
if (::GetTokenInformation(
hToken,
TokenElevationType,
ptet,
sizeof(*ptet),
&dwReturnLength))
{
ASSERT(dwReturnLength == sizeof(*ptet));
hResult = S_OK;
}
::CloseHandle(hToken);
return hResult;
}
對於IsVista函數(以及有關GetElevationType的更多詳細信息),請參閱Andrei的博文:http ://www.softblog.com/2008-02/vista-tools/ – 2010-09-02 17:30:31
對於我們這些工作在C#,在Windows SDK有一個「UACDemo」應用爲「跨技術示例」的一部分。他們發現,如果當前用戶是使用此方法管理員:
private bool IsAdministrator
{
get
{
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(wi);
return wp.IsInRole(WindowsBuiltInRole.Administrator);
}
}
(注:我重構原始代碼是一個屬性,而不是一個「如果」語句)
問題,這將做域安全嗎? (MYDOMAIN \ Administrators)或者這只是本地安全? – mattlant 2008-09-24 07:17:39
我不認爲擡高類型是你想要的答案。你只是想知道它是否升高。當您調用GetTokenInformation時,請使用TokenElevation而不是TokenElevationType。如果結構返回正值,則用戶是admin。如果爲零,則用戶爲正常高程。
這裏是一個Delphi的解決方案:
function TMyAppInfo.RunningAsAdmin: boolean;
var
hToken, hProcess: THandle;
pTokenInformation: pointer;
ReturnLength: DWord;
TokenInformation: TTokenElevation;
begin
hProcess := GetCurrentProcess;
try
if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then try
TokenInformation.TokenIsElevated := 0;
pTokenInformation := @TokenInformation;
GetTokenInformation(hToken, TokenElevation, pTokenInformation, sizeof(TokenInformation), ReturnLength);
result := (TokenInformation.TokenIsElevated > 0);
finally
CloseHandle(hToken);
end;
except
result := false;
end;
end;
這裏是一個VB6實施檢查的,如果(當前)工藝升高
Option Explicit
'--- for OpenProcessToken
Private Const TOKEN_QUERY As Long = &H8
Private Const TokenElevation As Long = 20
Private Declare Function GetCurrentProcess Lib "kernel32"() As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function IsElevated(Optional ByVal hProcess As Long) As Boolean
Dim hToken As Long
Dim dwIsElevated As Long
Dim dwLength As Long
If hProcess = 0 Then
hProcess = GetCurrentProcess()
End If
If OpenProcessToken(hProcess, TOKEN_QUERY, hToken) Then
If GetTokenInformation(hToken, TokenElevation, dwIsElevated, 4, dwLength) Then
IsElevated = (dwIsElevated <> 0)
End If
Call CloseHandle(hToken)
End If
End Function
- 1. 如何使用UAC提升Perl進程
- 2. 如何檢測可執行文件是否需要UAC提升(C#pref)
- 3. 如何在啓動時運行需要UAC提升的程序?
- 4. 檢查進程是否正在運行
- 5. 檢查進程是否正在運行
- 6. 檢測Windows進程和應用程序是否正在運行
- 7. 如何檢測我的新貴腳本是否正在運行?
- 8. 如何檢測我的應用程序是否正在運行EarlGrey測試?
- 9. 如何檢測Vista UAC是否啓用?
- 10. 檢測Internet Explorer進程是否正在運行Flash Player
- 11. 如何檢查正在運行的進程是否是後臺進程?
- 12. 我如何在運行時在運行時提升我的進程win xp
- 13. 如何在Win和MAC上使用Python檢測進程是否正在運行
- 14. 使用UAC進行權限提升
- 15. 如何檢測遠程計算機是否正在運行RDP?
- 16. 如何檢測應用程序是否正在運行Cloud9?
- 17. 通過UAC提升ProcessBuilder進程?
- 18. 如何檢查Hadoop守護進程是否正在運行?
- 19. 如何檢查某個進程是否正在運行或不
- 20. 如何檢查Linux中是否正在運行進程?
- 21. 如何檢查是否一些進程正在運行
- 22. 如何檢查進程是否正在運行
- 23. 如何檢查進程是否正在運行 - Linux
- 24. UAC提升,運行一些不提升的代碼
- 25. 如何檢查某人是否正在運行我的程序?
- 26. 如何檢查我的線程是否正在運行Java
- 27. 如何檢測當前登錄用戶是否正在運行進程?
- 28. 檢查進程是否正在運行(使用進程名稱)
- 29. 檢測MediaBrowserServiceCompat是否正在運行
- 30. 檢測CountdownTimer是否正在運行
的IsUserAnAdmin功能也可能會有所幫助。 http://msdn.microsoft.com/en-us/library/windows/desktop/bb776463%28v=vs.85%29.aspx – jmnben 2011-10-14 19:11:46