title: Using EnumWindowsProc on C# Windows Service
body: <p>I've writed this code inside a Windows Service with an Administrator rights account:</p>
  
  <pre><code>private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
  [DllImport("USER32.DLL")]
  static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
  [DllImport("USER32.DLL")]
  static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int MaxCount);
  [DllImport("USER32.DLL")]
  static extern int GetWindowTextLength(IntPtr hWnd);
  [DllImport("USER32.DLL")]
  static extern bool IsWindowVisible(IntPtr hWnd);
  [DllImport("USER32.DLL")]
  static extern IntPtr GetShellWindow();
  
  private static IntPtr shellWindow = GetShellWindow();
  private static Dictionary&lt;intptr, string=""&gt; windows = new Dictionary&lt;intptr, string=""&gt;();
  
  private static bool _fEnumWindowsCallBack(IntPtr hWnd, int lParam)
  {
      // It never execute this code
      if (hWnd == shellWindow) return true;
      if (!IsWindowVisible(hWnd)) return true;
      int length = GetWindowTextLength(hWnd);
      if (length == 0) return true;
      StringBuilder builder = new StringBuilder(length);
      GetWindowText(hWnd, builder, length + 1);
      windows[hWnd] = builder.ToString();
      return true;
  }
  
  public static IDictionary&lt;intptr, string=""&gt; GetOpenWindows()
  {
      windows.Clear();
      EnumWindowsProc fEnumWindowsCallBack = new  EnumWindowsProc(_fEnumWindowsCallBack);
      EnumWindows(fEnumWindowsCallBack, 0);
      return windows;
  }
  </code></pre>
  
  <p>But it does'nt work (never executes the _fEnumWindowsCallBack function).</p>
  
  <p>Any idea why?</p>
  
  <p>I'm using W7 x64, Visual Studio 2012.</p>
  
  <p>Thanks.</p>