c - 执行从 Windows 服务捕获屏幕截图的程序

标签 c windows service

我正在编写一个 Windows 服务,它将管理一些捕获屏幕截图的代理程序。代理程序运行良好,完全没有问题 - 它只是截取屏幕截图并将其保存到 bmp 文件中。 但是当我尝试从我的服务中执行这个代理程序时 - 它不起作用,我得到的只是黑色图片(就好像我试图直接从我的服务中捕获屏幕截图一样) 我的服务内部代码是:

ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);

SHELLEXECUTEINFO seInfo;
seInfo.cbSize = sizeof(SHELLEXECUTEINFO);
seInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
seInfo.hwnd = NULL;
seInfo.lpVerb = TEXT("open");
seInfo.lpFile = TEXT("D:\\dev\\work\\agent.exe");
seInfo.lpDirectory = TEXT("D:\\dev\\work\\");
seInfo.nShow = SW_SHOWNORMAL;
seInfo.hInstApp = NULL;

ShellExecuteEx(&seInfo);

我尝试更改服务属性 - 以允许它与桌面交互,将服务用户从 SYSTEM 更改为我的本地帐户 - 无济于事。 我该怎么做才能使其正常工作?

更新。好吧,我想,这是因为我的服务在 session0 中执行程序。我也尝试了 CreateProcess()CreateProcessAsUser() - 没有结果。那么我如何才能创建不在 session0 中的进程呢?

最佳答案

Aliostad 为此承担了所有繁重的工作。看看他的代码,这是一个很好的开始。请记住,您需要一个单独的应用程序来截取屏幕截图,因为在模拟登录用户时必须启动一个新进程。 https://stackoverflow.com/a/4147868/125406

这是我的版本(完全基于 Aliostad 的代码)。它添加命令行参数,等待进程完成并返回退出代码。

public static class ProcessAsCurrentUser
{

    /// <summary>
    /// Connection state of a session.
    /// </summary>
    public enum ConnectionState
    {
        /// <summary>
        /// A user is logged on to the session.
        /// </summary>
        Active,

        /// <summary>
        /// A client is connected to the session.
        /// </summary>
        Connected,

        /// <summary>
        /// The session is in the process of connecting to a client.
        /// </summary>
        ConnectQuery,

        /// <summary>
        /// This session is shadowing another session.
        /// </summary>
        Shadowing,

        /// <summary>
        /// The session is active, but the client has disconnected from it.
        /// </summary>
        Disconnected,

        /// <summary>
        /// The session is waiting for a client to connect.
        /// </summary>
        Idle,

        /// <summary>
        /// The session is listening for connections.
        /// </summary>
        Listening,

        /// <summary>
        /// The session is being reset.
        /// </summary>
        Reset,

        /// <summary>
        /// The session is down due to an error.
        /// </summary>
        Down,

        /// <summary>
        /// The session is initializing.
        /// </summary>
        Initializing
    }


