Windows Manager program (3)

Source: Internet
Author: User

Today, I upgraded the Windows Manager program to 1.0.0.3.

Add the remote shutdown function and the system command set function. Download the program

Remote Shutdown:

Principle: first, Ping the command to determine whether the remote machine is shut down, and then shut down the remote machine through the ShutDown command.

Determines whether the remote machine is starting.

Private bool isAlive (string svr)
{
This. ping. StartInfo. Arguments = "-n 1-w 500" + svr;
This. ping. Start ();
This. ping. WaitForExit ();
String text1 = this. ping. StandardOutput. ReadToEnd ();
This.txt Results. Text = this.txt Results. Text + "Pinging host: {" + this. serverName + "}";
This.txt Results. Text = this.txt Results. Text + "\ r \ n ";
If (text1.Contains ("cocould not find "))
{
This.txt Results. Text = this.txt Results. Text + "Host {" + this. serverName + "} cannot be resolved. \ nTry again using IP address? ";
This.txt Results. Text = this.txt Results. Text + "\ r \ n ";
This. resetForm ();
Return false;
}
If (text1.Contains ("TTL = "))
{
Return true;
}
This.txt Results. Text = this.txt Results. Text + "Host {" + this. serverName + "} is not alive... Sleeping... Try next 30 s ";
This.txt Results. Text = this.txt Results. Text + "\ r \ n ";
This. tmrWait. Stop ();
This. tmrWait. Interval = 30*1000;
This. tmrWait. Start ();
Return false;
}
To disable a remote machine, follow these steps:

Private bool killHost (string svr)
{
Bool flag1;
This. shutdown. StartInfo. Arguments = "-f-s-t 0/m" + this. serverName;
If (clsAuth. strPassword! = "") & (ClsAuth. strUsername! = ""))
{
This. shutdown. StartInfo. UserName = clsAuth. strUsername;
SecureString text1 = new SecureString ();
Char [] chrArray1 = new char [clsAuth. strPassword. Length];
For (int num1 = 0; num1 <clsAuth. strPassword. Length; num1 ++)
{
ChrArray1 [num1] = clsAuth. strPassword [num1];
}
For (int num2 = 0; num2 <chrArray1.Length; num2 ++)
{
Char chr1 = chrArray1 [num2];
Text1.AppendChar (chrArray1 [num2]);
}
This. shutdown. StartInfo. Password = text1;
}
Try
{
This. shutdown. Start ();
This. shutdown. WaitForExit ();
String text2 = this. shutdown. StandardError. ReadToEnd ();
If (text2.ToString ()! = "")
{
If (text2.Contains ("denied "))
{
This. resetForm ();
Return false;
}
This.txt Results. Text = this.txt Results. Text + "Error Information: \ n" + text2.ToString ();
This. resetForm ();
Return false;
}
This.txt Results. Text = this.txt Results. Text + "Host accepted shutdown command. Waiting 60 seconds ...";
This.txt Results. Text = this.txt Results. Text + "\ r \ n ";
This. tmrWait. Stop ();
This. tmrWait. Interval = 60*1000;
This. tmrWait. Start ();
Flag1 = true;
}
Catch (Exception exception1)
{
MessageBox. Show ("An Error has Occured:" + exception1.Message );
This. resetForm ();
Flag1 = false;
}
Return flag1;
}

Call the KillHOst method when the remote machine can be pinged.

How to obtain the IP addresses of other machines in the LAN:

Public void GetIPAddress ()
{
DirectoryEntry entryPC = new DirectoryEntry ("WinNT :");
ArrayList arr = new ArrayList ();
Foreach (DirectoryEntry child in entryPC. Children)
{
UpdateResult ("obtaining lan ip \ r \ n ");

UpdateResult (child. SchemaClassName + ":" + child. Name );
Foreach (DirectoryEntry pc in child. Children)
{
If (String. Compare (pc. SchemaClassName, "computer", true) = 0)
{

Try
{
IPHostEntry hostent = Dns. GetHostByName (pc. Name );

Array addrs = hostent. AddressList;
IEnumerator it = addrs. GetEnumerator ();

While (it. MoveNext ())
{
IPAddress ip = (IPAddress) it. Current;
// This.txt Results. Text + = ip. ToString ();
Arr. Add (ip. ToString ());
}
}
Catch
{
This.txt Results. Text + = pc. Name;
}
}
}
}
Foreach (string s in arr)
{
This. UpdateCombo (s );
}
UpdateResult ("\ r \ n obtaining lan ip \ r \ n ");
}

