JournalHook can not help doing...

Re: QASetWindowsJournalHook by managed code
microsoft.public.pactframework
Re: QASetWindowsJournalHook by managed code
I feel so frustrated, it just can't work.
I compared your code, I only found the differences below:
1. QASetWindowsJournalHook return IntPtr, my is return Int.
2. JournalHookStruct , my is a class.
3. delegate int HookProc, my is public
I don't think they are problems, however, I still tried everything became
the same as yours, but still can't work ( will crash after executed any
other program )
Here is my latest code, could you help to review ? appreciate.
//////////////////////////////////////////////////////////////////////
using System.L
using System.Collections.G
using System.D
using System.D
using System.T
using System.Windows.F
using System.Runtime.InteropS
namespace JB
public partial class Form1 : Form
public struct JournalHookStruct
public int paramL;
public int paramH;
public IntP
public static IntPtr hH
public static IntPtr GwP // for MouseHookProc
public static IntPtr GlP
// for MouseHookProc
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr
public static JournalHookStruct myHookMessage = new
JournalHookStruct();
public static HookProc MouseHookProcedure = new
HookProc(MouseHookProc);
public static IntPtr pMouseHookProcedure =
Marshal.GetFunctionPointerForDelegate(MouseHookProcedure);
/// &summary&
try to keep everything alive
/// &/summary&
public static GCHandle gc1 = GCHandle.Alloc(myHookMessage,
GCHandleType.Pinned);
public static GCHandle gc2 = GCHandle.Alloc(MouseHookProcedure,
GCHandleType.Pinned);
public static GCHandle gc3 = GCHandle.Alloc(pMouseHookProcedure,
GCHandleType.Pinned);
public static GCHandle gc4 = GCHandle.Alloc(hHook,
GCHandleType.Pinned);
public static GCHandle gc5 = GCHandle.Alloc(GwParam,
GCHandleType.Pinned);
public static GCHandle gc6 = GCHandle.Alloc(GlParam,
GCHandleType.Pinned);
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr
while (false)
int a = 1;
GwParam = wP
GlParam = lP
return NativeDLLs.CallNextHookEx(gc4.AddrOfPinnedObject(),
nCode, gc5.AddrOfPinnedObject(), gc6.AddrOfPinnedObject()); // pass
wParam / lParam directly, the result is the same.
public Form1()
InitializeComponent();
hHook = NativeDLLs.QASetWindowsJournalHook(0,
pMouseHookProcedure, gc1.AddrOfPinnedObject()); // whatever pass
MouseHookProcedure , address of gc2, target of gc3, result are the same =&
will crash after executed other program
public class NativeDLLs
public enum HookType
JournalRecord = 0,
JournalPlayback = 1,
KeyboardLowLevel = 20
[DllImport(&coredll.dll&, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string mod);
[DllImport(&coredll.dll&, SetLastError = true)]
public static extern IntPtr QASetWindowsJournalHook(HookType
nFilterType, IntPtr pfnFilterProc, IntPtr pfnEventMsg);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport(&CoreDll.dll&, EntryPoint = &QAUnhookWindowsJournalHook&,
SetLastError = true)]
public static extern int QAUnhookWindowsJournalHook(IntPtr hook);
[DllImport(&CoreDll.dll&, CharSet = CharSet.Auto/*,CallingConvention
= CallingConvention.StdCall)*/)]
public static extern int CallNextHookEx(IntPtr idHook, int nCode,
IntPtr wParam, IntPtr lParam);
&Chris Tacke, eMVP& &ctacke.at.& wrote in message
Just digging though code I have, I see this:
#if !DESKTOP
[DllImport(&coredll.dll&, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string mod);
[DllImport(&coredll.dll&, SetLastError = true)]
public static extern IntPtr QASetWindowsJournalHook(HookType
nFilterType, HookProc pfnFilterProc, ref JournalHookStruct pfnEventMsg);
QASetWindowsJournalHook(
nFilterType,
pfnFilterProc,
*pfnEventMsg
* From pwinuser.h
typedef struct tagEVENTMSG {
} EVENTMSG, *PEVENTMSGMSG, NEAR *NPEVENTMSGMSG, FAR *LPEVENTMSGMSG;
public struct JournalHookStruct
public int paramL;
public int paramH;
public IntP
* From pwinuser.h
#define WH_JOURNALRECORD
#define WH_JOURNALPLAYBACK
#define WH_KEYBOARD_LL
internal enum HookType
JournalRecord = 0,
JournalPlayback = 1,
KeyboardLowLevel = 20
internal delegate int HookProc(HookCode nCode, IntPtr wParam, IntPtr
That should get you a long ways there.
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
&Jack P& && wrote in message
could you teach me where is wrong ?
I almost try everything out .....
&Chris Tacke, eMVP& &ctacke.at.& wrote in message
Now it looks like you're just throwing code at the wall hoping something
will stick. You need to go back and reasearch what a GCHandle is and
understand when you should and shouldn't use them. You seem to just
have them in here to have them. You also need to see how
GetFunctionPointerForDelegate works and exactly what it returns. You
need to *understand* the code, not just grasp at straws.
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
&Jack P& && wrote in message
I added few lines as following for GetFunctionPointerForDelegate:
public static IntPtr pMouseHookProcedure =
Marshal.GetFunctionPointerForDelegate(MouseHookProcedure);
public static GCHandle gc3 = GCHandle.Alloc(pMouseHookProcedure,
GCHandleType.Pinned);
and changed the init_hook() as following:
public static void init_hook() /////////////////// change
(HookProc) to (IntPtr) because can't convert IntPtr back to HookProc)
hHook = LibWrap.QASetWindowsJournalHook(0,
pMouseHookProcedure, gc1.AddrOfPinnedObject());
It didn't work yet ( behave the same as before, system will crash after
execute other program )
and here is my complete code:
/////////////////////////////////////////////////////////////////
using System.Collections.G
using System.L
using System.T
using System.Runtime.InteropS
using System.Security.P
namespace ClassLibrary1
unsafe public delegate int HookProc(int nCode, IntPtr wParam, IntPtr
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
unsafe public class Class1
private const int WM_LBUTTONDBLCLK = 515;
private const int WH_JOURNALRECORD = 0;
public static int hHook = 0;
unsafe public static EVENTMSG myHookMessage = new EVENTMSG();
unsafe public static HookProc MouseHookProcedure =
MouseHookP
public static IntPtr pMouseHookProcedure =
Marshal.GetFunctionPointerForDelegate(MouseHookProcedure);
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr
return LibWrap.CallNextHookEx(hHook, nCode, wParam, lParam);
public static GCHandle gc1 = GCHandle.Alloc(myHookMessage,
GCHandleType.Pinned);
public static GCHandle gc2 = GCHandle.Alloc(MouseHookProcedure,
GCHandleType.Pinned);
public static GCHandle gc3 = GCHandle.Alloc(pMouseHookProcedure,
GCHandleType.Pinned);
public static void init_hook()
hHook = LibWrap.QASetWindowsJournalHook(0,
pMouseHookProcedure, gc1.AddrOfPinnedObject());
public class LibWrap
[DllImport(&CoreDll.dll&, SetLastError = true)]
//public static extern int QASetWindowsJournalHook(int
nFilterType, HookProc pfnFilterProc, ref EVENTMSG pfnEventMsg);
public static extern int QASetWindowsJournalHook(int
nFilterType, IntPtr pfnFilterProc, IntPtr pfnEventMsg);
[DllImport(&CoreDll.dll&, EntryPoint =
&QAUnhookWindowsJournalHook&, SetLastError = true)]
internal static extern int QAUnhookWindowsJournalHook(int hook);
[DllImport(&CoreDll.dll&, CharSet =
CharSet.Auto/*,CallingConvention = CallingConvention.StdCall)*/)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class EVENTMSG
public Int16
public Int16
public int paramH;
Appreciate,
&Chris Tacke, eMVP& &ctacke.at.& wrote in message
You are still using these things *way* wrong. My guess (without
putting too much grey matter into it) is that you need to create a
delegate and call GetFunctionPointForDelegate and pass that. You also
shouldn't be passing the Target of the GCHandle anywhere.
&Jack P& && wrote in message
I found something wrong in my application, in fact it didn't really
work, why I didn't see crash that is because the callback didn't
assign correctly when using GCHandle.
I list my complete code as following, it packaged as C# DLL, it will
crash after perform any other program. ( if not exit this program,
everything looks fine )
using System.Collections.G
using System.L
using System.T
using System.Runtime.InteropS
using System.Security.P
namespace ClassLibrary1
unsafe public delegate int HookProc(int nCode, IntPtr wParam,
IntPtr lParam);
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
unsafe public class Class1
private const int WM_LBUTTONDBLCLK = 515;
private const int WH_JOURNALRECORD = 0;
public static int hHook = 0;
unsafe public static EVENTMSG myHookMessage = new EVENTMSG();
unsafe public static HookProc MouseHookProcedure =
MouseHookP
unsafe public static GCHandle gc1 =
GCHandle.Alloc(myHookMessage, GCHandleType.Pinned);
unsafe public static GCHandle gc2 =
GCHandle.Alloc(MouseHookProcedure, GCHandleType.Pinned);
unsafe public static int MouseHookProc(int nCode, IntPtr
wParam, IntPtr lParam) ////////////////// HERE is the hookproc
/////////////
return LibWrap.CallNextHookEx(hHook, nCode, wParam,
public static void init_hook() ////////////////// HERE is the
init /////////////
hHook = LibWrap.QASetWindowsJournalHook(0,(HookProc)
gc2.Target, gc1.AddrOfPinnedObject());
public class LibWrap
////////// class for DLL
[DllImport(&CoreDll.dll&, SetLastError = true)]
//public static extern int QASetWindowsJournalHook(int
nFilterType, HookProc pfnFilterProc, ref EVENTMSG pfnEventMsg);
public static extern int QASetWindowsJournalHook(int
nFilterType, HookProc pfnFilterProc, IntPtr pfnEventMsg);
[DllImport(&CoreDll.dll&, EntryPoint =
&QAUnhookWindowsJournalHook&, SetLastError = true)]
internal static extern int QAUnhookWindowsJournalHook(int
[DllImport(&CoreDll.dll&, CharSet =
CharSet.Auto/*,CallingConvention = CallingConvention.StdCall)*/)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class EVENTMSG
public Int16
public Int16
public int paramH;
Appreciate for your help.
&Jack P& && wrote in message
OK, I have to say I am newbie to handle
something : D
Everything great now :D
Appreciate! Chris!
&Chris Tacke, eMVP& &ctacke.at.& wrote in message
1. I don't see your definition of the P/Invoke.
2. What are the GCHandles for?
You're never using them.
3. I'm surprised it works as well as it does. My *guess* at this
point is that you got lucky and the actual callback function
address got passed in to the hook procedure (I wouldn't have
guessed it), but the address is the slot 0 address. When you
change programs, your app is no longer in slot 0, the running app
is, and so that slot 0 address now points to something altogether
different, and when the hook tries to call it it pukes.
I know these work as I've done them.
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
&Jack P& && wrote in message
Thanks for help, I changed code as follows but result is the same,
regarding the leakage, I guess you mean the GCHandle never free in
code, I guess it will OK because enough memory for leakage ...
just to make sure everything should exist in memory all the time
public class Class1
public static int hHook = 0;
public static EVENTMSG myHookMessage = new EVENTMSG();
public static GCHandle gc1 = GCHandle.Alloc(myHookMessage,
GCHandleType.Pinned);
public static GCHandle gc2 =
GCHandle.Alloc(MouseHookProcedure, GCHandleType.Pinned);
public static GCHandle gc3 = GCHandle.Alloc(hHook,
GCHandleType.Pinned);
public static void init_hook()
myHookMessage.hwnd = 0; ;
myHookMessage.message = 0;
myHookMessage.paramH = 0;
myHookMessage.time = 0;
hHook = QASetWindowsJournalHook(0, MouseHookProcedure,
ref myHookMessage);
The Class1 was packaged to DLL then called from my application, I
am not sure it will help or not.
or I should package Native C DLL to make sure no memory collection
I still can't get my application work .... please help,
appreciate!
&Chris Tacke, eMVP& &ctacke.at.& wrote in message
My guess is that the gchandle is going out of scope and getting
collected. If not you still have a memory leak.
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
&Jack P& && wrote in message
I am trying QASetWindowsJournalHook() in c#, it works but having
some problem.
I can receive the hooked message correctly ( mouse move, click,
x , y ...etc ), but as long as I do something as below, the
system (ppc) will crash.
1. Run my c# QASetWindowsJournalHook application, working.
2. Click [Start], my application is still working, message
received well.
3. Click [Today] or [Setup] or [IE] ... whatever the next
program is .... the while system will crash ( stop there then
can't do anything .... )
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr
return CallNextHookEx(hHook, nCode, wParam, lParam);
I also tried to GCHandle everything but result is the same ...
public static void init_hook()
EVENTMSG myHookMessage = new EVENTMSG();
myHookMessage.message = 0;
myHookMessage.paramH = 0;
myHookMessage.time = 0;
GCHandle gc1 = GCHandle.Alloc(myHookMessage,
GCHandleType.Pinned);
GCHandle gc2 = GCHandle.Alloc(MouseHookProcedure,
GCHandleType.Pinned);
GCHandle gc3 = GCHandle.Alloc(hHook, GCHandleType.Pinned);
hHook = QASetWindowsJournalHook(0, MouseHookProcedure, ref
myHookMessage);
and I also tried to package the hook functions into a c# DLL ...
but result is the same ... will also crash ...
Can somebody help ?
Appreciate!
Current&Thread
Re: QASetWindowsJournalHook by managed code, (continued)
, Chris Tacke, eMVP
, Chris Tacke, eMVP
, Chris Tacke, eMVP
, Chris Tacke, eMVP
, Chris Tacke, eMVP
Re: QASetWindowsJournalHook by managed code,
, Chris Tacke, eMVP
Previous&by&Date:&
, Simon Hart [MVP]
Next&by&Date:&
, Chris Tacke, eMVP
Previous&by&Thread:&
, Chris Tacke, eMVP
Next&by&Thread:&
, Chris Tacke, eMVP

参考资料

 

随机推荐