[C #] SQL Server Service monitor of WinForm (avoid starting the service at startup ),

Source: Internet
Author: User

[C #] SQL Server Service monitor of WinForm (avoid starting the service at startup ),

Since I have just started to write a blog and recently become lazy, I have no time to organize many projects in the past. I will first send a small tool that I have previously written.

 

About MS-SQL service Optimization

People who have installed MS-SQL servers should know that the system will have 4, 5 more new services

  • SQL Active Directory Helper Service
  • SQL Server (SQLEXPRESS) is provided by Visual Studio. The names of databases installed with the database management tool are different.
  • SQL Server Browser
  • SQL Server VSS Writer
  • SQL Server proxy (SQLEXPRESS) SQLEXPRESS database comes with Visual Studio Platform

I remember that when I first took the database course, my friends had to go through all the hardships to install Microsoft SQL Server 2008. However, in addition to using several GB of disk space in the white space, the most unacceptable is the extra series of services, which immediately doubled the boot time.

To optimize the boot service, I tried to disable these services one by one, and finally got

  • To connect to a database, the SQL Server (SQLEXPRESS) service must be enabled;
  • If you do not know the connection address between the local Server name and the database, you can enable the SQL Server Browser service to enable automatic list on the vsplatform for manual binding.

Therefore, in order to speed up the boot as much as possible and eliminate the negative impact of DBMS, I only keep the unique service SQL Server and disable other service items.

However, an accidental connection failure caused me to discover the huge security risks. At that time, I was viewing the Log file (refer to the path C: \ Program Files \ Microsoft SQL Server \ MSSQL10.SQLEXPRESS \ MSSQL \ Log ), I found that 2 MB or more of the error warning files contained a dense remote IP address logon failure record, and my database is only used for local SQL learning and it is completely impossible to have remote access, that is to say, the computer was attacked when the service was started !! What we need to know is that in a windows environment, the entire operating system is part of the database. intrusion is not just a self-built database. Attackers can even log on to the server to control the entire host. At that time, I realized that the network attack was so close to myself that I had vowed to track those illegal IP addresses, however, this is not a long journey (You may have time to sort out network security issues in the future ).

Since that time, I have never dared to open the SQL service for a long time, and this tool was written at that time. The purpose is to start and close the SQL service at any time, so as to avoid searching for this service from the task manager every time, so that you can close it in time after learning. It seems that I am still lazy. management; using System. windows. forms; namespace Watch {public partial class Form1: Form {// The Menu ContextMenu after clicking create notification bar policycontextmenu = new ContextMenu (); private ManagementClass mc = new ManagementClass ("Win32_Service "); // In the system management class, select the Win32 service (not 64-bit) private ManagementObjectCollection moc; // It is used to store all the services in the system, private ManagementObject myService; // It is used to select a single management object, that is, a single service Me NuItem m_open; // three menu items MenuItem m_close; MenuItem m_exit; public Form1 () {System. diagnostics. process. getCurrentProcess (). maxWorkingSet = (IntPtr) 500000; InitializeComponent (); this. notifyIcon1.Text = "Monitoring Service... "; this. textBox1.Text = "MSSQL $ SQLEXPRESS"; m_open = new MenuItem (); m_close = new MenuItem (); m_exit = new MenuItem (); m_open.Text = "Start Service "; m_close.Text = "Stop Service"; m_exit.Text = "quit"; m_op En. click + = new EventHandler (m_open_Click); m_close.Click + = new EventHandler (m_close_Click); m_exit.Click + = new EventHandler (m_exit_Click); notifyContextMenu. menuItems. add (m_open); yycontextmenu. menuItems. add (m_close); yycontextmenu. menuItems. add (m_exit); policyicon1.contextmenu = policycontextmenu; getState () ;}// disable the specified service void m_close_Click (object sender, EventArgs e) {if (bool) myServi Ce ["AcceptStop"]) {myService. invokeMethod ("StopService", null); this. policyicon1.icon = Properties. resources. close;} getState () ;}// start the specified service void m_open_Click (object sender, EventArgs e) {if (string) myService ["State"] = "Stopped ") {myService. invokeMethod ("StartService", null);} else if (string) myService ["State"] = "Paused") {myService. invokeMethod ("ResumeService", null);} getState ();}/ /Exit the program void m_exit_Click (object sender, EventArgs e) {this. Dispose (); // release the memory, better than the first one. Application. exit () ;}// double-click the notification bar icon and the private void policyicon=mousedoubleclick (object sender, MouseEventArgs e) {getState (); if (this. visible = true) {this. hide ();} else {this. show (); this. activate () ;}/// obtain real-time system service information private void getState () {if (moc! = Null) // clear the historical service information moc. dispose (); moc = mc. getInstances (); // get the service list foreach (ManagementObject mo in moc) // query the specified service {if (string) mo ["Name"] = this. textBox1.Text) {myService = mo; break;} if (string) myService ["StartMode"] = "Disabled") // determine whether the service is Disabled {this. label1.Text = "Disabled"; this. policyicon1.icon = Properties. resources. disabled;} else if (string) myService ["State"] = "Running") // determine whether the service is Running {this. policyicon1.icon = Properties. resources. open; m_close.Checked = false; m_open.Checked = true; this. label1.Text = "Running";} else if (string) myService ["State"] = "Stopped") // judge whether the service has Stopped {this. policyicon1.icon = Properties. resources. close; m_close.Checked = true; m_open.Checked = false; this. label1.Text = "Stopped";} else // if it is in the intermediate state, obtain the service status again after a period of time {this. label1.Text = (string) myService ["State"]; System. threading. thread. sleep (500); getState () ;}// reload the default system close event, that is, do not exit the program when you click the close cross button in the main window, only hide the window (minimized to the notification bar in disguise) protected override void WndProc (ref Message m) {const int WM_SYSCOMMAND = 0x0112; const int SC _CLOSE = 0xF060; if (m. msg = WM_SYSCOMMAND & (int) m. WParam = SC _CLOSE) {// block the incoming message event this. hide (); return;} base. wndProc (ref m);} // to learn the drag event, a hidden small function is added here: When the mouse is pressed at the tag, the label private void label2_MouseDown (object sender, mouseEventArgs e) {this. label2.DoDragDrop (this. label2, DragDropEffects. move);} private void textboxincludragenter (object sender, DragEventArgs e) // when the label is dragged into the input box, the drag sign {e. effect = DragDropEffects. move;} private void textboxincludragdrop (object sender, DragEventArgs e) // restore the default service name {this. textBox1.Text = "MSSQL $ SQLEXPRESS ";}}}

 

About "minimize to notification bar"

Many applications with the notification bar icon do not exit the program after the main window is closed, but only "minimize to the notification bar ".

Previously, the code used overload to disable the Message protected override void WndProc (ref Message m.

Rewrite the WndProc function to capture messages in all windows. In this way, we can "tamper" the incoming message, and manually change the window behavior.

Here we will add some common message types in Windows, which are as follows:

  • WM_NULL =$ 0000;
  • WM_CREATE =$ 0001;
    Application creation window
  • WM_DESTROY =$ 0002;
    A window is destroyed.
  • WM_MOVE =$ 0003;
    Move a window
  • WM_SIZE =$ 0005;
    Change the size of a window
  • WM_ACTIVATE =$ 0006;
    A window is activated or inactive;
  • WM_SETFOCUS =$ 0007;
    After getting focus
  • WM_KILLFOCUS = $0008;
    No focus
  • WM_ENABLE = $ 000A;
    Enable status change
  • WM_SETREDRAW = $ 000B;
    Set whether the window can be re-painted
  • WM_SETTEXT = $ 000C;
    The application sends this message to set the text of a window.
  • WM_GETTEXT = $ 000D;
    The application sends this message to copy the text of the corresponding window to the buffer zone.
  • WM_GETTEXTLENGTH = $ 000E;
    Get the length of text related to a window (excluding empty characters)
  • WM_PAINT = $ 000F;
    Require a window to redraw itself
  • WM_CLOSE = $0010;
    Send a signal when a window or application is closed
  • WM_QUERYENDSESSION = $0011;
    When the user selects the end dialog box or the program calls the ExitWindows Function
  • WM_QUIT =$ 0012;
    Used to end the program running or when the program calls the postquitmessage Function
  • WM_QUERYOPEN =$ 0013;
    Send the message to an icon when the user window restores the previous size position.
  • WM_ERASEBKGND =$ 0014;
    When the window background must be erased (for example, when the window size changes)
  • WM_SYSCOLORCHANGE = $0015;
    When the system color changes, send this message to all top-level windows.
  • WM_ENDSESSION =$ 0016;
    After the system process sends the WM_QUERYENDSESSION message, the message is sent to the application to notify the application of whether the conversation ends.

For the complete message type code, see: http://www.cnblogs.com/KC-Mei/p/4334177.html.

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.