Implementing interactive Windows Services

Source: Internet
Author: User

I want to be a file monitoring service over the past few days. I have read many articles about Windows services on the Internet. I only talked about how to make the most basic service, but it does not show how to interact with users. Check msdn and see the description about the service:

Windows service applications run in a window area different from the login user's interaction area. The window area is a security object that contains the clipboard, a group of global atoms, and a group of desktop objects. Because the Windows Service region is not an Interactive region, the dialog box raised in the Windows service application is invisible and may cause the program to stop responding. Similarly, the error information should be recorded in the Windows event log, rather than in the user interface.

The Windows service class supported by. NET Framework does not support interaction with the interactive area (that is, the login user. At the same time,. NET Framework does not contain classes that represent regions and desktops. If the Windows Service must interact with other regions, you need to access the unmanaged Windows API.

That is to say, to implement interactive services (for example, if we want to set some parameters for the service at runtime), we must use system. runtime. interopservices.

Let's take a look at how to implement an interactive service. The steps are the same as implementing basic services (you can refer to msdn or Google ).

When implementing onstart, note that a form or something cannot be displayed here. There is no response to this. We can run a thread in this method. This thread needs to access window area objects or desktop objects. Of course, these are not provided in the framework, and they need to access unmanaged code.

Let's take a look at the code and try again.

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. serviceprocess;
Using system. Threading;
Using system. runtime. interopservices;
Namespace filewatchservice
{
Public class service1: system. serviceprocess. servicebase
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Thread threadform = NULL;
Public service1 ()
{
// This call is required by the windows. Forms component designer.
Initializecomponent ();

// Todo: add any initialization after the initcomponent call
}

# Code generated by the region component designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
//
// Service1
//
This. servicename = "jadewatchservice ";

}
# Endregion
[Stathread]
Static void main ()
{
System. serviceprocess. servicebase. Run (New service1 ());

}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

/// <Summary>
/// Set specific operations so that the service can perform its work.
/// </Summary>
Protected override void onstart (string [] ARGs)
{
Threadform = new thread (New threadstart (formshow ));
Threadform. Start ();
}
 
/// <Summary>
/// Stop the service.
/// </Summary>
Protected override void onstop ()
{
If (threadform! = NULL)
{
If (threadform. isalive)
{
Threadform. Abort ();
Threadform = NULL;
}
}
}

Void formshow ()
{

Getasktopwindow ();
Intptr hwinstasave = getprocesswindowstation ();
Intptr dwthreadid = getcurrentthreadid ();
Intptr hsf-save = getthreaddesktop (dwthreadid );
Intptr hwinstauser = openwindowstation ("winsta0", false, 33554432 );
If (hwinstauser = intptr. Zero)
{
Rpcreverttoself ();
Return;
}
Setprocesswindowstation (hwinstauser );
Intptr hsf-user = opendesktop ("default", 0, false, 33554432 );
Rpcreverttoself ();
If (hsf-user = intptr. Zero)
{
Setprocesswindowstation (hwinstasave );
Closewindowstation (hwinstauser );
Return;
}
Setthreaddesktop (hsf-user );
 
Intptr dwguithreadid = dwthreadid;

Form1 F = new form1 (); // This form1 can contain policyicon, which can be displayed in the tray. You can click the tray icon to set it.
System. Windows. Forms. application. Run (f );


Dwguithreadid = intptr. zero;
Setthreaddesktop (hsf-save );
Setprocesswindowstation (hwinstasave );
Closedesktop (hsf-user );
Closewindowstation (hwinstauser );
}

[Dllimport ("user32.dll")]
Static extern int getasktopwindow ();

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

[Dllimport ("user32.dll")]
Static extern intptr setprocesswindowstation (intptr );
[Dllimport ("user32.dll")]
Static extern intptr closewindowstation (intptr );
}
}

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.