WindowsManager程序(三)

今天将WindowsManager程式升级到1.0.0.3

添加远程关机功能和系统命令集的功能. 下载程序


远程关机功能实现:

原理: 首先通过Ping的命令,来判断远程的机器是否关闭,然后通过ShutDown 的命令去关闭远程的机器.

判断远程机器是否开机中


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.txtResults.Text = this.txtResults.Text + "Pinging host: { " + this.serverName + " }";
this.txtResults.Text = this.txtResults.Text + "\r\n ";
if (text1.Contains("could not find"))
{
this.txtResults.Text = this.txtResults.Text + "Host { " + this.serverName + " } cannot be resolved.\nTry again using IP address?";
this.txtResults.Text = this.txtResults.Text + "\r\n\r\n ";
this.resetForm();
return false;
}
if (text1.Contains("TTL="))
{
return true;
}
this.txtResults.Text = this.txtResults.Text + "Host { " + this.serverName + " } is NOT ALIVE.. Sleeping...Try next 30s";
this.txtResults.Text = this.txtResults.Text + "\r\n\r\n ";
this.tmrWait.Stop();
this.tmrWait.Interval = 30*1000;
this.tmrWait.Start();
return false;
}
关闭远程机器的方法如下:

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.txtResults.Text = this.txtResults.Text + "Error Information:\n\n " + text2.ToString();
this.resetForm();
return false;
}
this.txtResults.Text = this.txtResults.Text + "Host accepted shutdown command. Waiting 60 seconds...";
this.txtResults.Text = this.txtResults.Text + "\r\n\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;
}

当判断远程机器可以ping通时,调用KillHOst的方法.



获取局域网中其他机器的IP的方法:

public void GetIPAddress()
{
DirectoryEntry entryPC = new DirectoryEntry("WinNT:");
ArrayList arr = new ArrayList();
foreach (DirectoryEntry child in entryPC.Children)
{
UpdateResult("正在获取局域网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.txtResults.Text += ip.ToString();
arr.Add(ip.ToString());
}
}
catch
{
this.txtResults.Text += pc.Name;
}
}
}
}
foreach (string s in arr)
{
this.UpdateCombo(s);
}
UpdateResult("\r\n获取局域网IP完成\r\n\n");
}

系统命令集功能:


这个功能实现就比较简单了,就是使用Process类了,然后打开相应的命令就可以了。

关于系统命令,可以自己Google,网上有好多。


全局鼠标的实现:(该功能主要当判断鼠标中健按下时,隐藏选中的窗体)

实现步骤:

1.这个功能实现使用Hook的方法,所以开始需要定义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.然后就是下钩子了。

方法如下:注意哦,全局鼠标钩子要WH_MOUSE_LL 这个类型,即下一个low level 的。如果要设定全局按键,就相应要下WH_KEYBOARD_LL 这个类型的。

方法如下

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. 回调函数:

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;
}
}


ok了,只要在loading的时候,调用SetMouseHook的方法就可以了。


全局键盘的方法:

该功能的实现可以使用Hook的方法,大概过程如上。

在WindowsManager中使用其他API的方法:


public class WinHotKey
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
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() { }
}


你可能感兴趣的:(windows)