Hook sur le bouton droit de la souris

cirtey Messages postés 30 Date d'inscription lundi 29 janvier 2007 Statut Membre Dernière intervention 8 mai 2009 - 8 mai 2009 à 18:07
cs_tartour Messages postés 4 Date d'inscription jeudi 8 décembre 2005 Statut Membre Dernière intervention 8 septembre 2009 - 8 sept. 2009 à 11:30
Bonjour;

Environnement : winforms C# .NET 3.5

Un client me demande d'interdire le copier coller et l'impression écran
lorsque l'application que je dois lui faire est en runtime pour
protéger les données.

J'ai réussi à bloquer les touches clavier impr écran et ctrl par un global hook.

Mais pas encore réussi à désactiver le bouton droit de la souris.

Le problème est au niveau de : LowLevelMouseProc et de la struct juste en bas de cette méthode.

Merci de votre aide.

 
 
#region
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace GlobalHook
{
publicclass UserActivityHook
{
privateconstint VmRButton = 0x02;
privateconstint VkLControl = 0xA2;
privateconstint VkRControl = 0xA3;
privateconstint VkControl = 0x11;
privateconstint VkSnapshot = 0x2C;
privateconstint WhKeyboardLl = 13;
privateconstint WhMouseLl = 14;
privateconstint WmKeydown = 0x0100;
privateconstint WmMousedown =0x0201;
privatestatic IntPtr kbh_Handle;
privatestatic IntPtr kbh_Handle1;
 
privatestatic HookProc KeyboardHookProcedure;
privatestatic HookProc MouseHookProcedure;
privateint HMouseHook;
privateint HKeyboardHook;
 
#region Windows Function Imports
 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
privatestaticexternint SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
 
 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
publicstaticexternint CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);
 
 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
privatestaticexternint UnhookWindowsHookEx(int idHook);
 
 
privatedelegateint HookProc(int nCode, int wParam, IntPtr lParam);
 
#endregion
 
#region Windows constants
 
#endregion
 
~UserActivityHook()
{
//uninstall hooks and do not throw exceptions
Stop(true, true, false);

}
 
publicvoid 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.
thrownew 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.
thrownew Win32Exception(errorCode);
}
}
}
 
publicvoid Stop()
{
Stop(true, true, true);
}
 
 
publicvoid 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.
thrownew 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.
thrownew Win32Exception(errorCode);
}
}
}
 
 

 
privatestaticint LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
{
if(nCode < 0)
{
CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
return0;
}
 
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);
}
 
 
 
 
privatestaticint LowLevelMouseProc(int nCode, int wParam, IntPtr lParam)
{
if(nCode < 0)
{
CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
return0;
}
 
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)]
privatestruct Point
{
publicint X;
publicint Y;
}
 
 
 
[StructLayout(LayoutKind.Sequential)]
privatestruct MouseLlHookStruct
{
public Point Point;
publicint MouseData;
publicint Flags;
publicint Time;
publicint ExtraInfo;
}

 
#region Nested type: Kbdllhookstruct
 
[StructLayout(LayoutKind.Sequential)]
publicstruct Kbdllhookstruct
{
publicint VkCode;
publicint ScanCode;
publicint Flags;
publicint Time;
publicint ExtraInfo;
}
 
#endregion
}
}
 
 

1 réponse

cs_tartour Messages postés 4 Date d'inscription jeudi 8 décembre 2005 Statut Membre Dernière intervention 8 septembre 2009
8 sept. 2009 à 11:30
je suis
0
Rejoignez-nous