Skip to content

Commit 5052b9f

Browse files
author
James Forshaw
committed
Added some non-throwing apis.
1 parent 88e3b89 commit 5052b9f

1 file changed

Lines changed: 99 additions & 7 deletions

File tree

NtApiDotNet/NtKey.cs

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,38 @@ public static NtResult<NtKey> Open(ObjectAttributes obj_attributes, KeyAccessRig
275275
return Open(obj_attributes, desired_access, open_options, null, throw_on_error);
276276
}
277277

278+
/// <summary>
279+
/// Try and open a Key
280+
/// </summary>
281+
/// <param name="key_name">Path to the key to open</param>
282+
/// <param name="root">Root key if key_name is relative</param>
283+
/// <param name="desired_access">Desired access for the root key</param>
284+
/// <param name="open_options">Open options.</param>
285+
/// <param name="transaction">Optional transaction object.</param>
286+
/// <param name="throw_on_error">True to throw an exception on error.</param>
287+
/// <returns>The NT status code and object result.</returns>
288+
public static NtResult<NtKey> Open(string key_name, NtObject root, KeyAccessRights desired_access, KeyCreateOptions open_options, INtTransaction transaction, bool throw_on_error)
289+
{
290+
using (ObjectAttributes obja = new ObjectAttributes(key_name, AttributeFlags.CaseInsensitive, root))
291+
{
292+
return Open(obja, desired_access, open_options, transaction, throw_on_error);
293+
}
294+
}
295+
296+
/// <summary>
297+
/// Try and open a Key
298+
/// </summary>
299+
/// <param name="key_name">Path to the key to open</param>
300+
/// <param name="root">Root key if key_name is relative</param>
301+
/// <param name="desired_access">Desired access for the root key</param>
302+
/// <param name="open_options">Open options.</param>
303+
/// <param name="throw_on_error">True to throw an exception on error.</param>
304+
/// <returns>The NT status code and object result.</returns>
305+
public static NtResult<NtKey> Open(string key_name, NtObject root, KeyAccessRights desired_access, KeyCreateOptions open_options, bool throw_on_error)
306+
{
307+
return Open(key_name, root, desired_access, open_options, null, throw_on_error);
308+
}
309+
278310
/// <summary>
279311
/// Open a Key
280312
/// </summary>
@@ -386,11 +418,22 @@ public static NtKey CreateSymbolicLink(string path, NtKey rootkey, string target
386418
/// <summary>
387419
/// Open the machine key
388420
/// </summary>
389-
/// <returns>The opened key</returns>
421+
/// <returns>The opened key with the maximum access allowed.</returns>
390422
/// <exception cref="NtException">Thrown on error.</exception>
391423
public static NtKey GetMachineKey()
392424
{
393-
return Open(@"\Registry\Machine", null, KeyAccessRights.MaximumAllowed);
425+
return GetMachineKey(true).Result;
426+
}
427+
428+
/// <summary>
429+
/// Open the machine key
430+
/// </summary>
431+
/// <returns>The opened key with the maximum access allowed.</returns>
432+
/// <param name="throw_on_error">True to throw on error.</param>
433+
/// <exception cref="NtException">Thrown on error.</exception>
434+
public static NtResult<NtKey> GetMachineKey(bool throw_on_error)
435+
{
436+
return Open(@"\Registry\Machine", null, KeyAccessRights.MaximumAllowed, KeyCreateOptions.NonVolatile, throw_on_error);
394437
}
395438

396439
/// <summary>
@@ -400,18 +443,41 @@ public static NtKey GetMachineKey()
400443
/// <exception cref="NtException">Thrown on error.</exception>
401444
public static NtKey GetUserKey()
402445
{
403-
return Open(@"\Registry\User", null, KeyAccessRights.MaximumAllowed);
446+
return GetUserKey(true).Result;
447+
}
448+
449+
/// <summary>
450+
/// Open the user key
451+
/// </summary>
452+
/// <returns>The opened key with the maximum access allowed.</returns>
453+
/// <param name="throw_on_error">True to throw on error.</param>
454+
/// <exception cref="NtException">Thrown on error.</exception>
455+
public static NtResult<NtKey> GetUserKey(bool throw_on_error)
456+
{
457+
return Open(@"\Registry\User", null, KeyAccessRights.MaximumAllowed, KeyCreateOptions.NonVolatile, throw_on_error);
404458
}
405459

406460
/// <summary>
407461
/// Open a specific user key
408462
/// </summary>
409-
/// <param name="sid">The SID fo the user to open</param>
463+
/// <param name="sid">The SID of the user to open</param>
410464
/// <returns>The opened key</returns>
411465
/// <exception cref="NtException">Thrown on error.</exception>
412466
public static NtKey GetUserKey(Sid sid)
413467
{
414-
return Open(@"\Registry\User\" + sid.ToString(), null, KeyAccessRights.MaximumAllowed);
468+
return GetUserKey(sid, true).Result;
469+
}
470+
471+
/// <summary>
472+
/// Open the user key
473+
/// </summary>
474+
/// <param name="sid">The SID of the user to open</param>
475+
/// <param name="throw_on_error">True to throw on error.</param>
476+
/// <returns>The opened key with the maximum access allowed.</returns>
477+
/// <exception cref="NtException">Thrown on error.</exception>
478+
public static NtResult<NtKey> GetUserKey(Sid sid, bool throw_on_error)
479+
{
480+
return Open(@"\Registry\User\" + sid, null, KeyAccessRights.MaximumAllowed, KeyCreateOptions.NonVolatile, throw_on_error);
415481
}
416482

417483
/// <summary>
@@ -421,7 +487,22 @@ public static NtKey GetUserKey(Sid sid)
421487
/// <exception cref="NtException">Thrown on error.</exception>
422488
public static NtKey GetCurrentUserKey()
423489
{
424-
return GetUserKey(NtToken.CurrentUser.Sid);
490+
return GetCurrentUserKey(true).Result;
491+
}
492+
493+
/// <summary>
494+
/// Open the current user key
495+
/// </summary>
496+
/// <param name="throw_on_error">True to throw on error.</param>
497+
/// <returns>The opened key with the maximum access allowed.</returns>
498+
/// <exception cref="NtException">Thrown on error.</exception>
499+
public static NtResult<NtKey> GetCurrentUserKey(bool throw_on_error)
500+
{
501+
var user = NtToken.GetCurrentUser(throw_on_error);
502+
if (!user.IsSuccess)
503+
return user.Cast<NtKey>();
504+
505+
return GetUserKey(user.Result.Sid, throw_on_error);
425506
}
426507

427508
/// <summary>
@@ -431,7 +512,18 @@ public static NtKey GetCurrentUserKey()
431512
/// <exception cref="NtException">Thrown on error.</exception>
432513
public static NtKey GetRootKey()
433514
{
434-
return Open(@"\Registry", null, KeyAccessRights.MaximumAllowed);
515+
return GetRootKey(true).Result;
516+
}
517+
518+
/// <summary>
519+
/// Open the root key
520+
/// </summary>
521+
/// <returns>The opened key with the maximum access allowed.</returns>
522+
/// <param name="throw_on_error">True to throw on error.</param>
523+
/// <exception cref="NtException">Thrown on error.</exception>
524+
public static NtResult<NtKey> GetRootKey(bool throw_on_error)
525+
{
526+
return Open(@"\Registry", null, KeyAccessRights.MaximumAllowed, KeyCreateOptions.NonVolatile, throw_on_error);
435527
}
436528

437529
#endregion

0 commit comments

Comments
 (0)