Ejemplo n.º 1
0
        /// <summary>
        /// Faster than the version that takes a pid.
        /// </summary>
        public static string GetProcessName(IntPtr hProcess)
        {
            // Get the handle to the first module in the process.
            IntPtr[] modules = new IntPtr[1];
            uint     needed  = 0;

            if (!Psapi.EnumProcessModules(hProcess, modules, (uint)IntPtr.Size, ref needed))
            {
                return(null);
            }
            if (needed == 0)
            {
                return(null);
            }
            IntPtr firstModule = modules[0];

            // Get the filename for the module.
            uint          bufsize       = Kernel32.MAX_PATH * 4;
            StringBuilder filename      = new StringBuilder((int)bufsize);
            uint          charsReturned = Psapi.GetModuleFileNameEx(hProcess, firstModule, filename, bufsize);

            if (charsReturned == 0)
            {
                return(null);
            }

            return(Path.GetFileName(filename.ToString()));
        }
    public static string GetMainModuleFileName(this Process process, int buffer = 1024)
    {
        var  fileNameBuilder = new StringBuilder(buffer);
        uint code            = Psapi.GetModuleFileNameEx(process.Handle, IntPtr.Zero, fileNameBuilder, (uint)fileNameBuilder.Capacity + 1);

        return(code != 0 ?
               fileNameBuilder.ToString() :
               null);
    }
Ejemplo n.º 3
0
 public string GetWindowName()
 {
     try
     {
         User32.GetWindowThreadProcessId(Handle, out var lpdwProcessId);
         var handle = Kernel32.OpenProcess(0x0410, false, lpdwProcessId);
         var text   = new StringBuilder(1000);
         Psapi.GetModuleFileNameEx(handle, IntPtr.Zero, text, text.Capacity);
         Kernel32.CloseHandle(handle);
         return(text.ToString());
     }
     catch (Exception ex)
     {
         Log.LogEvent("Window", $"Handle: {Handle}\nCaption: {GetWindowText()}", ex);
         return("");
     }
 }