-2
我所著Windows服務在這個代碼使用管理員權限帳戶使用EnumWindowsProc:C#的Windows服務
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<intptr, string=""> windows = new Dictionary<intptr, string="">();
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<intptr, string=""> GetOpenWindows()
{
windows.Clear();
EnumWindowsProc fEnumWindowsCallBack = new EnumWindowsProc(_fEnumWindowsCallBack);
EnumWindows(fEnumWindowsCallBack, 0);
return windows;
}
但它一點兒也不作品(從未執行_fEnumWindowsCallBack功能)。
任何想法爲什麼?
我正在使用W7 x64,Visual Studio 2012.
謝謝。