博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Win7开发系列: Win7 UAC帮助类
阅读量:6201 次
发布时间:2019-06-21

本文共 3295 字,大约阅读时间需要 10 分钟。

我有一个应用程序需要检测是否正在升高的特权。我现在代码建立这样的:

 

函数功能 : 是否是管理员

 

private
 
static
 
bool
 _isAdministrator()
{
    WindowsIdentity identity 
=
 WindowsIdentity.GetCurrent();
    WindowsPrincipal principal 
=
 
new
 WindowsPrincipal(identity);
    
return
 principal.IsInRole (WindowsBuiltInRole.Administrator);

 

}

 

 类主要功能:

   1:UAC状态查询

   2:用户状态查询

 

public
 
static
 
class
 UacHelper
{
    
private
 
const
 
string
 uacRegistryKey 
=
 
"
Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System
"
;
    
private
 
const
 
string
 uacRegistryValue 
=
 
"
EnableLUA
"
;
    
private
 
static
 
uint
 STANDARD_RIGHTS_READ 
=
 
0x00020000
;
    
private
 
static
 
uint
 TOKEN_QUERY 
=
 
0x0008
;
    
private
 
static
 
uint
 TOKEN_READ 
=
 (STANDARD_RIGHTS_READ 
|
 TOKEN_QUERY);
    [DllImport(
"
advapi32.dll
"
, SetLastError 
=
 
true
)]
    [
return
: MarshalAs(UnmanagedType.Bool)]
    
static
 
extern
 
bool
 OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, 
out
 IntPtr TokenHandle);
    [DllImport(
"
advapi32.dll
"
, SetLastError 
=
 
true
)]
    
public
 
static
 
extern
 
bool
 GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, 
uint
 TokenInformationLength, 
out
 
uint
 ReturnLength);
    
public
 
enum
 TOKEN_INFORMATION_CLASS
    {
        TokenUser 
=
 
1
,
        TokenGroups,
        TokenPrivileges,
        TokenOwner,
        TokenPrimaryGroup,
        TokenDefaultDacl,
        TokenSource,
        TokenType,
        TokenImpersonationLevel,
        TokenStatistics,
        TokenRestrictedSids,
        TokenSessionId,
        TokenGroupsAndPrivileges,
        TokenSessionReference,
        TokenSandBoxInert,
        TokenAuditPolicy,
        TokenOrigin,
        TokenElevationType,
        TokenLinkedToken,
        TokenElevation,
        TokenHasRestrictions,
        TokenAccessInformation,
        TokenVirtualizationAllowed,
        TokenVirtualizationEnabled,
        TokenIntegrityLevel,
        TokenUIAccess,
        TokenMandatoryPolicy,
        TokenLogonSid,
        MaxTokenInfoClass
    }
    
public
 
enum
 TOKEN_ELEVATION_TYPE
    {
        TokenElevationTypeDefault 
=
 
1
,
        TokenElevationTypeFull,
        TokenElevationTypeLimited
    }
    
public
 
static
 
bool
 IsUacEnabled
    {
        
get
        {
            RegistryKey uacKey 
=
 Registry.LocalMachine.OpenSubKey(uacRegistryKey, 
false
);
            
bool
 result 
=
 uacKey.GetValue(uacRegistryValue).Equals(
1
);
            
return
 result;
        }
    }
    
public
 
static
 
bool
 IsProcessElevated
    {
        
get
        {
            
if
 (IsUacEnabled)
            {
                IntPtr tokenHandle;
                
if
 (
!
OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, 
out
 tokenHandle))
                {
                    
throw
 
new
 ApplicationException(
"
Could not get process token.  Win32 Error Code: 
"
 
+
 Marshal.GetLastWin32Error());
                }
                TOKEN_ELEVATION_TYPE elevationResult 
=
 TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault;
                
int
 elevationResultSize 
=
 Marshal.SizeOf((
int
)elevationResult);
                
uint
 returnedSize 
=
 
0
;
                IntPtr elevationTypePtr 
=
 Marshal.AllocHGlobal(elevationResultSize);
                
bool
 success 
=
 GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, (
uint
)elevationResultSize, 
out
 returnedSize);
                
if
 (success)
                {
                    elevationResult 
=
 (TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr);
                    
bool
 isProcessAdmin 
=
 elevationResult 
==
 TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
                    
return
 isProcessAdmin;
                }
                
else
                {
                    
throw
 
new
 ApplicationException(
"
Unable to determine the current elevation.
"
);
                }
            }
            
else
            {
                WindowsIdentity identity 
=
 WindowsIdentity.GetCurrent();
                WindowsPrincipal principal 
=
 
new
 WindowsPrincipal(identity);
                
bool
 result 
=
 principal.IsInRole(WindowsBuiltInRole.Administrator);
                
return
 result;
            }
        }
    }
}

 

 

 相关资料:

    

转载于:https://www.cnblogs.com/luomingui/archive/2011/07/04/2097174.html

你可能感兴趣的文章
环境搭建之六-- mysql
查看>>
linux下查看各软件编译参数
查看>>
【58沈剑 架构师之路】选redis还是memcache,源码怎么说
查看>>
phpmyadmin安装与配置
查看>>
code blocks 无法运行
查看>>
互联网产品经理的必读书目有哪些?
查看>>
MySQL 8.0新特性--Regular Expressions(四)
查看>>
我的友情链接
查看>>
C#学习常用类--DataRow类
查看>>
lua中关于table的处理
查看>>
Objective-C 入门(三)内存管理
查看>>
MongoDB文档更新(二)
查看>>
docker删除镜像
查看>>
Sharepoint Server 2010安装部署
查看>>
Tomcat 源码分析(-)启动过程分析
查看>>
ndk中使用native Socket及jni异常处理
查看>>
bash的算术运算和条件测试语句
查看>>
MySQL -- INSERT 中ON DUPLICATE KEY UPDATE的使用
查看>>
HTTP缓存控制
查看>>
mysql 函数之 字符串函数
查看>>