在服务器桌面会话上捕获屏幕

2024-05-14

我开发了一个 GUI 测试框架,可以按计划对我们公司网站进行集成测试。当出现问题时,它会截取桌面等内容。它在专用 Windows Server 2008 上的登录用户上运行,无人值守。

问题是在我已断开远程桌面会话连接的桌面上截取屏幕截图。我得到以下异常:

System.ComponentModel.Win32Exception (0x80004005): The handle is invalid     
at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)     
at System.Drawing.Graphics.CopyFromScreen(Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize)     
at IntegrationTester.TestCaseRunner.TakeScreenshot(String name) in C:\VS2010\IntegrationTester\IntegrationTester\Config\TestCaseRunner.cs:line 144     
at IntegrationTester.TestCaseRunner.StartTest() in C:\VS2010\IntegrationTester\IntegrationTester\Config\TestCaseRunner.cs:line 96

The 截屏()方法如下所示:

public static void TakeScreenshot(string name)
        {
            var bounds = Screen.GetBounds(Point.Empty);
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                bitmap.Save("someFileName", ImageFormat.Jpeg);
            }
        }

我已确保屏幕保护程序设置为“无”且没有超时。 我还实现了一段代码,可以执行几次 pinvokes发送鼠标移动,希望它能生成桌面图形句柄..但没有。

IntPtr hWnd = GetForegroundWindow();
if (hWnd != IntPtr.Zero)
    SendMessage(hWnd, 0x200, IntPtr.Zero, IntPtr.Zero);

任何建议表示赞赏。


为了捕获屏幕,您需要在用户会话中运行程序。这是因为如果没有用户,就无法关联桌面。

为了解决这个问题,您可以运行桌面应用程序来拍摄图像,可以在活动用户的会话中调用该应用程序,这可以通过服务来完成。

下面的代码允许您调用桌面应用程序,使其在本地用户的桌面上运行。

如果需要以特定用户身份执行,请查看文章中的代码允许服务与桌面交互吗?哎哟。 http://asprosys.blogspot.com/2009/03/allow-service-to-interact-with-desktop.html。您还可以考虑使用该功能登录用户 http://msdn.microsoft.com/en-us/library/windows/desktop/aa378184%28v=vs.85%29.aspx.

代码:

public void Execute()
{
    IntPtr sessionTokenHandle = IntPtr.Zero;
    try
    {
        sessionTokenHandle = SessionFinder.GetLocalInteractiveSession();
        if (sessionTokenHandle != IntPtr.Zero)
        {
            ProcessLauncher.StartProcessAsUser("Executable Path", "Command Line", "Working Directory", sessionTokenHandle);
        }
    }
    catch
    {
        //What are we gonna do?
    }
    finally
    {
        if (sessionTokenHandle != IntPtr.Zero)
        {
            NativeMethods.CloseHandle(sessionTokenHandle);
        }
    }
}

internal static class SessionFinder
{
    private const int INT_ConsoleSession = -1;

    internal static IntPtr GetLocalInteractiveSession()
    {
        IntPtr tokenHandle = IntPtr.Zero;
        int sessionID = NativeMethods.WTSGetActiveConsoleSessionId();
        if (sessionID != INT_ConsoleSession)
        {
            if (!NativeMethods.WTSQueryUserToken(sessionID, out tokenHandle))
            {
                throw new System.ComponentModel.Win32Exception();
            }
        }
        return tokenHandle;
    }
}

internal static class ProcessLauncher
{
    internal static void StartProcessAsUser(string executablePath, string commandline, string workingDirectory, IntPtr sessionTokenHandle)
    {
        var processInformation = new NativeMethods.PROCESS_INFORMATION();
        try
        {
            var startupInformation = new NativeMethods.STARTUPINFO();
            startupInformation.length = Marshal.SizeOf(startupInformation);
            startupInformation.desktop = string.Empty;
            bool result = NativeMethods.CreateProcessAsUser
            (
                sessionTokenHandle,
                executablePath,
                commandline,
                IntPtr.Zero,
                IntPtr.Zero,
                false,
                0,
                IntPtr.Zero,
                workingDirectory,
                ref startupInformation,
                ref processInformation
            );
            if (!result)
            {
                int error = Marshal.GetLastWin32Error();
                string message = string.Format("CreateProcessAsUser Error: {0}", error);
                throw new ApplicationException(message);
            }
        }
        finally
        {
            if (processInformation.processHandle != IntPtr.Zero)
            {
                NativeMethods.CloseHandle(processInformation.processHandle);
            }
            if (processInformation.threadHandle != IntPtr.Zero)
            {
                NativeMethods.CloseHandle(processInformation.threadHandle);
            }
            if (sessionTokenHandle != IntPtr.Zero)
            {
                NativeMethods.CloseHandle(sessionTokenHandle);
            }
        }
    }
}

