cirtey
Messages postés30Date d'inscriptionlundi 29 janvier 2007StatutMembreDernière intervention 8 mai 2009
-
8 mai 2009 à 18:07
cs_tartour
Messages postés4Date d'inscriptionjeudi 8 décembre 2005StatutMembreDerniè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;
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);
}
}
}