    [StructLayout(LayoutKind.Sequential)]
    class SECURITY_ATTRIBUTES
    {
        public int nLength;
        public IntPtr lpSecurityDescriptor;
        public int bInheritHandle;
    }


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct STARTUPINFO
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }

    enum LOGON_TYPE
    {
        LOGON32_LOGON_INTERACTIVE = 2,
        LOGON32_LOGON_NETWORK,
        LOGON32_LOGON_BATCH,
        LOGON32_LOGON_SERVICE,
        LOGON32_LOGON_UNLOCK = 7,
        LOGON32_LOGON_NETWORK_CLEARTEXT,
        LOGON32_LOGON_NEW_CREDENTIALS
    }

    enum LOGON_PROVIDER
    {
        LOGON32_PROVIDER_DEFAULT,
        LOGON32_PROVIDER_WINNT35,
        LOGON32_PROVIDER_WINNT40,
        LOGON32_PROVIDER_WINNT50
    }

    [Flags]
    enum CreateProcessFlags : uint
    {
        CREATE_BREAKAWAY_FROM_JOB = 0x01000000,
        CREATE_DEFAULT_ERROR_MODE = 0x04000000,
        CREATE_NEW_CONSOLE = 0x00000010,
        CREATE_NEW_PROCESS_GROUP = 0x00000200,
        CREATE_NO_WINDOW = 0x08000000,
        CREATE_PROTECTED_PROCESS = 0x00040000,
        CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x02000000,
        CREATE_SEPARATE_WOW_VDM = 0x00000800,
        CREATE_SHARED_WOW_VDM = 0x00001000,
        CREATE_SUSPENDED = 0x00000004,
        CREATE_UNICODE_ENVIRONMENT = 0x00000400,
        DEBUG_ONLY_THIS_PROCESS = 0x00000002,
        DEBUG_PROCESS = 0x00000001,
        DETACHED_PROCESS = 0x00000008,
        EXTENDED_STARTUPINFO_PRESENT = 0x00080000,
        INHERIT_PARENT_AFFINITY = 0x00010000
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct WTS_SESSION_INFO
    {
        public int SessionID;
        [MarshalAs(UnmanagedType.LPTStr)] public string WinStationName;
        public ConnectionState State;
    }

    [DllImport("wtsapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern Int32 WTSEnumerateSessions(IntPtr hServer, int reserved, int version,
        ref IntPtr sessionInfo, ref int count);


    [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUserW", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool CreateProcessAsUser(
        IntPtr hToken,
        string lpApplicationName,
        string lpCommandLine,
        IntPtr lpProcessAttributes,
        IntPtr lpThreadAttributes,
        bool bInheritHandles,
        UInt32 dwCreationFlags,
        IntPtr lpEnvironment,
        string lpCurrentDirectory,
        ref STARTUPINFO lpStartupInfo,
        out PROCESS_INFORMATION lpProcessInformation);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern Int32 WaitForSingleObject(IntPtr handle, Int32 wait);

    public const Int32 INFINITE = -1;
    public const Int32 WAIT_ABANDONED = 0x80;
    public const Int32 WAIT_OBJECT_0 = 0x00;
    public const Int32 WAIT_TIMEOUT = 0x102;
    public const Int32 WAIT_FAILED = -1;

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool GetExitCodeProcess(IntPtr hProcess, out uint exitCode);

    [DllImport("wtsapi32.dll")]
    public static extern void WTSFreeMemory(IntPtr memory);

    [DllImport("kernel32.dll")]
    private static extern UInt32 WTSGetActiveConsoleSessionId();

    [DllImport("wtsapi32.dll", SetLastError = true)]
    static extern int WTSQueryUserToken(UInt32 sessionId, out IntPtr Token);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public extern static bool DuplicateTokenEx(
        IntPtr hExistingToken,
        uint dwDesiredAccess,
        IntPtr lpTokenAttributes,
        int ImpersonationLevel,
        int TokenType,
        out IntPtr phNewToken);

    private const int TokenImpersonation = 2;
    private const int SecurityIdentification = 1;
    private const int MAXIMUM_ALLOWED = 0x2000000;
    private const int TOKEN_DUPLICATE = 0x2;
    private const int TOKEN_QUERY = 0x00000008;

    /// <summary>
    /// Launches a process for the current logged on user if there are any.
    /// If none, return false as well as in case of 
    /// 
    /// ##### !!! BEWARE !!! ####  ------------------------------------------
    /// This code will only work when running in a windows service (where it is really needed)
    /// so in case you need to test it, it needs to run in the service. Reason
    /// is a security privileg which only services have (SE_??? something, cant remember)!
    /// </summary>
    /// <param name="processExe"></param>
    /// <returns></returns>
    public static uint CreateProcessAsCurrentUser(string processExe, string commandLine)
    {

        IntPtr duplicate = new IntPtr();
        STARTUPINFO info = new STARTUPINFO();
        PROCESS_INFORMATION procInfo = new PROCESS_INFORMATION();

        Debug.WriteLine(string.Format("CreateProcessAsCurrentUser. processExe: " + processExe));

        IntPtr p = GetCurrentUserToken();

        bool result = DuplicateTokenEx(p, MAXIMUM_ALLOWED | TOKEN_QUERY | TOKEN_DUPLICATE, IntPtr.Zero,
            SecurityIdentification, SecurityIdentification, out duplicate);
        Debug.WriteLine(string.Format("DuplicateTokenEx result: {0}", result));
        Debug.WriteLine(string.Format("duplicate: {0}", duplicate));


        if (result)
        {
            //NOTE: CREATE_NO_WINDOW will hide the console 
            //If there are commandline options, pass them AND the exe in 
            //commandLine and leave processExe empty.
            result = CreateProcessAsUser(duplicate, processExe, commandLine,
                //                    IntPtr.Zero, IntPtr.Zero, false, (UInt32) CreateProcessFlags.CREATE_NEW_CONSOLE, IntPtr.Zero, null,
                IntPtr.Zero, IntPtr.Zero, false, (UInt32)CreateProcessFlags.CREATE_NO_WINDOW, IntPtr.Zero, null,
                ref info, out procInfo);
            Debug.WriteLine(string.Format("CreateProcessAsUser result: {0}", result));

        }


        if (p.ToInt32() != 0)
        {
            Marshal.Release(p);
            Debug.WriteLine(string.Format("Released handle p: {0}", p));
        }


        if (duplicate.ToInt32() != 0)
        {
            Marshal.Release(duplicate);
            Debug.WriteLine(string.Format("Released handle duplicate: {0}", duplicate));
        }

        //Wait for the process to complete
        WaitForSingleObject(procInfo.hProcess, (int)INFINITE);

        //Get and return the exit code
        uint exitcode;
        GetExitCodeProcess(procInfo.hProcess, out exitcode);

        return exitcode;
    }

    public static int GetCurrentSessionId()
    {
        uint sessionId = WTSGetActiveConsoleSessionId();
        Debug.WriteLine(string.Format("sessionId: {0}", sessionId));

        if (sessionId == 0xFFFFFFFF)
            return -1;
        else
            return (int)sessionId;
    }

    public static bool IsUserLoggedOn()
    {
        List<WTS_SESSION_INFO> wtsSessionInfos = ListSessions();
        Debug.WriteLine(string.Format("Number of sessions: {0}", wtsSessionInfos.Count));
        int activeSessionCount = 0;
        foreach (var session in wtsSessionInfos)
        {
            if (session.State == ConnectionState.Active)
                activeSessionCount++;
        }
        //return wtsSessionInfos.Where(x => x.State == ConnectionState.Active).Count() > 0;
        return activeSessionCount > 0;
    }

    private static IntPtr GetCurrentUserToken()
    {
        List<WTS_SESSION_INFO> wtsSessionInfos = ListSessions();
        int sessionId = 0;
        foreach (var session in wtsSessionInfos)
        {
            if (session.State == ConnectionState.Active)
            {
                sessionId = session.SessionID;
                break;
            }
        }
        //Old Linq method
        //int sessionId = wtsSessionInfos.Where(x => x.State == ConnectionState.Active).FirstOrDefault().SessionID;
        //int sessionId = GetCurrentSessionId();

        Debug.WriteLine(string.Format("sessionId: {0}", sessionId));
        if (sessionId == int.MaxValue)
        {
            return IntPtr.Zero;
        }
        else
        {
            IntPtr p = new IntPtr();
            int result = WTSQueryUserToken((UInt32)sessionId, out p);
            Debug.WriteLine(string.Format("WTSQueryUserToken result: {0}", result));
            Debug.WriteLine(string.Format("WTSQueryUserToken p: {0}", p));

            return p;
        }
    }

    public static List<WTS_SESSION_INFO> ListSessions()
    {
        IntPtr server = IntPtr.Zero;
        List<WTS_SESSION_INFO> ret = new List<WTS_SESSION_INFO>();

        try
        {
            IntPtr ppSessionInfo = IntPtr.Zero;

            Int32 count = 0;
            Int32 retval = WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref ppSessionInfo, ref count);
            Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

            Int64 current = (int)ppSessionInfo;

            if (retval != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    WTS_SESSION_INFO si =
                        (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current,
                            typeof(WTS_SESSION_INFO));
                    current += dataSize;

                    ret.Add(si);
                }

                WTSFreeMemory(ppSessionInfo);
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.ToString());
        }

        return ret;
    }

}

我们将文件名作为参数传递给截图应用程序,然后等待它完成。该服务随后会拾取文件并进行处理并将其删除。

我们还使用命名管道找到了另一种解决方法。我不太熟悉该解决方案。

关于c - 执行从 Windows 服务捕获屏幕截图的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752939/

相关文章:

c++ - InternetCanonicalizeUrl 无法解码变音字母

c - 如何更改C语言程序的默认图标?

java - Guava的AbstractExecutionThreadService可以复用吗?

c - 如何循环一个整数数组并将正数和负数保存到另一个数组中?

c - 如何使用 C 和 WinAPI 将包含特殊字符的文本复制到剪贴板?

C - 生成 X 字符词的所有可能性

windows - NetTcpBinding(读取 WindowsStreamSecurityBindingElement)如何加密/签署消息?

c - 主线程中的多线程矩阵乘法不会等到其他线程完成工作吗?

web-services - 服务是否应在每次请求时要求提供凭据?

service - 没有足够的权限来安装服务