Exemplo n.º 1
0
        /// <summary>
        /// Obtains the executable name of the process
        /// </summary>
        /// <param name="processID"></param>
        /// <param name="throwExException"></param>
        /// <returns></returns>
        public static string GetExecutableName(int processID, bool throwExException)
        {
            Exception error  = null;
            var       result = new StringBuilder(65536);
            var       len    = result.Capacity;

            if ((Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version >= new Version("6.0")))
            {
                var processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_INFORMATION, false, processID);
                if (processHandle == IntPtr.Zero)
                {
                    //try limited
                    processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_LIMITED_INFORMATION, false, processID);
                }
                if (processHandle == IntPtr.Zero)
                {
                    error = new Win32ErrorException();
                }
                else if (!KERNEL32.SafeNativeMethods.QueryFullProcessImageName(processHandle, 0, result, ref len))
                {
                    error = new Win32ErrorException();
                }
                KERNEL32.SafeNativeMethods.CloseHandle(processHandle);
            }
            else
            {
                var processHandle = KERNEL32.SafeNativeMethods.OpenProcess(ACCESS_PROCESS.QUERY_INFORMATION | ACCESS_PROCESS.VM_READ, false, processID);
                if (processHandle == IntPtr.Zero)
                {
                    error = new Win32ErrorException();
                }
                else
                {
                    len = PSAPI.GetModuleFileNameEx(processHandle, IntPtr.Zero, result, len);
                    if (len == 0)
                    {
                        error = new Win32ErrorException();
                    }
                }
                KERNEL32.SafeNativeMethods.CloseHandle(processHandle);
            }
            if (throwExException && (error != null))
            {
                throw error;
            }
            return(result.ToString());
        }
Exemplo n.º 2
0
 /// <summary>
 /// updates the process id list and returns the number of processes in the list
 /// </summary>
 /// <returns></returns>
 public int Update()
 {
     if (!PSAPI.EnumProcesses(m_ListPointer, 32768, out var len))
     {
         if (len > m_Size)
         {
             m_Size = len * 2;
             Marshal.FreeHGlobal(m_ListPointer);
             m_ListPointer = Marshal.AllocHGlobal(m_Size);
             return(Update());
         }
         throw new Win32ErrorException();
     }
     m_Count = len / 4;
     m_Items = null;
     return(m_Count);
 }