我如何获得Process.MainMoudle.FileName?

时间:2015-07-08 07:12:35

标签: c# .net winforms

我有这堂课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;

namespace HardwareMonitoring
{


    public class GetProcesses: IWin32Window

    {
        internal IntPtr handle;
        internal String title;
        internal String filename;

        public IntPtr Handle
        {
            get { return handle; }
        }

        public String Title
        {
            get { return title; }
        }

        public String FileName
        {
            get { return filename; }
        }

        public static readonly Int32 GWL_STYLE = -16;
        public static readonly UInt64 WS_VISIBLE = 0x10000000L;
        public static readonly UInt64 WS_BORDER = 0x00800000L;
        public static readonly UInt64 DESIRED_WS = WS_BORDER | WS_VISIBLE;

        public delegate Boolean EnumWindowsCallback(IntPtr hwnd, Int32 lParam);

        public static List<GetProcesses> GetAllWindows()
        {
            List<GetProcesses> windows = new List<GetProcesses>();
            StringBuilder buffer = new StringBuilder(100);
            EnumWindows(delegate(IntPtr hwnd, Int32 lParam)
            {
                if ((GetWindowLongA(hwnd, GWL_STYLE) & DESIRED_WS) == DESIRED_WS)
                {
                    GetWindowText(hwnd, buffer, buffer.Capacity);
                    GetProcesses wnd = new GetProcesses();
                    wnd.handle = hwnd;
                    wnd.title = buffer.ToString();
                    windows.Add(wnd);
                }
                return true;
            }, 0);

            return windows;
        }

        [DllImport("user32.dll")]
        static extern Int32 EnumWindows(EnumWindowsCallback lpEnumFunc, Int32 lParam);

        [DllImport("user32.dll")]
        public static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);

        [DllImport("user32.dll")]
        static extern UInt64 GetWindowLongA(IntPtr hWnd, Int32 nIndex);
    }
}

在form1中:

foreach (var wnd in GetProcesses.GetAllWindows())
            {

            }

使用wnd我可以获得进程的句柄和标题,但我还需要获取FileName。

如果我在做:

foreach (Process p in Process.GetProcesses()) 

然后我可以做:

p.MainModule.FileName

如何在我的类中获取FileName属性?我在那里添加了属性但是如何获得它的值?

修改

在课堂底部,我补充道:

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

然后在课堂上我用它:

wnd.processid = GetWindowThreadProcessId(wnd.handle, IntPtr.Zero);

在form1中:

    string ff = "";
    void PopulateApplications()
    {

        foreach (var wnd in GetProcesses.GetAllWindows())
        {
            uint uuu = wnd.ProcessId;
            ff = Process.GetProcessById((int)uuu).MainModule.FileName;
        }
    }

但是当它正在行时:

ff = Process.GetProcessById((int)uuu).MainModule.FileName;

它只会退出程序。没有错误或例外。

修改

在课堂底部:

[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

然后在Form1中使用它:

    int ff = 0;
    List<string> FileNames = new List<string>();
    void PopulateApplications()
    {

        foreach (var wnd in GetProcesses.GetAllWindows())
        {
            GetProcesses.GetWindowThreadProcessId(wnd.handle, out ff);
            FileNames.Add(Process.GetProcessById(ff).MainModule.FileName);
        }
    }

我只获得了18个进程FileNames。

0 个答案:

没有答案