110,530
社区成员
发帖
与我相关
我的任务
分享
对“WindowsFormsApplication1!WindowsFormsApplication1.KBHook+LowLevelKeyboardProcDelegate::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。
private void button4_Click(object sender, EventArgs e)
{
KBHook kbh = new KBHook();
string s = kbh.RunKBHook();
label1.Text = s;
}
private delegate void testtttt();
private void button2_Click(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
testtttt gogogo = new testtttt(DoWork);
gogogo.BeginInvoke(null, gogogo);
//Thread workerThread = new Thread(this.DoWork);
//workerThread.Start();
}
private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show("Start!");
}
public void DoWork(){
WebServicesHelper wshelper = new WebServicesHelper();
string url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
string mname = "getCountryCityByIp";
String[] args = new String[1];
args[0] = "111.178.11.70";
string namespaceurl = "WebXml.com.cn";
object o = wshelper.InvokeWebService(namespaceurl,url, mname, args);
string[] s = (string[])o;
textBox3.Text = s[1].ToString();
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
#region 禁用系统按键
public partial class KBHook
{
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
}
private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
//禁用系统ctrl alt delete
//其他组合按键钩子
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hHook);
[DllImport("user32.dll")]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(IntPtr path);
private IntPtr hHook;
LowLevelKeyboardProcDelegate hookProc; // prevent gc
const int WH_KEYBOARD_LL = 13;
public string RunKBHook()
{
string ret="";
// hook keyboard
IntPtr hModule = GetModuleHandle(IntPtr.Zero);
hookProc = new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hModule, 0);
if (hHook == IntPtr.Zero)
{
ret = "Failed to set hook, error = " + Marshal.GetLastWin32Error();
}else{
ret="ok";
}
return ret;
}
public void StopKBHook() {
UnhookWindowsHookEx(hHook); // release keyboard hook
}
//protected override void OnExit(ExitEventArgs e)
//{
// UnhookWindowsHookEx(hHook); // release keyboard hook
// base.OnExit(e);
//}
private static int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
if (nCode >= 0)
switch (wParam)
{
case 256: // WM_KEYDOWN
case 257: // WM_KEYUP
case 260: // WM_SYSKEYDOWN
case 261: // M_SYSKEYUP
if (
(lParam.vkCode == 0x09 && lParam.flags == 32) || // Alt+Tab
(lParam.vkCode == 0x1b && lParam.flags == 32) || // Alt+Esc
(lParam.vkCode == 0x73 && lParam.flags == 32) || // Alt+F4
(lParam.vkCode == 0x20 && lParam.flags == 32) || // Alt+Space
(lParam.vkCode == 0x1b && lParam.flags == 0) || // Ctrl+Esc
(lParam.vkCode == 0x2e && (((lParam.flags & 32) == 32) && (lParam.flags & 0) == 0)) || // Ctrl+Alt+Delete
(lParam.vkCode == 0x5b && lParam.flags == 1) || // Left Windows Key
(lParam.vkCode == 0x5c && lParam.flags == 1)) // Right Windows Key
{
return 1;
}
break;
}
return CallNextHookEx(0, nCode, wParam, ref lParam);
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace KeyHookTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc hProc, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hHook);
[DllImport("kernel32.dll")]//获取模块句柄
public static extern IntPtr GetModuleHandle(string lpModuleName);
private delegate void testtttt();
private void KillTaskmgr()
{
while (true)
{
Process[] sum = Process.GetProcesses();
foreach (Process p in sum)
{
if (p.ProcessName == "taskmgr" || p.ProcessName == "cmd")
{
try
{
p.Kill();
}
catch { ;}
}
}
}
}
public struct KeyInfoStruct
{
public int vkCode; //按键键码
public int scanCode;
public int flags; //键盘是否按下的标志
public int time;
public int dwExtraInfo;
}
private const int WH_KEYBOARD_LL = 13; //钩子类型 全局钩子
private const int WM_KEYUP = 0x101; //按键抬起
private const int WM_KEYDOWN = 0x100; //按键按下
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
bool bStopMsg = false;
int hHook = 0;
GCHandle gc;
public int MethodHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
if (nCode >= 0) {
KeyInfoStruct inputInfo = (KeyInfoStruct)Marshal.PtrToStructure(lParam, typeof(KeyInfoStruct));
if (wParam == (IntPtr)WM_KEYDOWN) {//如果按键按下
textBox1.Text += "-" + ((Keys)inputInfo.vkCode).ToString() + "-";
}
if (bStopMsg)
return 1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);//继续传递消息
}
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
button1.Text = "屏蔽按键";
button1.Enabled = false;
button2.Text = "设置Hook";
testtttt gogogo = new testtttt(KillTaskmgr);
gogogo.BeginInvoke(null, gogogo);
}
private void button1_Click(object sender, EventArgs e) {
if (button1.Text == "屏蔽按键") {
bStopMsg = true;
button1.Text = "解除禁用";
} else {
bStopMsg = false;
button1.Text = "屏蔽按键";
}
}
private void button2_Click(object sender, EventArgs e) {
if (0 == hHook) {
HookProc KeyCallBack = new HookProc(MethodHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyCallBack,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
if (hHook == 0) {
MessageBox.Show("设置Hook失败");
} else {
button1.Enabled = true;
button2.Text = "卸载Hook";
gc = GCHandle.Alloc(KeyCallBack); //保持活动 避免 回调过程 被垃圾回收
}
} else {
if (UnhookWindowsHookEx(hHook)) {
hHook = 0;
button1.Enabled = false;
button2.Text = "设置Hook";
gc.Free();
} else {
MessageBox.Show("卸载失败");
}
}
MessageBox.Show(hHook.ToString());
}
}
}