2011-01-17 4 views
3
protected override void OnStart(string[] args) 
{ 
    base.OnStart(args); 
    CaptureScreen(); 

} 

protected override void OnStop() 
{ 
    base.OnStop(); 

} 

private void CaptureScreen() 
{ 

    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

    Graphics graphics = Graphics.FromImage(printscreen as Image); 

    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

    printscreen.Save(@"L:\" + Counter++ + ".jpg", ImageFormat.Jpeg); 
} 
  • Ich habe interact mit Desktop
  • versucht, die das Konto Local & Benutzer
+0

Welches Betriebssystem? –

+0

Hinweis: Ein Dienst weiß möglicherweise nichts von einem zugeordneten Laufwerk L:, das normalerweise benutzerabhängig ist. Verwenden Sie einen UNC-Pfad (\\ Server \ Pfad). –

+0

L: \\ lokales Laufwerk und das Betriebssystem 7 64bit – Rami

Antwort

0

Der Dienst wird „kopflos“, keine Benutzeroberfläche und ohne eine 100% sicher zu sein (Die Dokumentation für CopyFromScreen ist ziemlich vage) Ich würde erwarten, dass das scheitert, wenn ich kopflos renne. Wie würde der Dienst wissen, welcher Bildschirm kopiert werden soll, wenn mehrere Benutzer gleichzeitig angemeldet sind?

sehen this Fragen sowie

+0

so was soll ich es den aktuellen Benutzer – Rami

+1

@Rami tun, um zu erfassen: Sie fehlt der Punkt.Wenn mehrere Benutzer angemeldet sind, gibt es keine Vorstellung vom "aktuellen Benutzer" - alle sind "aktuell", aus ihrer individuellen Perspektive. Von einer Außenseiterposition aus, ähnlich einer Menschenmenge, sind alle gleich (sehr interessant). –

+0

Ich benutze keinen Server, also habe ich nur einzelne Benutzer und danke – Rami

0

Im Fall von XP/2003 die Interact mit dem Desktop helfen sollen. Im Fall von Windows 7/Windows 2008 funktioniert Interact mit Desktop anders.

Die beste Lösung für Sie wäre, Anmeldesitzungen aus dem Dienst zu analysieren und für neue Sitzung den "Desktop" -Prozess in Benutzersitzung zu starten und mit diesem Prozess zu kommunizieren, um Bildschirme zu erhalten.

+0

können Sie mir sagen, was in mehr detals zu tun ist – Rami

+0

Ok. Zunächst muss Ihr Dienst mit allen Anmeldesitzungen kommunizieren. Sie sollte sie fortlaufend aufzählen, und sobald die neue Anmeldesitzung angezeigt wird, erstellen Sie den Prozess in dieser Anmeldesitzung, der die Bitmaps erfassen wird. Dann kann der Dienst mit Bitmap-Capture-Anwendung über TCP/IP, Shared Memory usw. kommunizieren. Der Dienst sollte auch überwachen, ob die Anwendung aktiv ist. Wenn der Benutzer die Anwendung beendet hat, muss der Dienst sie erneut starten. Weitere Details würden diese Anwendung schreiben. –

+0

Yeah ... Wenn ich den Kommentar von Cody Grey in einem anderen Thread anschaue, sehe ich, dass ich einen Fehler gemacht habe, wenn man davon ausgeht, dass der Dienst den Benutzerprozess sehr leicht starten kann. Wahrscheinlich gibt es eine Möglichkeit, die Anwendung zu starten und sie an den Desktop anzuhängen ... Keine schnelle Antwort. –

6

Dies ist Teil der Isolationsfunktion Sitzung 0, die zu Vista hinzugefügt wurde. Dienste führen jetzt ihre eigene Sitzung mit ihrer eigenen Arbeitsstation und ihrem eigenen Desktop aus. Ähnlich wie in der Sitzung, in der die Anmeldeaufforderung und der Bildschirmschoner ausgeführt werden. Sie machen einen Screenshot des Desktops dieser Sitzung, da ist nichts drin. Der Zugriff auf den Benutzerdesktop ist nicht mehr möglich. Es ist eine Sicherheitsfunktion, es verhindert Splitterangriffe. Zugegebenermaßen verstehe ich nicht, warum das Kontrollkästchen "Mit Desktop interagieren" nicht entfernt wurde.

Sie müssen Ihr Programm so ändern, dass es als "Windows-Anwendung" und nicht als Dienst ausgeführt wird. Fügen Sie eine Verknüpfung in den Ordner Autostart ein, oder verwenden Sie den Registrierungsschlüssel Ausführen. Welche Ordnung ist, gibt es nicht viel wert Schnappen, wenn kein Benutzer angemeldet ist.

1

Sie benötigen Service für die Benutzer Fenster Station einzustellen. Das ist nicht mein Code und ich kann mich nicht erinnern, woher ich ihn habe. Fügen Sie diese zwei Klassen hinzu, dann können Sie ein Desktop-Objekt erstellen

Desktop userDesk = new Desktop();

Dann, wenn Sie Ihren Dienst müssen mit der Benutzersitzung zu interagieren würden Sie

userDesk.BeginInteraction() schreiben;

und schließlich Sie

userDesk.EndInteraction zurückzukehren Sitzung Ihren Dienst nennen();

internal class Desktop 
{ 
    private IntPtr m_hCurWinsta = IntPtr.Zero; 
    private IntPtr m_hCurDesktop = IntPtr.Zero; 
    private IntPtr m_hWinsta = IntPtr.Zero; 
    private IntPtr m_hDesk = IntPtr.Zero; 

    /// <summary> 
    /// associate the current thread to the default desktop 
    /// </summary> 
    /// <returns></returns> 
    internal bool BeginInteraction() 
    { 
     EndInteraction(); 
     m_hCurWinsta = User32DLL.GetProcessWindowStation(); 
     if (m_hCurWinsta == IntPtr.Zero) 
      return false; 

     m_hCurDesktop = User32DLL.GetDesktopWindow(); 
     if (m_hCurDesktop == IntPtr.Zero) 
      return false; 

     m_hWinsta = User32DLL.OpenWindowStation("winsta0", false, 
      WindowStationAccessRight.WINSTA_ACCESSCLIPBOARD | 
      WindowStationAccessRight.WINSTA_ACCESSGLOBALATOMS | 
      WindowStationAccessRight.WINSTA_CREATEDESKTOP | 
      WindowStationAccessRight.WINSTA_ENUMDESKTOPS | 
      WindowStationAccessRight.WINSTA_ENUMERATE | 
      WindowStationAccessRight.WINSTA_EXITWINDOWS | 
      WindowStationAccessRight.WINSTA_READATTRIBUTES | 
      WindowStationAccessRight.WINSTA_READSCREEN | 
      WindowStationAccessRight.WINSTA_WRITEATTRIBUTES 
      ); 
     if (m_hWinsta == IntPtr.Zero) 
      return false; 

     User32DLL.SetProcessWindowStation(m_hWinsta); 

     m_hDesk = User32DLL.OpenDesktop("default", OpenDesktopFlag.DF_NONE, false, 
       DesktopAccessRight.DESKTOP_CREATEMENU | 
       DesktopAccessRight.DESKTOP_CREATEWINDOW | 
       DesktopAccessRight.DESKTOP_ENUMERATE | 
       DesktopAccessRight.DESKTOP_HOOKCONTROL | 
       DesktopAccessRight.DESKTOP_JOURNALPLAYBACK | 
       DesktopAccessRight.DESKTOP_JOURNALRECORD | 
       DesktopAccessRight.DESKTOP_READOBJECTS | 
       DesktopAccessRight.DESKTOP_SWITCHDESKTOP | 
       DesktopAccessRight.DESKTOP_WRITEOBJECTS 
       ); 
     if (m_hDesk == IntPtr.Zero) 
      return false; 

     User32DLL.SetThreadDesktop(m_hDesk); 

     return true; 
    } 

    /// <summary> 
    /// restore 
    /// </summary> 
    internal void EndInteraction() 
    { 
     if (m_hCurWinsta != IntPtr.Zero) 
      User32DLL.SetProcessWindowStation(m_hCurWinsta); 

     if (m_hCurDesktop != IntPtr.Zero) 
      User32DLL.SetThreadDesktop(m_hCurDesktop); 

     if (m_hWinsta != IntPtr.Zero) 
      User32DLL.CloseWindowStation(m_hWinsta); 

     if (m_hDesk != IntPtr.Zero) 
      User32DLL.CloseDesktop(m_hDesk); 
    } 
} 
public static class User32DLL 
{ 
    /// <summary> 
    /// The GetDesktopWindow function returns a handle to the desktop window. 
    /// The desktop window covers the entire screen. 
    /// The desktop window is the area on top of which other windows are painted. 
    /// </summary> 
    /// <returns>The return value is a handle to the desktop window. </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr GetDesktopWindow(); 


    /// <summary> 
    /// Retrieves a handle to the current window station for the calling process. 
    /// </summary> 
    /// <returns>If the function succeeds, 
    /// the return value is a handle to the window station. 
    /// If the function fails, the return value is NULL.</returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr GetProcessWindowStation(); 


    /// <summary> 
    /// Retrieves a handle to the desktop assigned to the specified thread. 
    /// </summary> 
    /// <param name="dwThread">[in] Handle to the thread 
    ///  for which to return the desktop handle.</param> 
    /// <returns>If the function succeeds, the return value is a handle to the 
    /// desktop associated with the specified thread. 
    /// If the function fails, the return value is NULL.</returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr GetThreadDesktop(uint dwThread); 


    /// <summary> 
    /// Opens the specified window station. 
    /// </summary> 
    /// <param name="lpszWinSta">Pointer to a null-terminated 
    ///   string specifying the name of the window station 
    /// to be opened. Window station names are case-insensitive. 
    /// This window station must belong to the current session. 
    /// </param> 
    /// <param name="fInherit">[in] If this value 
    ///   is TRUE, processes created by this process 
    /// will inherit the handle. Otherwise, 
    /// the processes do not inherit this handle. 
    /// </param> 
    /// <param name="dwDesiredAccess">[in] Access to the window station</param> 
    /// <returns>If the function succeeds, the return value 
    ///   is the handle to the specified window station. 
    /// If the function fails, the return value is NULL.</returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr OpenWindowStation(string lpszWinSta 
     , bool fInherit 
     , WindowStationAccessRight dwDesiredAccess 
     ); 


    /// <summary> 
    /// Assigns the specified window station to the calling process. 
    /// This enables the process to access objects in the window 
    /// station such as desktops, the clipboard, and global atoms. 
    /// All subsequent operations on the window station 
    /// use the access rights granted to hWinSta. 
    /// </summary> 
    /// <param name="hWinSta">[in] Handle to the window 
    ///   station to be assigned to the calling process</param> 
    /// <returns>If the function succeeds, the return value is nonzero. 
    /// If the function fails, the return value is zero. </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr SetProcessWindowStation(IntPtr hWinSta); 


    /// <summary> 
    /// Closes an open window station handle. 
    /// </summary> 
    /// <param name="hWinSta">[in] Handle 
    ///   to the window station to be closed.</param> 
    /// <returns>If the function succeeds, the return value is nonzero. 
    /// If the function fails, the return value is zero. </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr CloseWindowStation(IntPtr hWinSta); 


    /// <summary> 
    /// Opens the specified desktop object. 
    /// </summary> 
    /// <param name="lpszDesktop">[in] Pointer to null-terminated string 
    /// specifying the name of the desktop to be opened. 
    /// Desktop names are case-insensitive. 
    /// This desktop must belong to the current window station.</param> 
    /// <param name="dwFlags">[in] This parameter can 
    ///   be zero or DF_ALLOWOTHERACCOUNTHOOK=0x0001</param> 
    /// <param name="fInherit">[in] If this value is TRUE, processes created by 
    /// this process will inherit the handle. 
    /// Otherwise, the processes do not inherit this handle. </param> 
    /// <param name="dwDesiredAccess">[in] Access 
    ///   to the desktop. For a list of access rights</param> 
    /// <returns>If the function succeeds, the return value is a handle to the opened desktop. 
    /// When you are finished using the handle, call the CloseDesktop function to close it. 
    /// If the function fails, the return value is NULL. 
    /// </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr OpenDesktop(string lpszDesktop 
     , OpenDesktopFlag dwFlags 
     , bool fInherit 
     , DesktopAccessRight dwDesiredAccess 
     ); 


    /// <summary> 
    /// Closes an open handle to a desktop object. 
    /// </summary> 
    /// <param name="hDesktop">[in] Handle to the desktop to be closed.</param> 
    /// <returns>If the function succeeds, the return value is nonzero. 
    /// If the function fails, the return value is zero. </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern IntPtr CloseDesktop(IntPtr hDesktop); 

    /// <summary> 
    /// Assigns the specified desktop to the calling thread. 
    /// All subsequent operations on the desktop use the access rights granted to the desktop. 
    /// </summary> 
    /// <param name="hDesktop">[in] Handle to the desktop 
    ///   to be assigned to the calling thread.</param> 
    /// <returns>If the function succeeds, the return value is nonzero. 
    /// If the function fails, the return value is zero. </returns> 
    [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern bool SetThreadDesktop(IntPtr hDesktop); 
} 

