mirror of https://github.com/dahall/Vanara.git
Added `TestHelper.IsElevated` property
parent
5ef5280cb7
commit
680892bf5c
|
@ -6,9 +6,13 @@ using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.InteropServices.ComTypes;
|
using System.Runtime.InteropServices.ComTypes;
|
||||||
|
using System.Security.AccessControl;
|
||||||
|
using System.Security.Principal;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Vanara.Extensions;
|
using Vanara.Extensions;
|
||||||
using Vanara.InteropServices;
|
using Vanara.InteropServices;
|
||||||
|
using Vanara.Windows.Shell;
|
||||||
|
using static Vanara.PInvoke.AdvApi32;
|
||||||
|
|
||||||
namespace Vanara.PInvoke.Tests
|
namespace Vanara.PInvoke.Tests
|
||||||
{
|
{
|
||||||
|
@ -23,10 +27,30 @@ namespace Vanara.PInvoke.Tests
|
||||||
new GenJsonConverter<ulong, SizeT>(i => new SizeT(i), o => o.Value),
|
new GenJsonConverter<ulong, SizeT>(i => new SizeT(i), o => o.Value),
|
||||||
new GenJsonConverter<DateTime, FILETIME>(dt => dt.ToFileTimeStruct(), ft => ft.ToDateTime()),
|
new GenJsonConverter<DateTime, FILETIME>(dt => dt.ToFileTimeStruct(), ft => ft.ToDateTime()),
|
||||||
new GenJsonConverter<string, IPAddress>(i => IPAddress.Parse(i), o => o.ToString()),
|
new GenJsonConverter<string, IPAddress>(i => IPAddress.Parse(i), o => o.ToString()),
|
||||||
|
new GenJsonConverter<string, GenericSecurityDescriptor>(i => new RawSecurityDescriptor(i), o => o.GetSddlForm(AccessControlSections.All)),
|
||||||
|
new GenJsonConverter<string, IndirectString>(i => new(i), o => o.ToString()),
|
||||||
|
new GenJsonConverter<string, SecurityIdentifier>(i => new(i), o => o.ToString()),
|
||||||
},
|
},
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/// <summary>Gets a value indicating whether the current process is elevated.</summary>
|
||||||
|
/// <value><see langword="true"/> if the current process is elevated; otherwise, <see langword="false"/>.</value>
|
||||||
|
public static bool IsElevated
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Open the access token of the current process with TOKEN_QUERY.
|
||||||
|
using var hObject = SafeHTOKEN.FromProcess(Process.GetCurrentProcess(), TokenAccess.TOKEN_QUERY | TokenAccess.TOKEN_DUPLICATE);
|
||||||
|
return hObject.IsElevated;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Process RunThrottleApp() => Process.Start(testApp);
|
public static Process RunThrottleApp() => Process.Start(testApp);
|
||||||
|
|
||||||
public static void SetThrottle(string type, bool on)
|
public static void SetThrottle(string type, bool on)
|
||||||
|
@ -106,6 +130,9 @@ Simple:
|
||||||
case SafeAllocatedMemoryHandleBase mem:
|
case SafeAllocatedMemoryHandleBase mem:
|
||||||
return mem.Dump;
|
return mem.Dump;
|
||||||
|
|
||||||
|
case System.Security.AccessControl.GenericSecurityDescriptor sd:
|
||||||
|
return sd.GetSddlForm(System.Security.AccessControl.AccessControlSections.All);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
try { return JsonConvert.SerializeObject(value, Formatting.Indented, jsonSet.Value); }
|
try { return JsonConvert.SerializeObject(value, Formatting.Indented, jsonSet.Value); }
|
||||||
catch (Exception e) { return e.ToString(); }
|
catch (Exception e) { return e.ToString(); }
|
||||||
|
@ -116,8 +143,8 @@ Simple:
|
||||||
|
|
||||||
private class GenJsonConverter<TIn, TOut> : JsonConverter<TOut>
|
private class GenJsonConverter<TIn, TOut> : JsonConverter<TOut>
|
||||||
{
|
{
|
||||||
Func<TIn, TOut> rdr;
|
private readonly Func<TIn, TOut> rdr;
|
||||||
Func<TOut, TIn> wtr;
|
private readonly Func<TOut, TIn> wtr;
|
||||||
|
|
||||||
public GenJsonConverter(Func<TIn, TOut> r, Func<TOut, TIn> w)
|
public GenJsonConverter(Func<TIn, TOut> r, Func<TOut, TIn> w)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue