C # Windows services that implement user interaction through a service initiation form (adding a form to a service) [forwarding]

Source: Internet
Author: User

Because of personal needs, want to find a keylogger program, downloaded from the Internet a lot, most of them need to register, in addition to be killed more soft killing. So decide to write one yourself, if as a Windows application, you can implement the capture keyboard record. One way to get started with the system is to use it as a Windows service, write the code directly into the service and not grab the keylogger, read the data on the Web and view MSDN:

The Windows Service application runs in a window area that differs from the logged-on user's interactive area. A window area is a securable object that contains the Clipboard, a set of global atoms, and a set of desktop objects. Because the region of the Windows service is not an interactive zone, the dialog box that is raised in the Windows Service application will be invisible and may cause the program to stop responding. Similarly, error messages should be logged in the Windows event log instead of being raised in the user interface.

The service program generally uses the LocalSystem account, has its own window station, and the default desktop, the window station is not user interaction, that is, you can not display the window, it does not accept the user's mouse, Keyboard and other inputs.

When we log in with a user account, we see the desktop, which is the default (desktop) under WINSTA0 (Window station).
There are 3 desktops under WINSTA0:
WinLogon: Appears as a logon dialog box. When the user logs on, WinLogon.exe switches to default desktop.
Default: This is where Explorer.exe and all the user program Windows appear, which is where we usually see Windows. The application runs on this desktop
Screen saver: When the system is idle, run the screensaver's desktop.

When you select a service in Computer Management, modify the properties, and select "Allow service to interact with the desktop" On the Login tab, the service uses default (desktop) under WINSTA0 (Window station). You can also interoperate with your service. At this point, you can get the default desktop bitmap, because the thread's desktop is the default under WINSTA0. To get the Winlogon desktop bitmap at the same time, you should first set the thread's desktop to Winlogon.

This part of the code is published as follows:

Service1.Designer.cs file

Using System.Threading;

Namespace KeyBoard
{
    partial class Service1
    {
        //<summary> 
       // The required designer variables.
       //</summary>
         Private System.ComponentModel.IContainer components = null;
        Thread threadform = null;

       //<summary>
       / Clean up all the resources that are in use.
       //</summary>
        //<param name= "disposing" > True if the managed resource should be released, otherwise false. </param>
        protected override void Dispose (bool disposing)
        {
             if (disposing && (components = null))
             {
                Components. Dispose ();
           }
            Base. Dispose (disposing);
       }

Code generated by the #region Component Designer

<summary>
The designer supports the required method-do not
Use the Code Editor to modify the contents of this method.
</summary>
private void InitializeComponent ()
{
//
Service1
//
This. ServiceName = "Service1";

}

#endregion

}
}

Service1.cs file

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.ServiceProcess;
Using System.Text;
Using System.Threading;
Using System.Runtime.InteropServices;

Namespace KeyBoard
{
public partial class Service1:servicebase
{
Public Service1 ()
{
InitializeComponent ();
}

        protected override void OnStart (string[] args)
         {
            threadform = new Thread (new ThreadStart (formshow));
            Threadform.start ();
       }

        protected override void OnStop ()
         {
            if (threadform! = null)
            {
                 if (threadform.isalive)
                 {
                     Threadform.abort ();
                     threadform = null;
               }
           }

}

void Formshow ()
{

GetDesktopWindow ();
IntPtr Hwinstasave = Getprocesswindowstation ();
IntPtr dwThreadID = GetCurrentThreadID ();
IntPtr Hdesksave = Getthreaddesktop (dwThreadID);
IntPtr Hwinstauser = openwindowstation ("WinSta0", false,33554432);
if (Hwinstauser = = IntPtr.Zero)
{
Rpcreverttoself ();
return;
}
SetProcessWindowStation (Hwinstauser);
IntPtr Hdeskuser = OpenDesktop ("Default", 0, False, 33554432);
Rpcreverttoself ();
if (Hdeskuser = = IntPtr.Zero)
{
SetProcessWindowStation (Hwinstasave);
Closewindowstation (Hwinstauser);
return;
}
SetThreadDesktop (Hdeskuser);

IntPtr dwguithreadid = dwThreadID;

Mousekeyboard f=new Mousekeyboard (); This FORM1 can be NotifyIcon, can be displayed in the tray, the user can click on the tray icon to set
System.Windows.Forms.Application.Run (f);


Dwguithreadid = IntPtr.Zero;
SetThreadDesktop (Hdesksave);
SetProcessWindowStation (Hwinstasave);
Closedesktop (Hdeskuser);
Closewindowstation (Hwinstauser);
}

[DllImport ("User32.dll")]
static extern int GetDesktopWindow ();

[DllImport ("User32.dll")]
static extern IntPtr getprocesswindowstation ();

[DllImport ("kernel32.dll")]
static extern IntPtr GetCurrentThreadID ();

[DllImport ("User32.dll")]
static extern IntPtr Getthreaddesktop (IntPtr dwthread);

[DllImport ("User32.dll")]
static extern IntPtr openwindowstation (string a,bool b,int c);

[DllImport ("User32.dll")]
static extern IntPtr OpenDesktop (string lpszdesktop, uint dwFlags,
BOOL Finherit, uint dwdesiredaccess);

[DllImport ("User32.dll")]
static extern IntPtr Closedesktop (IntPtr p);

[DllImport ("Rpcrt4.dll", Setlasterror=true)]
static extern IntPtr rpcimpersonateclient (int i);


[DllImport ("Rpcrt4.dll", Setlasterror=true)]
static extern IntPtr rpcreverttoself ();

[DllImport ("User32.dll")]
static extern IntPtr SetThreadDesktop (IntPtr a);

[DllImport ("User32.dll")]
static extern IntPtr setprocesswindowstation (IntPtr a);
[DllImport ("User32.dll")]
static extern IntPtr closewindowstation (IntPtr a);

}


}

C # Windows services that implement user interaction through a service initiation form (adding a form to a service) [forwarding]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.