internal static class NativeMethods
{
    [DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    internal static extern bool CloseHandle(IntPtr handle);

    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern bool CreateProcessAsUser(IntPtr tokenHandle, string applicationName, string commandLine, IntPtr processAttributes, IntPtr threadAttributes, bool inheritHandle, int creationFlags, IntPtr envrionment, string currentDirectory, ref STARTUPINFO startupInfo, ref PROCESS_INFORMATION processInformation);

    [DllImport("Kernel32.dll", EntryPoint = "WTSGetActiveConsoleSessionId")]
    internal static extern int WTSGetActiveConsoleSessionId();

    [DllImport("WtsApi32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool WTSQueryUserToken(int SessionId, out IntPtr phToken);

    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr processHandle;
        public IntPtr threadHandle;
        public int processID;
        public int threadID;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct STARTUPINFO
    {
        public int length;
        public string reserved;
        public string desktop;
        public string title;
        public int x;
        public int y;
        public int width;
        public int height;
        public int consoleColumns;
        public int consoleRows;
        public int consoleFillAttribute;
        public int flags;
        public short showWindow;
        public short reserverd2;
        public IntPtr reserved3;
        public IntPtr stdInputHandle;
        public IntPtr stdOutputHandle;
        public IntPtr stdErrorHandle;
    }
}

此代码是对文章中找到的代码的修改允许服务与桌面交互吗?哎哟。 http://asprosys.blogspot.com/2009/03/allow-service-to-interact-with-desktop.html(必读)


附录:

上面的代码允许在本地登录到计算机的用户的桌面上执行程序。此方法特定于当前本地用户,但可以为任何用户执行此操作。检查文章中的代码允许服务与桌面交互吗?哎哟。 http://asprosys.blogspot.com/2009/03/allow-service-to-interact-with-desktop.html举个例子。

这个方法的核心是函数创建进程为用户 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682429%28v=vs.85%29.aspx,您可以在以下位置找到更多信息:MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ms682429%28v=vs.85%29.aspx.

Replace "Executable Path"以及要运行的可执行文件的路径。代替"Command Line"使用作为执行参数传递的字符串,并替换"Working Directory"与您想要的工作目录。例如,您可以提取可执行路径的文件夹:

    internal static string GetFolder(string path)
    {
        var folder = System.IO.Directory.GetParent(path).FullName;
        if (!folder.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
        {
            folder += System.IO.Path.DirectorySeparatorChar;
        }
        return folder;
    }

如果您有服务,则可以在服务中使用此代码来调用桌面应用程序。该桌面应用程序也可能是服务可执行文件...您可以使用Assembly.GetExecutingAssembly().Location http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx作为可执行路径。然后你可以使用System.Environment.UserInteractive http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx检测可执行文件是否未作为服务运行,并将有关所需执行的任务的信息作为执行参数传递。在此答案的上下文中,即捕获屏幕(例如CopyFromScreen http://msdn.microsoft.com/en-us/library/system.drawing.graphics.copyfromscreen.aspx),也可能是别的东西。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在服务器桌面会话上捕获屏幕 的相关文章

随机推荐

  • TYPO3 显示子页面的内容,包括 css 类

    使用以下打字稿 我从一页上的子页面获取所有内容 lib allPid COA lib allPid 10 HMENU 10 special directory special value 2 1 TMENU 1 expAll 1 NO do
  • 未装饰窗户的 Windows Snap 功能?

    有谁知道如何允许未装饰的窗户使用此功能 唯一的选择就是重新实施它 有任何想法吗 谢谢 可停靠可能是唯一的JToolBar http docs oracle com javase tutorial uiswing components too
  • .NET 迭代器包装抛出 API

    我有一个带有 API 的类 它允许我请求对象 直到它抛出一个IndexOutOfBoundsException 我想将它包装到一个迭代器中 以便能够编写更清晰的代码 但是 我需要捕获异常以停止迭代 static IEnumerable It
  • 当 Delphi 处于覆盖模式时,如何更改它的光标形状?

    当使用 Delphi 的任何版本时 我有时会碰巧按下插入键并打开覆盖模式 这通常是一个很大的麻烦 并且您进入覆盖模式的唯一指示是编辑器底部的小文本 当使用其他程序 首先想到的是 Notepad 时 当您处于插入模式时 文本光标是 当您处于覆
  • ASP.NET Web Api 2 中的异常处理

    Problem 我需要处理 Web api 2 异常并返回一个具有正确状态代码的丰富对象 401 表示未经授权 404 表示 ContentNotFound 等 以及一些额外信息作为内容 此外 我需要内容看起来像序列化的Exception对
  • Java中Gson、JsonElement、String比较

    好吧 我想知道这可能非常简单和愚蠢 但在与这种情况作斗争一段时间后 我不知道发生了什么 我正在使用 Gson 来处理一些 JSON 元素 在我的代码中的某个位置 我将 JsonObject 的 JsonElements 之一作为字符串获取
  • 允许点击 twitter bootstrap 下拉切换链接吗?

    我们已经设置了 Twitter 引导下拉菜单以在悬停时工作 而不是单击 是的 我们知道触摸设备上没有悬停 但我们希望当我们单击主链接时能够使其正常工作 默认情况下 twitter bootstrap 会阻止它 那么我们如何重新启用它呢 只需
  • 不使用 PHP 提交联系表单

    我还是一名学生 今天我们的讲师告诉我们 无需使用 mailto 函数即可提交联系我们表单的唯一方法是使用 PHP 我发誓去年另一位讲师向我们展示了一种仅使用 javascript 的方法 是否可以使用基本表单和 javascript 提交反
  • 如何将 numpy rearray 的子集转换为连续数组?

    我有一个recarray来自读取 csv 文件 我有兴趣将列的子集转换为连续浮点数组 我想避免将它们转换为列表或将它们一一堆叠 我尝试了中的建议https stackoverflow com a 11792956 https stackov
  • Google电子表格脚本创建特定范围内的数据验证

    我正在尝试这个脚本来创建数据验证 但它总是不起作用 function test validation var Spread SpreadsheetApp getActiveSpreadsheet var Sheet Spread getSh
  • 更新 anaconda navigator 中的软件包时出现问题

    我无法更新 anaconda navigator 中的软件包 程序 1 更新索引 2 标记我要更新的包 3 点击应用 过了一会儿 窗口仍然是空的 什么也没有发生 见图 我已经从计算机中完全删除了 anaconda 并重新安装了它 问题仍然存
  • 如何将字符串解析为 UNIQUEIDENTIFIER?

    我正在编写一个实体框架 LINQ 查询 其中我想将字符串解析为 UNIQUEIDENTIFIER 又名 GUID 作为WHERE clause public IEnumerable
  • 在 C++ 中查找精确的字符串匹配

    这是我用来检测 txt 文件中一行中的字符串的代码 int main std ifstream file C log txt std string line while file eof while std getline file lin
  • 将“npm run start”的输出写入文件

    我想捕获的输出npm run start在一个文件中 我遇到了大量错误 我想更好地控制如何筛选输出 当我尝试时 npm run start gt log txt 我得到一个非常简短的文件 8 行 其结尾为 34m 39m 90m wdm 3
  • 如何推回向量的向量?

    我正在输入 20 行 我想用空格分隔每一行的内容并将其放入向量的向量中 如何制作向量的向量 我很难把它推回来 我的输入文件 Mary had a little lamb lalala up the hill the sun is up 该向
  • 下拉 24 小时选项值和 12 小时显示

    我需要创建一个时间数组 以便在 HTML 下拉列表中使用 数组键应采用 24 小时格式 值应采用 12 小时制 包含 am 和 pm 在数据库中我想存储 24 小时格式 有没有一种快速的方法来创建数组而不是每小时键入 example 00
  • 如何调用使用 Define 创建的 GNU make 宏?

    在我的 Makefile 中调用 GREP 的两种方式有什么区别吗 我有什么理由应该使用其中之一 两者似乎产生相同的结果 define GREP word 3 shell echo define FOO 0xfff00100 endef a
  • 检查Windows电源管理是否关闭了显示器

    如何在 Windows 7 和 XP 中以编程方式检查 Windows 电源管理 是否已关闭显示屏 如果能收到一个活动就更好了 我不认为 XP 可以做到这一点 Windows 7 中有各种与电源管理相关的好东西 这Windows API 代
  • git 可以与 Xcode 集成吗?

    有没有办法将 git 存储库与 Xcode 内置的 SCM 功能一起使用 Xcode 4 原生支持 git WWDC 2010 上的开发者工具国情咨文演讲 在这里了解更多 Xcode 4 中的新增功能 http developer appl
  • 在服务器桌面会话上捕获屏幕

    我开发了一个 GUI 测试框架 可以按计划对我们公司网站进行集成测试 当出现问题时 它会截取桌面等内容 它在专用 Windows Server 2008 上的登录用户上运行 无人值守 问题是在我已断开远程桌面会话连接的桌面上截取屏幕截图 我