2015-02-08 62 views
-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.

謝謝。

回答

0

我的假設是因爲您的Windows服務未在交互式會話中運行。沒有窗口可以計算。

docs(重點煤礦):

最低支持的客戶端

Windows 2000專業版[桌面應用程序僅]

最低支持的服務器

Windows 2000 Server的[僅適用於桌面應用]