123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace DiskAccessLibrary
- {
- public enum SecurityPrivilegeName
- {
- SeRestorePrivilege,
- SeBackupPrivilege,
- SeManageVolumePrivilege,
- }
- public class SecurityUtils
- {
- public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
- public const int TOKEN_QUERY = 0x00000008;
-
- public const int SE_PRIVILEGE_ENABLED = 0x00000002;
- [StructLayout(LayoutKind.Sequential)]
- public struct LUID
- {
- public int LowPart;
- public int HighPart;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct TOKEN_PRIVILEGES
- {
- public int PrivilegeCount;
- public LUID Luid;
- public int Attributes;
- }
- [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
- ref int tokenhandle);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- public static extern int GetCurrentProcess();
- [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
- [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs, [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate,
- int bufferlength, int PreivousState, int Returnlength);
- public static bool ObtainManageVolumePrivilege()
- {
- return ObtainPrivilege(SecurityPrivilegeName.SeManageVolumePrivilege);
- }
- public static bool ObtainPrivilege(SecurityPrivilegeName privilegeName)
- {
- int tokenHandle = 0;
- int retval = 0;
- TOKEN_PRIVILEGES tokenPrivileges = new TOKEN_PRIVILEGES();
- LUID privilegeLuid = new LUID();
- retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle);
- if (retval == 0)
- {
- return false;
- }
- retval = LookupPrivilegeValue(null, privilegeName.ToString(), ref privilegeLuid);
- if (retval == 0)
- {
- return false;
- }
- tokenPrivileges.PrivilegeCount = 1;
- tokenPrivileges.Attributes = SE_PRIVILEGE_ENABLED;
- tokenPrivileges.Luid = privilegeLuid;
- retval = AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 0, 0, 0);
- if (retval == 0)
- {
- return false;
- }
- else
- {
-
-
-
- int errorCode = Marshal.GetLastWin32Error();
- return (errorCode == (int)Win32Error.ERROR_SUCCESS);
- }
- }
- }
- }
|