C# .NET - MainWindowTitle returns empty - Asked By mostafa hamdy on 06-Dec-10 06:39 AM

Hello all
I have some desktop application built using C# 3.5 and run the application under win 7 , I have some process which catches the exe of this desktop application , but when I try to get its Mainwindow title it returns empty , the desktop application title is "MyApplication fires" although that I get it empty the code I did is as the following:

foreach (Process ps in Process.GetProcessesByName(Tile))


if (ps.MainWindowTitle.ToLower()==Title.ToLower())





please if any body get what I mean and can help me please send me or tell me about some URL may help me in solving it

Anoop S replied to mostafa hamdy on 06-Dec-10 09:49 AM
GetWindowText is one of those special functions that does work with other applications--under the covers WM_GETTEXT marshals the memory.  For example: the following does retrieve the caption text for a Notepad window:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Security;
namespace Application
  public class Program
    public static void Main ( )
      IntPtr hwnd = UnsafeNativeMethods.FindWindow("Notepad", null);
      StringBuilder stringBuilder = new StringBuilder(256);
      UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);
  internal static class UnsafeNativeMethods
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    internal static extern int GetWindowText ( IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount );
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr FindWindow ( string lpClassName, string lpWindowName );
Rolf Jaeger replied to mostafa hamdy on 06-Dec-10 09:17 PM
Hi Mustafa:

the GetProcessesByName method requires the name of the PROCESS, not the title of the main window. Please give the code listed below a try and see whether it might work for you.

Hope this helped,

foreach (Process p in Process.GetProcesses())
  if (p.MainWindowTitle.ToLower() == Title.ToLower())