日期:2014-05-17  浏览次数:21077 次

C#服务获取不了当前激活窗口句柄
C# code

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();//获取当前激活窗口

//获取窗口标题
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd, //窗口句柄
StringBuilder lpString, //标题
int nMaxCount  //最大值
);
//获取类的名字
[DllImport("user32.dll")]
private static extern int GetClassName(
    IntPtr hWnd, //句柄
    StringBuilder lpString, //类名
    int nMaxCount //最大值
);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

private void getAllProcess(object sender, ElapsedEventArgs e)
        {
            allprocess = Process.GetProcesses();

            foreach (Process pro in allprocess)
            {                
                string pName = pro.ProcessName;
               
                if (pName == "QQ")
                {

                    try
                    {
                        
                        IntPtr formHandle = GetForegroundWindow();
                        StringBuilder title = new StringBuilder(256);
                        GetWindowText(formHandle, title, title.Capacity);//得到窗口的标题
                        StringBuilder className = new StringBuilder(256);
                        GetClassName(formHandle, className, className.Capacity);//得到窗口的句柄
                        
                        RECT rc = new RECT();
                        GetWindowRect(formHandle, ref rc);

                        string formName = className.ToString();

                        string fileSavePath = @"D:\image\qqimage/";
                        publicDll.logMsg("QQ:开始查找 " + className, logMsgType.Normal);
                        publicDll.logMsg("QQ:是否截图 " + title, logMsgType.Normal);     
                        if (formName.EndsWith("TXGuiFoundation"))
                        {
                            publicDll.logMsg("TXGuiFoundation:" + pName, logMsgType.Normal);
                            if (!System.IO.Directory.Exists(fileSavePath))
                            {
                                System.IO.Directory.CreateDirectory(fileSavePath);
                            }
                            GetImageFromScreen();//截图
                        }
                    }
                    catch (Exception ex)
                    {
                        publicDll.logMsg("Error:" + ex.Message, logMsgType.Normal);
                    }
                }
            }
        }



以上代码,我设定每隔2秒自动获取当前激活的窗口并截图保存,
在winform 中执行没问题,写成控制台程序执行也么问题,
但是写成服务形式就不行了,编译不会报错,但是
publicDll.logMsg("QQ:开始查找 " + , logMsgType.Normal);
publicDll.logMsg("QQ:是否截图 " + title, logMsgType.Normal);

日志发现 className title 为空导致(winform 或者控制台时候就不会),求大神看看。。。

难道服务不能用这个??

------解决方案--------------------
C# code
using System;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        private Boolean _bStart;

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();//获取当前激活窗口

        //获取窗口标题
        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
        IntPtr hWnd, //窗口句柄
        StringBuilder lpString, //标题
        int nMaxCount  //最大值
        );

        //获取类的名字
        [DllImport("user32.dll")]
        private static extern int GetClassName(
            IntPtr hWnd, //句柄
            StringBuilder lpString, //类名