0

I'm currently developing an app that uses System.Diagnostics.Process to get the Window Title of the Current Active App or the App on the ForeGround. Now, this is fine and working with the current code I have until I came across with this issue. This NullReferenceException occurs if I switch my app using the task bar icon instead of the minimized button of the app window.

Edit: It maybe confusing if I don't raise this up. So basically NullReferenceException is caused by (title.Equals(System.Diagnostics.Process.GetCurrentProcess().MainWindowTitle))

title variable is null due to

String title = GetActiveWindowTitle();

GetActiveWindowTitle() returns null. And this uses Window.GetForegroundWindow(); And for some reason using taskbar icons to switch up, this Window.GetForegroundWindow(); method returns nothing.

enter image description here

enter image description here

Originally I used this

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

To get what I need. But I'm still having the same issue when switching app using the task bar icon. The only difference with using this code is that the exception is this A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

My operating system is Windows 8.1.

Any inputs is greatly appreciated.

Update The answer from @KcDoD was somewhat correct. And since the NullReferenceException was actually part of my question I will mark it as the Answer. And it also guides me a lot to figure out what's going on.

Basically, on my application I call this method GetActiveWindowTitle() within WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime). WinEventProc() is a delegate method that triggers when you switch window. And it turns out that by calling GetForegroundWindow(); or GetActiveWindowTitle() the process is not active yet that's why when it goes to get the Active application or Process it gives/return null.

Best regards

mr.b
  • 2,708
  • 8
  • 39
  • 64

1 Answers1

0

Your Problem is related to the String you are trying to Equal with MainWindowTitle . That means the title variable/field.

You should check tite for null before trying to use Equal

Additionally : Here is a program using answer from here

  static void Main(string[] args)
  {
      String answ;

      while (true)
      {
           answ = GetActiveWindowTitle();
           if (answ == null)
           {
               Console.WriteLine("NO active program");
           }
           else {
              Console.WriteLine(answ);
           }
        Thread.Sleep(1000);
       }

 }


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

 [DllImport("user32.dll")]
 static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

 static private string GetActiveWindowTitle()
 {
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    IntPtr handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
     }
     return null;
  }

Answer : If there is no process selected then this will return null. I think that's the answer for your question.

Community
  • 1
  • 1
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
  • 1
    Yes I agree with you, if we focus on the exception but the root cause of it is that on my code String title = GetActiveWindowTitle(); That methods returns null. – mr.b Feb 28 '15 at 07:30
  • I think you are using this : http://stackoverflow.com/questions/115868/how-do-i-get-the-title-of-the-current-active-window-using-c And you can see that it retun null for falure case :) – Kavindu Dodanduwa Feb 28 '15 at 07:34
  • Not exactly! But Yea- pretty much the same codes and to what I'm trying to do. Using the codes from the link produces the same issue. – mr.b Feb 28 '15 at 07:39
  • Strange , I added a small program I just created,, it return current window even when I switch programs using task bar – Kavindu Dodanduwa Feb 28 '15 at 07:41
  • hmmmm- strange, what kind of app did you put this on? console/windows service/windows forms? Cause I think that will matter. Mine is windows forms. – mr.b Feb 28 '15 at 07:43
  • @Shen Lance Check updated answer :) You will get a NULL when you don't have any selected process :) i think that's your answer – Kavindu Dodanduwa Feb 28 '15 at 07:45
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71933/discussion-between-shen-lance-and-kcdod). – mr.b Feb 28 '15 at 07:47