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);
/*
HHOOK
WINAPI
QASetWindowsJournalHook(
int nFilterType,
HOOKPROC pfnFilterProc,
EVENTMSG *pfnEventMsg
);
*/
#endif
/*
* From pwinuser.h
*
typedef struct tagEVENTMSG {
UINT message;
UINT paramL;
UINT paramH;
DWORD time;
HWND hwnd;
} EVENTMSG, *PEVENTMSGMSG, NEAR *NPEVENTMSGMSG, FAR *LPEVENTMSGMSG;
*/
public struct JournalHookStruct
{
public int message;
public int paramL;
public int paramH;
public int time;
public IntPtr hwnd;
}
/*
* From pwinuser.h
*
#define WH_JOURNALRECORD 0
#define WH_JOURNALPLAYBACK 1
#define WH_KEYBOARD_LL 20
*/
internal enum HookType
{
JournalRecord = 0,
JournalPlayback = 1,
KeyboardLowLevel = 20
}
internal delegate int HookProc(HookCode nCode, IntPtr wParam, IntPtr
lParam);
That should get you a long ways there.
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
could you teach me where is wrong ?
I almost try everything out .....
"Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> 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
http://community.OpenNETCF.com
Hi Chris,
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Permissions;
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 =
MouseHookProc;
public static IntPtr pMouseHookProcedure =
Marshal.GetFunctionPointerForDelegate(MouseHookProcedure);
public static int MouseHookProc(int nCode, IntPtr wParam,
IntPtr lParam)
{
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 int message;
public Int16 x;
public Int16 y;
public int paramH;
public int time;
public int hwnd;
}
}
Appreciate,
JB.
"Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> 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.
-Chris
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Permissions;
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 =
MouseHookProc;
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,
lParam);
}
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
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 int message;
public Int16 x;
public Int16 y;
public int paramH;
public int time;
public int hwnd;
}
}
Appreciate for your help.
JB.
OK, I have to say I am newbie to handle something : D
Everything great now
Appreciate! Chris!
"Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> 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
http://community.OpenNETCF.com
Hi Chris,
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!
J.B.
"Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> 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
http://community.OpenNETCF.com
Hi All,
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 lParam)
{
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!
JB