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.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
 
 
 
#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
    }
}