/// <summary> /// Open an NT object with a specified type. /// </summary> /// <param name="typename">The name of the type to open (e.g. Event). If null the method will try and lookup the appropriate type.</param> /// <param name="path">The path to the object to open.</param> /// <param name="root">A root directory to open from.</param> /// <param name="access">Generic access rights to the object.</param> /// <returns>The opened object.</returns> /// <exception cref="NtException">Thrown if an error occurred opening the object.</exception> /// <exception cref="ArgumentException">Thrown if type of resource couldn't be found.</exception> public static NtObject OpenWithType(string typename, string path, NtObject root, GenericAccessRights access) { if (typename == null) { typename = NtDirectory.GetDirectoryEntryType(path, root); if (typename == null) { throw new ArgumentException(String.Format("Can't find type for path {0}", path)); } } switch (typename.ToLower()) { case "device": return(NtFile.Open(path, root, (FileAccessRights)access, FileShareMode.None, FileOpenOptions.None)); case "file": return(NtFile.Open(path, root, (FileAccessRights)access, FileShareMode.Read | FileShareMode.Write | FileShareMode.Delete, FileOpenOptions.None)); case "event": return(NtEvent.Open(path, root, (EventAccessRights)access)); case "directory": return(NtDirectory.Open(path, root, (DirectoryAccessRights)access)); case "symboliclink": return(NtSymbolicLink.Open(path, root, (SymbolicLinkAccessRights)access)); case "mutant": return(NtMutant.Open(path, root, (MutantAccessRights)access)); case "semaphore": return(NtSemaphore.Open(path, root, (SemaphoreAccessRights)access)); case "section": return(NtSection.Open(path, root, (SectionAccessRights)access)); case "job": return(NtJob.Open(path, root, (JobAccessRights)access)); case "key": return(NtKey.Open(path, root, (KeyAccessRights)access)); default: throw new ArgumentException(String.Format("Can't open type {0}", typename)); } }
/// <summary> /// Attempt to convert an NT device filename to a DOS filename. /// </summary> /// <param name="filename">The filename to convert.</param> /// <returns>The converted string. Returns a path prefixed with GLOBALROOT if it doesn't understand the format.</returns> public static string NtFileNameToDos(string filename) { if (!filename.StartsWith(@"\")) { return(filename); } if (filename.StartsWith(@"\??\UNC\", StringComparison.OrdinalIgnoreCase)) { return(@"\\" + filename.Substring(8)); } else if (filename.StartsWith(@"\??\")) { return(@"\\." + filename.Substring(3)); } else if (filename.StartsWith(@"\Device\")) { for (char drive = 'A'; drive <= 'Z'; drive++) { using (var link = NtSymbolicLink.Open($@"\??\{drive}:", null, SymbolicLinkAccessRights.Query, false)) { if (!link.IsSuccess) { continue; } var target = link.Result.GetTarget(false); if (!target.IsSuccess || target.Result.Length == 0) { continue; } if (filename.StartsWith($@"{target.Result}\")) { return($"{drive}:" + filename.Substring(target.Result.Length)); } } } } return(@"\\.\GLOBALROOT" + filename); }
protected override sealed NtResult <NtSymbolicLink> OpenInternal(ObjectAttributes obj_attributes, SymbolicLinkAccessRights desired_access, bool throw_on_error) { return(NtSymbolicLink.Open(obj_attributes, desired_access, throw_on_error)); }