I'm working on an application that scans my active processes, and after selection, passes key commands to that process. The problem I'm having is that not all my processes seem to have a MainWindowTitle
. Below is the code snippit where I immediately recognized this problem:
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() + ")");
}
If you look at line 5, you'll see where I was forcing Notepad++ into the list to test against my results from WinSpy++ (alternative to Spy++) and without the force it refuses to display as it's MainWindowTitle
property is blank.
Without the MainWindowTitle
I can't get the class name for the application:
//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();
And thus I can't target the application to pass in key commands:
//winCaption currently blank, winTitle tested and working
IntPtr appHandler = FindWindow(winCaption, winTitle);
SetForegroundWindow(appHandler);
SendKeys.SendWait("things and junk");
I have the proper DllImports
setup for my project so the problem isn't there and I haven't found an answer here or anything reliable cruising the rest of the interwebs. Am I missing something in my code, or is this just bad and should I feel bad?