2014-11-24 65 views
0

我正在研究扫描我的活动进程的应用程序,并且在选择之后将关键命令传递给该进程。我遇到的问题是,并非我所有的流程似乎都有MainWindowTitle。下面是代码片段,我立即意识到了这个问题:对所有正在运行的进程不显示进程MainWindowTitle

Dictionary<int, string> procInfo = new Dictionary<int, string>(); 
Process[] processes = Process.GetProcesses(); 
foreach (Process procData in processes) 
{ 
    if (procData.MainWindowTitle.Length > 0)// || procData.ProcessName == "notepad++") 
    { 
     procInfo.Add(procData.Id, procData.ProcessName); 
    } 
} 

foreach (KeyValuePair<int, string> proc in procInfo) 
{ 
    lstProcesses.Items.Add(proc.Value + " (" + proc.Key.ToString() + ")"); 
} 

如果你看一下第5行,你会看到我在那里迫使记事本++到列表中,以测试对来自WinSpy我的成绩++(替代间谍++)并且没有它拒绝显示的力量,因为它的MainWindowTitle属性是空的。

没有MainWindowTitle我无法为应用程序获取类名称:

//procID set earlier and shown to be working 
Process proc = Process.GetProcessById(procID); 
string winTitle = proc.MainWindowTitle; 

IntPtr hWnd = proc.MainWindowHandle; 
StringBuilder buffer = new StringBuilder(1024); 
GetClassName(hWnd, buffer, buffer.Capacity); 
string winCaption = buffer.ToString(); 

这样我就不能定位到应用程序中的关键命令传递:

//winCaption currently blank, winTitle tested and working 
IntPtr appHandler = FindWindow(winCaption, winTitle); 
SetForegroundWindow(appHandler); 
SendKeys.SendWait("things and junk"); 

我有为我的项目设置适当的DllImports,所以问题不存在,我还没有在这里找到答案或任何可靠的巡航其他互联网。我是否在代码中丢失了某些东西,或者这是不好的,我应该感觉不好吗?

+0

可能的重复[为什么Process.MainWindowTitle总是空的除了一个窗口?](http://stackoverflow.com/questions/10063847/why-is-process-mainwindowtitle-always-empty-for-all-但一个窗口) – MethodMan 2014-11-24 20:40:41

+0

我发现答案,虽然这是类似的,但我的问题遍及我的所有进程,而不仅仅是IE。 – 2014-11-24 21:10:15

回答

0

我不得不赞成的P/Invoke的解决方案完全取消上述选项:http://pinvoke.net/default.aspx/user32.EnumDesktopWindows

趁着GetWindowTextGetWindowThreadProcessIDGetClassName在一个新的功能:

//the following was jacked from: http://pinvoke.net/default.aspx/user32.EnumDesktopWindows 
var procCollection = new List<string>(); 
//Dictionary<string, int> procCollection = new Dictionary<string, int>(); 
EnumDelegate filter = delegate(IntPtr hWnd, int lParam) 
{ 
    //return window titles 
    StringBuilder strbTitle = new StringBuilder(255); 
    int nLength = GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); 
    string winTitle = strbTitle.ToString(); 

    //return thread process id 
    uint getID = 0; 
    GetWindowThreadProcessId(hWnd, ref getID); 
    int winID = Convert.ToInt32(getID); 

    //return class names 
    StringBuilder strbClass = new StringBuilder(255); 
    GetClassName(hWnd, strbClass, strbClass.Capacity+1); 
    string winClass = strbClass.ToString(); 

    if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(winTitle) == false) 
    { 
     procCollection.Add(winTitle+" -- "+winID+" -- "+winClass); 
    } 
    return true; 
}; 

if (EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)) 
{ 
    //foreach (KeyValuePair<string, int> procInfo in procCollection) 
    foreach(string procData in procCollection) 
    { 
     //if (procInfo.Key != "Start" && procInfo.Key != "Program Manager") 
     if (procData.Contains("Start") == false && procData.Contains("Program Manager") == false) 
     { 
      lstProcesses.Items.Add(procData); 
     } 
    } 
} 

可以让我我需要的一切建立我的开放式Windows列表。这并没有给我像WinSpy ++(Spy ++)这样的进程列表,但这正是我需要的。

相关问题