[Mwinapi-commits] SF.net SVN: mwinapi:[109] trunk/ManagedWinapi
Status: Beta
Brought to you by:
schierlm
From: <sch...@us...> - 2011-07-14 18:26:52
|
Revision: 109 http://mwinapi.svn.sourceforge.net/mwinapi/?rev=109&view=rev Author: schierlm Date: 2011-07-14 18:26:46 +0000 (Thu, 14 Jul 2011) Log Message: ----------- Replace System.Windows.Forms.Message structure by custom structure which correctly marshals the time and POINT fields. Keep the old delegate and methods as obsolete ones to not break compatibility (thanks to Daniel Rose). Modified Paths: -------------- trunk/ManagedWinapi/Hook.cs trunk/ManagedWinapi/JournalHook.cs trunk/ManagedWinapi/PInvokeTypes.cs Modified: trunk/ManagedWinapi/Hook.cs =================================================================== --- trunk/ManagedWinapi/Hook.cs 2011-07-13 17:24:53 UTC (rev 108) +++ trunk/ManagedWinapi/Hook.cs 2011-07-14 18:26:46 UTC (rev 109) @@ -26,6 +26,7 @@ using System.Diagnostics; using System.Reflection; using System.Threading; +using ManagedWinapi.Windows; namespace ManagedWinapi.Hooks { @@ -226,20 +227,40 @@ public class LocalMessageHook : Hook { /// <summary> - /// Called when a message has been intercepted. + /// Called when a message has been intercepted. Since this callback uses an incorrect + /// <see cref="System.Windows.Forms.Message"/> marshalling, it has been replaced by the + /// <see cref="MSGOccurred"/> event. /// </summary> + [Obsolete("Use MSGOccurred instead")] public event MessageCallback MessageOccurred; /// <summary> - /// Represents a method that handles a message from a message hook. + /// Represents a method that handles a message from a message hook. Since this callback + /// uses an incorrect <see cref="System.Windows.Forms.Message"/> marshalling, it has + /// been replaced by the <see cref="MSGCallback"/> event. /// </summary> /// <param name="msg"></param> + [Obsolete("Use MSGCallback instead")] public delegate void MessageCallback(Message msg); /// <summary> - /// Creates a local message hook and hooks it. + /// Called when a message has been intercepted. /// </summary> + public event MSGCallback MSGOccurred; + + /// <summary> + /// Represents a method that handles a message from a message hook. + /// </summary> + /// <param name="msg"></param> + public delegate void MSGCallback(MSG msg); + + /// <summary> + /// Creates a local message hook and hooks it. Since this constructor + /// uses an incorrect <see cref="System.Windows.Forms.Message"/> marshalling, it has + /// been replaced by constructor taking a <see cref="MSGCallback"/>. + /// </summary> /// <param name="callback"></param> + [Obsolete] public LocalMessageHook(MessageCallback callback) : this() { @@ -248,6 +269,17 @@ } /// <summary> + /// Creates a local message hook and hooks it. + /// </summary> + /// <param name="callback"></param> + public LocalMessageHook(MSGCallback callback) + : this() + { + this.MSGOccurred = callback; + StartHook(); + } + + /// <summary> /// Creates a local message hook. /// </summary> public LocalMessageHook() @@ -260,9 +292,14 @@ { if (code == HC_ACTION) { - Message msg = (Message)Marshal.PtrToStructure(lParam, typeof(Message)); + if (MSGOccurred != null) + { + MSG msg = (MSG)Marshal.PtrToStructure(lParam, typeof(MSG)); + MSGOccurred(msg); + } if (MessageOccurred != null) { + Message msg = (Message)Marshal.PtrToStructure(lParam, typeof(MSG)); MessageOccurred(msg); } } Modified: trunk/ManagedWinapi/JournalHook.cs =================================================================== --- trunk/ManagedWinapi/JournalHook.cs 2011-07-13 17:24:53 UTC (rev 108) +++ trunk/ManagedWinapi/JournalHook.cs 2011-07-14 18:26:46 UTC (rev 109) @@ -22,6 +22,7 @@ using System.Text; using System.ComponentModel; using System.Runtime.InteropServices; +using ManagedWinapi.Windows; namespace ManagedWinapi.Hooks { @@ -45,12 +46,12 @@ : base(type, true, false) { lmh = new LocalMessageHook(); - lmh.MessageOccurred += new LocalMessageHook.MessageCallback(lmh_Callback); + lmh.MSGOccurred += new LocalMessageHook.MSGCallback(lmh_Callback); } - private void lmh_Callback(System.Windows.Forms.Message msg) + private void lmh_Callback(MSG msg) { - if (msg.Msg == WM_CANCELJOURNAL) + if (msg.message == WM_CANCELJOURNAL) { hooked = false; lmh.Unhook(); Modified: trunk/ManagedWinapi/PInvokeTypes.cs =================================================================== --- trunk/ManagedWinapi/PInvokeTypes.cs 2011-07-13 17:24:53 UTC (rev 108) +++ trunk/ManagedWinapi/PInvokeTypes.cs 2011-07-14 18:26:46 UTC (rev 109) @@ -157,4 +157,40 @@ #endregion } + /// <summary> + /// The Win32 MSG structure. + /// </summary> + [StructLayout(LayoutKind.Sequential)] + public struct MSG + { + /// <summary> + /// The window handle of the message. + /// </summary> + public IntPtr hwnd; + + /// <summary> + /// The ID number for the message. + /// </summary> + public int message; + + /// <summary> + /// The WParam of the message. + /// </summary> + public IntPtr wParam; + + /// <summary> + /// The LParam of the message. + /// </summary> + public IntPtr lParam; + + /// <summary> + /// The time of the message. + /// </summary> + public int time; + + /// <summary> + /// The POINT of the message + /// </summary> + public POINT pt; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |