簡體   English   中英

如何在 C# 中獲取活動進程名稱?

[英]How to Get Active Process Name in C#?

如何在 C# 中獲取活動進程名稱?

我知道我必須使用這段代碼:

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

但我不知道如何使用它。

this answer中所述,您必須使用GetWindowThreadProcessId()獲取 window 的進程ID,然后您可以使用Process

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

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

string GetActiveProcessFileName()
{
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    Process p = Process.GetProcessById((int)pid);
    p.MainModule.FileName.Dump();
}

請注意,當活動進程是 64 位時,從 32 位應用程序運行時,這似乎會引發異常(“32 位進程無法訪問 64 位進程的模塊”)。

編輯:正如 Damien 指出的那樣,這段代碼容易出現競爭條件,因為在調用GetForegroundWindow()時具有活動 window 的進程在調用GetWindowThreadProcessId()時可能不再存在。 更糟糕的情況是,如果當時將相同的 hwnd 分配給另一個 window,但我想這應該很少見。

我建議使用System.Diagnostics.Process

var currentProc = System.Diagnostics.Process.GetCurrentProcess();
string name = currentProc.ProcessName;

作為替代方案,您可以使用:

string name = currentProc.MainModule.FileName;

它只需要兩行代碼,你可以使用 linq 來獲取所有進程。

var processss = from proc in System.Diagnostics.Process.GetProcesses() orderby proc.ProcessName ascending select proc;
foreach (var item in processss) {
    Console.WriteLine(item.ProcessName );
}

現在您只需在線即可擁有所有活動進程。

這是一個描述您想要做的確切事情的鏈接:

http://www.blackwasp.co.uk/GetActiveProcess.aspx

另一個描述GetForegroundWindow function,我在下面復制。

請注意,您可能需要引用一些額外的程序集才能使此代碼正常工作。 查看每個 function 的 MSDN。 例如, GetProcessesByName需要 System.Diagnostics。

public ApplicationState AppState
{
    get
    {
        Process[] processCollection =
                           Process.GetProcessesByName(ProcessName);
        if(processCollection != null && 
           processCollection.Length  >= 1 && 
            processCollection[0] != null)
        {
            IntPtr activeWindowHandle = Win32.GetForegroundWindow();
            // Optional int ProcessID;
            // Optional Win32.GetWindowThreadProcessId(
                                                 GetForegroundWindow(), 
                                                 out ProcessID)
            foreach(Process wordProcess in processCollection)
            {
                //Optional if( ProcessID == wordProcess.Id )
                //          return ApplicationState.Focused;
                if(wordProcess.MainWindowHandle == activeWindowHandle)
                {
                    return ApplicationState.Focused;
                }
            }

            return ApplicationState.Running;
        }

        return ApplicationState.NotRunning;
    }
} 
    public void GetProcessNames()
    {
        List<string> windowNames = new List<string>();

        foreach (Process window in Process.GetProcesses())
        {
            if (window.MainWindowHandle != IntPtr.Zero)
            {                    
                windowNames.Add(window.MainWindowTitle);
            }

            // It's that simple
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM