المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Hook on right button mouse



C# Programming
05-07-2009, 09:22 PM
Hello;
I'm building an application in winforms c# 3.0. I want to disable the mouses's right button click when the application is running on all the system (in order to prevent copy/paste).
I succeeded in blocking the keyboard keys but not the mouse button. Can you figure out why the code for the mouse does not work?
The problem comes from LowLevelMouseProc()
Thanks for help.



#region

using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

#endregion

namespace GlobalHook
{
public class UserActivityHook
{
private const int VmRButton = 0x02;
private const int VkLControl = 0xA2;
private const int VkRControl = 0xA3;
private const int VkControl = 0x11;
private const int VkSnapshot = 0x2C;
private const int WhKeyboardLl = 13;
private const int WhMouseLl = 14;
private const int WmKeydown = 0x0100;
private const int WmMousedown =0x0201;
private static IntPtr kbh_Handle;
private static IntPtr kbh_Handle1;

private static HookProc KeyboardHookProcedure;
private static HookProc MouseHookProcedure;
private int HMouseHook;
private int HKeyboardHook;

#region Windows Function Imports

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);


[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);


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


private delegate int HookProc(int nCode, int wParam, IntPtr lParam);

#endregion

#region Windows constants

#endregion

~UserActivityHook()
{
//uninstall hooks and do not throw exceptions
Stop(true, true, false);

}

public void Start()
{

if (HMouseHook == 0)
{
// Create an instance of HookProc.
MouseHookProcedure = new HookProc(LowLevelMouseProc);
//install hook
HMouseHook = SetWindowsHookEx(WhMouseLl, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
//If SetWindowsHookEx fails.
if (HMouseHook== 0)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(true, false, false);
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);
}
}



// install Keyboard hook only if it is not installed and must be installed
if (HKeyboardHook == 0)
{
// Create an instance of HookProc.
KeyboardHookProcedure = new HookProc(LowLevelKeyboardProc);
//install hook
HKeyboardHook = SetWindowsHookEx(WhKeyboardLl, KeyboardHookProcedure,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
0);
//If SetWindowsHookEx fails.
if (HKeyboardHook == 0)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(false, true, false);
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);
}
}
}

public void Stop()
{
Stop(true, true, true);
}


public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook, bool throwExceptions)
{
if (HMouseHook != 0 && uninstallMouseHook)
{
//uninstall hook
int retMouse = UnhookWindowsHookEx(HMouseHook);
//reset invalid handle
HMouseHook = 0;
//if failed and exception must be thrown
if (retMouse == 0 && throwExceptions)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);
}
}

//if keyboard hook set and must be uninstalled
if (HKeyboardHook != 0 && uninstallKeyboardHook)
{
//uninstall hook
int retKeyboard = UnhookWindowsHookEx(HKeyboardHook);
//reset invalid handle
HKeyboardHook = 0;
//if failed and exception must be thrown
if (retKeyboard == 0 && throwExceptions)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);
}
}
}




private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode < 0)
{
CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
return 0;
}

if (wParam == WmKeydown)
{
IntPtr kbdll = lParam;
Kbdllhookstruct kbdllstruct = (Kbdllhookstruct) Marshal.PtrToStructure(kbdll, typeof (Kbdllhookstruct));

if ((kbdllstruct.VkCode == VkSnapshot) || (kbdllstruct.VkCode == VkControl) || (kbdllstruct.VkCode == VkLControl) || (kbdllstruct.VkCode == VkRControl))
return -1;
}

return CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
}




private static int LowLevelMouseProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode < 0)
{
CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
return 0;
}

if (wParam == WmMousedown)
{
IntPtr msdll = lParam;
MouseLlHookStruct msdllstruct = (MouseLlHookStruct)Marshal.PtrToStructure(msdll, typeof(MouseLlHookStruct));

if (msdllstruct.MouseData == VmRButton)
return -1;
}

return CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
}


[StructLayout(LayoutKind.Sequential)]
private struct Point
{
public int X;
public int Y;
}



[StructLayout(LayoutKind.Sequential)]
private struct MouseLlHookStruct
{
public Point Point;
public int MouseData;
public int Flags;
public int Time;
public int ExtraInfo;
}


#region Nested type: Kbdllhookstruct

[StructLayout(LayoutKind.Sequential)]
public struct Kbdllhookstruct
{
public int VkCode;
public int ScanCode;
public int Flags;
public int Time;
public int ExtraInfo;
}

#endregion
}
}