/// <summary> 
/// REF MSDN:Window Station Security and Access Rights 
/// ms-help://MS.MSDN.vAug06.en/dllproc/base/window_station_security_and_access_rights.htm 
/// </summary> 
[FlagsAttribute] 
public enum WindowStationAccessRight : uint 
{ 
    /// <summary>All possible access rights for the window station.</summary> 
    WINSTA_ALL_ACCESS = 0x37F, 

    /// <summary>Required to use the clipboard.</summary> 
    WINSTA_ACCESSCLIPBOARD = 0x0004, 

    /// <summary>Required to manipulate global atoms.</summary> 
    WINSTA_ACCESSGLOBALATOMS = 0x0020, 

    /// <summary>Required to create new desktop 
    /// objects on the window station.</summary> 
    WINSTA_CREATEDESKTOP = 0x0008, 

    /// <summary>Required to enumerate existing desktop objects.</summary> 
    WINSTA_ENUMDESKTOPS = 0x0001, 

    /// <summary>Required for the window station to be enumerated.</summary> 
    WINSTA_ENUMERATE = 0x0100, 

    /// <summary>Required to successfully call the ExitWindows or ExitWindowsEx function. 
    /// Window stations can be shared by users and this access type can prevent other users 
    /// of a window station from logging off the window station owner.</summary> 
    WINSTA_EXITWINDOWS = 0x0040, 

