c# - Cannot get the top window or list of all windows -


the following piece of code in window service, supposed output text file, firstly name of top window, list of visible windows. process occurs when service first starts.

unfortunately doesn't spit out anything, outputtopwindow doesn't output anything, have expected "computer management" top window have start service computer management.

listallvisiblewindows not return either. have several windows visible when outputwindowinfo called, running service locally on win7 64bit.

why occurring?

public void outputwindowinfo() {     outputtopwindow();     listallvisiblewindows(); }  [dllimport("user32.dll")] static extern intptr getforegroundwindow();  [dllimport("user32.dll")] static extern int getwindowtext(intptr hwnd, stringbuilder text, int count);  [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] public static extern bool iswindowvisible(intptr hwnd);  [dllimport("user32.dll", entrypoint = "enumdesktopwindows", exactspelling = false, charset = charset.auto, setlasterror = true)] public static extern bool enumdesktopwindows(intptr hdesktop, enumdelegate lpenumcallbackfunction, intptr lparam);   private void outputtopwindow() {     const int nchars = 256;     stringbuilder buff = new stringbuilder(nchars);     intptr handle = getforegroundwindow();      if (getwindowtext(handle, buff, nchars) > 0)     {         writemessagelog( buff.tostring() );     } }  public delegate bool enumdelegate(intptr hwnd, int lparam);  public static void listallvisiblewindows() {     list<string> collection = new list<string>();     enumdelegate filter = delegate(intptr hwnd, int lparam)     {         stringbuilder strbtitle = new stringbuilder(255);         int nlength = getwindowtext(hwnd, strbtitle, strbtitle.capacity + 1);         string strtitle = strbtitle.tostring();          bool winvis = iswindowvisible(hwnd);         bool emptytitle = string.isnullorempty(strtitle);         writemessagelog(winvis + " " + emptytitle + " " + strtitle);         if (winvis && emptytitle == false)         {             collection.add(strtitle);         }         return true;     };      if (enumdesktopwindows(intptr.zero, filter, intptr.zero))     {         foreach (var item in collection)         {             writemessagelog(item);         }     } } public static void writemessagelog(string message) {     streamwriter sw = null;     try     {         sw = new streamwriter(appdomain.currentdomain.basedirectory + "\\logfile.txt", true);         sw.writeline(datetime.now.tostring() + ": " + message + ": ");         sw.flush();         sw.close();     }     catch { } } 

in listallvisiblewindows() have output of finds writemessagelog(winvis + " " + emptytitle + " " + strtitle);, result:

false false .net-broadcasteventwindow.4.0.0.0.bf7771.0:  false false hid input service:  false false bmonbrw1c3e84b6fe08:  false false internal window:  false true :  false false wmi provider host:  false true :  false false pdfsvc:  false true :  false true :  false false pnpwindow:  false false vcs event handler:900:  false true :  false false vcs event handler:1632: 

these windows have open: 2 windows explorer, recycle bin, firefox, computer management, visual studio, vs command prompt, notepad

it isn't working because default since windows vista, services run in separate session:

in windows® xp, windows server® 2003, , earlier versions of windows operating system, services run in same session first user logs on console. session called session 0. running services , user applications in session 0 poses security risk because services run @ elevated privilege , therefore targets malicious agents looking way elevate own privilege level.

in windows vista®, windows server 2008, , later versions of windows, operating system mitigates security risk isolating services in session 0 , making session 0 noninteractive. system processes , services run in session 0. first user logs on session 1, , subsequent users log on subsequent sessions. means services never run in same session users’ applications , therefore protected attacks originate in application code [source]

for legacy compatibility, it possible change settings services can interact desktop bad idea a security point of view - if can restructure doesn't have service @ all, best.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -