-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.
谢谢。