Windows Service(Win 7)获取活动窗口() [英] Windows Service(Win 7) Get Active Window()

查看:106
本文介绍了Windows Service(Win 7)获取活动窗口()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有类似这样的代码部分:

I have a code part like this in my project :

private void GetActiveWindow()
        {
           
            const int cChars = 256;
            int ihandler = 0;
            StringBuilder sBuff = new StringBuilder(cChars);
            ihandler = GetForegroundWindow();
              StreamWriter sw;
            if (File.Exists("C:\\Log.txt"))
            {
                sw = File.AppendText("C:\\Log.txt");
                sw.WriteLine(sBuff.ToString() + "->" + ihandler.ToString());
                sw.WriteLine("-----------");
                sw.Close();
            }
            else
            {
                sw = new StreamWriter("C:\\Log.txt");
                sw.WriteLine(sBuff.ToString() + "->" + ihandler.ToString());
                sw.WriteLine("-----------");
                sw.Close();
            }
}


它的值总是为0,我无法达到活动窗口的名称.我使用Windows7.这是Windows服务项目,我该怎么办? ihandler总是为0,所以我无法到达名称.


It''s value always getting 0 and i cant reach active window''s name.Iam using Windows 7.And this is a windows service project what should i do ? ihandler always getting 0 so i cant reach name.

推荐答案

我认为,作为Windows服务运行时,这种方式永远不会奏效.首先,7中的服务根本无法与桌面进行交互.除此之外,无论有多少用户登录,他们都可以运行,如果有多个用户登录(或没有登录),它应该返回什么?
I don''t think that should ever work when running as a windows service. To start with, services in 7 can''t interact with the desktop at all. In addition to that they can run regardless of how many users are logged in, so what should it return if there are multiple users logged in (or none)? Which session should it return the foreground window for?


调试您的GetForegroundWindow方法.

这是有关"如何调试Windows Service应用程序"的信息.
http://msdn.microsoft.com/en-us/library/7a50syb3%28v = VS.90%29.aspx
Debug your GetForegroundWindow Method.

Here is a information on "How to Debug Windows Service Application".
http://msdn.microsoft.com/en-us/library/7a50syb3%28v=VS.90%29.aspx


您无法做自己想做的事.在Windows 7下,所有服务都在与用户会话(会话1及更高版本)不同的会话(会话0)下运行.这意味着您的服务代码根本无法与用户桌面进行交互.保持GetForegroundWindow返回0并不奇怪.会话0中没有前台窗口,因为没有UI正在显示.
You can not do what you want. Under Windows 7 all services run under a different Session (session 0) than the user Session (session 1 and above). That means that your service code cannot interact with the users desktop at all. It''s no surprise you keep getting 0 returned by GetForegroundWindow. There is no foreground window in Session 0 because there''s no UI being displayed.


这篇关于Windows Service(Win 7)获取活动窗口()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