Exemplo n.º 1
0
        /// <summary>
        /// Generate a DOS filename from a full filename.
        /// </summary>
        /// <param name="filename">The full filename.</param>
        /// <param name="allow_extended">True to allow extended characters.</param>
        /// <param name="iterations">Number of iterations of the algorithm to test.</param>
        /// <param name="throw_on_error">True throw on error.</param>
        /// <returns>The DOS filename.</returns>
        public static NtResult <string> Generate8dot3Name(string filename, bool allow_extended, int iterations, bool throw_on_error)
        {
            if (iterations <= 0)
            {
                throw new ArgumentException("Invalid iteration count.");
            }

            GenerateNameContext context = new GenerateNameContext()
            {
                NameBuffer      = new byte[16],
                ExtensionBuffer = new byte[8]
            };

            if (IsLegal8dot3Name(filename))
            {
                return(filename.ToUpper().CreateResult());
            }

            NtResult <string> result = default;

            for (int i = 0; i < iterations; ++i)
            {
                using (var name = new UnicodeStringAllocated(24)) {
                    result = NtRtl.RtlGenerate8dot3Name(new UnicodeString(filename),
                                                        allow_extended, ref context, name).CreateResult(throw_on_error, () => name.ToString());
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Query the image path from a process ID.
        /// </summary>
        /// <param name="pid">The ID of the process.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The image path.</returns>
        /// <remarks>This method can be called without any permissions on the process.</remarks>
        public static NtResult <string> GetProcessIdImagePath(int pid, bool throw_on_error)
        {
            var info = new SystemProcessIdInformation()
            {
                ProcessId = new IntPtr(pid)
            };

            using (var buffer = info.ToBuffer())
            {
                NtStatus status = _system_info_object.QueryInformation(
                    SystemInformationClass.SystemProcessIdInformation,
                    buffer, out int length);
                if (status.IsSuccess())
                {
                    return(new NtResult <string>(NtStatus.STATUS_SUCCESS, string.Empty));
                }
                if (status != NtStatus.STATUS_INFO_LENGTH_MISMATCH)
                {
                    return(status.CreateResultFromError <string>(throw_on_error));
                }

                using (var str = new UnicodeStringAllocated(buffer.Result.ImageName.MaximumLength))
                {
                    info = new SystemProcessIdInformation()
                    {
                        ProcessId = new IntPtr(pid), ImageName = str.String
                    };
                    return(Query(SystemInformationClass.SystemProcessIdInformation,
                                 info, throw_on_error).Map(r => r.ImageName.ToString()));
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Get the symbolic link target path.
 /// </summary>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The target path.</returns>
 public NtResult <string> GetTarget(bool throw_on_error)
 {
     using (UnicodeStringAllocated ustr = new UnicodeStringAllocated()) {
         return(NtSystemCalls.NtQuerySymbolicLinkObject(Handle, ustr,
                                                        out int return_length).CreateResult(throw_on_error, () => ustr.ToString()));
     }
 }
Exemplo n.º 4
0
 private NtResult <string> GetUserName(bool throw_on_error)
 {
     using (UnicodeStringAllocated str = new UnicodeStringAllocated(2048))
     {
         int length = NtSystemCalls.NtUserGetAtomName(Atom, str);
         if (length == 0)
         {
             return(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND.CreateResultFromError <string>(throw_on_error));
         }
         str.String.Length = (ushort)(length * 2);
         return(str.ToString().CreateResult());
     }
 }
Exemplo n.º 5
0
        private NtResult <string> GetClassName(bool real_name, bool throw_on_error)
        {
            using (var str = new UnicodeStringAllocated()) {
                int length = NtSystemCalls.NtUserGetClassName(Handle, real_name, str);
                if (length == 0)
                {
                    return(NtObjectUtils.CreateResultFromDosError <string>(throw_on_error));
                }

                str.String.Length = (ushort)(length * 2);

                return(str.ToString().CreateResult());
            }
        }
 public static extern NtStatus NtQuerySymbolicLinkObject(
     SafeHandle LinkHandle,
     [In, Out] UnicodeStringAllocated LinkTarget,
     out int ReturnedLength
     );
Exemplo n.º 7
0
 public static extern int NtUserGetClassName(IntPtr Window, [MarshalAs(UnmanagedType.Bool)] bool RealName, UnicodeStringAllocated Name);
 public static extern int NtUserGetAtomName(ushort Atom, UnicodeStringAllocated Name);