日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Win7开发系列: Win7 UAC帮助类

發布時間:2023/12/31 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Win7开发系列: Win7 UAC帮助类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我有一個應用程序需要檢測是否正在升高的特權。我現在代碼建立這樣的:

?

函數功能 : 是否是管理員

?

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;
????????????}
????????}
????}
}

?

?

?相關資料:

? ?http://uachelpers.codeplex.com/releases/view/29976?

轉載于:https://www.cnblogs.com/luomingui/archive/2011/07/04/2097174.html

總結

以上是生活随笔為你收集整理的Win7开发系列: Win7 UAC帮助类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。