System Command set function:

This function is easy to implement, that is, to use the Process class, and then open the corresponding command.

You can Google the system commands on your own.

Global mouse implementation: (this function mainly hides the selected form when the mouse is pressed)

Steps:

1. This function uses the Hook method, so you need to define the API.

# Region About Hook
Public delegate int HookProc (int nCode, IntPtr wParam, IntPtr lParam );

[DllImport ("user32.dll", CharSet = CharSet. Auto,
CallingConvention = CallingConvention. StdCall)]
Public static extern int SetWindowsHookEx (int idHook, HookProc lpfn,
IntPtr hInstance, int threadId );

[DllImport ("user32.dll", CharSet = CharSet. Auto,
CallingConvention = CallingConvention. StdCall)]
Public static extern bool UnhookWindowsHookEx (int idHook );

[DllImport ("user32.dll", CharSet = CharSet. Auto,
CallingConvention = CallingConvention. StdCall)]
Public static extern int CallNextHookEx (int idHook, int nCode,
IntPtr wParam, IntPtr lParam );
[DllImport ("kernel32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr GetModuleHandle (string lpModuleName );

Public enum HookType: int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
# Endregion
2. Then there is the hook.

The method is as follows: note that the global mouse hook requires the WH_MOUSE_LL type, that is, the next low level. If you want to set the global button, you need to set the WH_KEYBOARD_LL type.

The method is as follows:

Int hHook = 0;
APIs. HookProc MouseHookeProc_Back;
Public void SetMouseHook ()
{
Using (Process process = Process. GetCurrentProcess ())
Using (ProcessModule module = process. MainModule)
{
IntPtr hModule = APIs. GetModuleHandle (module. ModuleName );
MouseHookeProc_Back = new APIs. HookProc (this. MouseHookProc );
HHook = APIs. SetWindowsHookEx (int) APIs. HookType. WH_MOUSE_LL, MouseHookeProc_Back, hModule, 0 );
}
If (hHook = 0)
{
This. ShowStatus ("MouseHook failed ");
Return;
}
This. ShowStatus ("MouseHook Succeed ");

}

3. callback function:

Public int MouseHookProc (int nCode, IntPtr wParam, IntPtr lParam)
{
If (nCode <0)
{
Return APIs. CallNextHookEx (0, nCode, wParam, lParam );
}
// APIs. MOUSEHOOKSTRUCT MyMouseHookStruct = (APIs. MOUSEHOOKSTRUCT) Marshal. PtrToStructure (lParam, typeof (APIs. MOUSEHOOKSTRUCT ));
// String strCaption = "x =" + MyMouseHookStruct.pt. x. ToString ("d") + "y =" + MyMouseHookStruct.pt. y. ToString ("d ");

// This. label2.Text = strCaption;

Switch (wParam. ToInt32 ())
{
Case APIs. MouseHookConstants. WM_MBUTTONDOWN:
{
This. OnMouseWheel ();
}
Break;
}
Return APIs. CallNextHookEx (0, nCode, wParam, lParam );

}

Private void OnMouseWheel ()
{
If (! HideItem)
{
GlassButton5_Click (null, null );
HideItem = true;
}
Else
{
ToolStripMenuItem_Click (null, null );
HideItem = false;
}
}

Okay. You only need to call the SetMouseHook method during loading.

Global keyboard method:

You can use the Hook method to implement this function. The process is roughly the same as above.

How to use other APIs in Windows Manager:

Public class WinHotKey
{
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool RegisterHotKey (
IntPtr hWnd, // window handle
Int id,
KeyModifiers fsModifiers,
Keys vk
);

[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool UnregisterHotKey (
IntPtr hWnd,
Int id
);

[Flags ()]
Public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}

Public WinHotKey (){}
}

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.