Exécuter le programme en capturant des captures d’écran du service Windows

J’écris un service Windows, qui gèrera des captures d’écran du programme de l’agent. Le programme d’agent fonctionne bien, pas de problème du tout – il suffit de prendre une capture d’écran et de l’enregistrer dans un fichier bmp. Mais lorsque j’essaie d’exécuter ce programme d’agent depuis mon service – cela ne fonctionne pas, tout ce que j’obtiens est des images noires (comme si j’essayais de capturer une capture d’écran directement de mon service). Mon code à l’intérieur du service est:

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); 

J’ai essayé de modifier les propriétés du service – pour lui permettre d’interagir avec le bureau, de changer d’utilisateur de service de SYSTEM à mon compte local – rien ne m’a aidé Que puis-je faire pour que cela fonctionne correctement?

UPD. Eh bien, je suppose que cela se produit car mon service exécute le programme dans session0. J’ai essayé CreateProcess() et CreateProcessAsUser() aussi – aucun résultat. Alors, comment puis-je créer des processus PAS dans session0?

Aliostad a fait le gros du travail pour cela. Regardez son code, c’est un excellent début. Gardez à l’esprit, vous aurez besoin d’une application distincte pour prendre la capture d’écran puisqu’un nouveau processus doit être lancé tout en empruntant l’identité de l’utilisateur connecté. https://stackoverflow.com/a/4147868/125406

Voici ma version de celui-ci (entièrement basé sur le code de Aliostad). Il ajoute des parameters de ligne de commande, une attente de la fin du processus et le retour du code de sortie.

 public static class ProcessAsCurrentUser { ///  /// Connection state of a session. ///  public enum ConnectionState { ///  /// A user is logged on to the session. ///  Active, ///  /// A client is connected to the session. ///  Connected, ///  /// The session is in the process of connecting to a client. ///  ConnectQuery, ///  /// This session is shadowing another session. ///  Shadowing, ///  /// The session is active, but the client has disconnected from it. ///  Disconnected, ///  /// The session is waiting for a client to connect. ///  Idle, ///  /// The session is listening for connections. ///  Listening, ///  /// The session is being reset. ///  Reset, ///  /// The session is down due to an error. ///  Down, ///  /// The session is initializing. ///  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 ssortingng lpReserved; public ssortingng lpDesktop; public ssortingng lpTitle; public Int32 dwX; public Int32 dwY; public Int32 dwXSize; public Int32 dwYSize; public Int32 dwXCountChars; public Int32 dwYCountChars; public Int32 dwFillAtsortingbute; 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 ssortingng 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, ssortingng lpApplicationName, ssortingng lpCommandLine, IntPtr lpProcessAtsortingbutes, IntPtr lpThreadAtsortingbutes, bool bInheritHandles, UInt32 dwCreationFlags, IntPtr lpEnvironment, ssortingng 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 lpTokenAtsortingbutes, 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; ///  /// 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)! ///  ///  ///  public static uint CreateProcessAsCurrentUser(ssortingng processExe, ssortingng commandLine) { IntPtr duplicate = new IntPtr(); STARTUPINFO info = new STARTUPINFO(); PROCESS_INFORMATION procInfo = new PROCESS_INFORMATION(); Debug.WriteLine(ssortingng.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(ssortingng.Format("DuplicateTokenEx result: {0}", result)); Debug.WriteLine(ssortingng.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(ssortingng.Format("CreateProcessAsUser result: {0}", result)); } if (p.ToInt32() != 0) { Marshal.Release(p); Debug.WriteLine(ssortingng.Format("Released handle p: {0}", p)); } if (duplicate.ToInt32() != 0) { Marshal.Release(duplicate); Debug.WriteLine(ssortingng.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(ssortingng.Format("sessionId: {0}", sessionId)); if (sessionId == 0xFFFFFFFF) return -1; else return (int)sessionId; } public static bool IsUserLoggedOn() { List wtsSessionInfos = ListSessions(); Debug.WriteLine(ssortingng.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 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(ssortingng.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(ssortingng.Format("WTSQueryUserToken result: {0}", result)); Debug.WriteLine(ssortingng.Format("WTSQueryUserToken p: {0}", p)); return p; } } public static List ListSessions() { IntPtr server = IntPtr.Zero; List ret = new List(); 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; } } 

Nous transmettons un nom de fichier en tant que paramètre à l'application de capture d'écran, puis attendons qu'il soit terminé. Le service prend ensuite le fichier, le traite et le supprime.

Nous avons également trouvé un autre moyen de contourner ce problème en utilisant des tubes nommés. Je connais moins cette solution.