    /// <summary>Required to read the attributes of a window station object. 
    /// This attribute includes color settings 
    /// and other global window station properties.</summary> 
    WINSTA_READATTRIBUTES = 0x0002, 

    /// <summary>Required to access screen contents.</summary> 
    WINSTA_READSCREEN = 0x0200, 

    /// <summary>Required to modify the attributes of 
    /// a window station object. 
    /// The attributes include color settings 
    /// and other global window station properties.</summary> 
    WINSTA_WRITEATTRIBUTES = 0x0010, 
} 

/// <summary> 
/// OpenDesktop 2nd param 
/// </summary> 
public enum OpenDesktopFlag : uint 
{ 
    /// <summary> 
    /// Default value 
    /// </summary> 
    DF_NONE = 0x0000, 

    /// <summary> 
    /// Allows processes running in other accounts on the desktop 
    /// to set hooks in this process. 
    /// </summary> 
    DF_ALLOWOTHERACCOUNTHOOK = 0x0001, 
} 

/// <summary> 
/// REF MSDN:Desktop Security and Access Rights 
/// ms-help://MS.MSDN.vAug06.en/dllproc/base/desktop_security_and_access_rights.htm 
/// </summary> 
[FlagsAttribute] 
public enum DesktopAccessRight : uint 
{ 
    /// <summary>Required to create a menu on the desktop. </summary> 
    DESKTOP_CREATEMENU = 0x0004, 

    /// <summary>Required to create a window on the desktop. </summary> 
    DESKTOP_CREATEWINDOW = 0x0002, 

    /// <summary>Required for the desktop to be enumerated. </summary> 
    DESKTOP_ENUMERATE = 0x0040, 

    /// <summary>Required to establish any of the window hooks. </summary> 
    DESKTOP_HOOKCONTROL = 0x0008, 

    /// <summary>Required to perform journal playback on a desktop. </summary> 
    DESKTOP_JOURNALPLAYBACK = 0x0020, 

    /// <summary>Required to perform journal recording on a desktop. </summary> 
    DESKTOP_JOURNALRECORD = 0x0010, 

    /// <summary>Required to read objects on the desktop. </summary> 
    DESKTOP_READOBJECTS = 0x0001, 

    /// <summary>Required to activate the desktop 
    ///   using the SwitchDesktop function. </summary> 
    DESKTOP_SWITCHDESKTOP = 0x0100, 

    /// <summary>Required to write objects on the desktop. </summary> 
    DESKTOP_WRITEOBJECTS = 0x0080, 
} 
Verwandte Themen