Re: QASetWindowsJournalHook by managed code
- From: "Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com>
- Date: Fri, 3 Apr 2009 16:44:53 -0500
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
"Jack P" <private@xxxxxxxxxxxx> wrote in message
news:%23Yup05JtJHA.4452@xxxxxxxxxxxxxxxxxxxxxxx
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.
"Jack P" <private@xxxxxxxxxxxx> wrote in message
news:OkQLro8sJHA.1492@xxxxxxxxxxxxxxxxxxxxxxx
OK, I have to say I am newbie to handle something : D
Everything great now :D
Appreciate! Chris!
"Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> wrote in message
news:eHVExH7sJHA.4648@xxxxxxxxxxxxxxxxxxxxxxx
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
"Jack P" <private@xxxxxxxxxxxx> wrote in message
news:uH1pRA6sJHA.3928@xxxxxxxxxxxxxxxxxxxxxxx
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
news:eHeyiv4sJHA.4684@xxxxxxxxxxxxxxxxxxxxxxx
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
"Jack P" <private@xxxxxxxxxxxx> wrote in message
news:%239cbS40sJHA.4632@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: QASetWindowsJournalHook by managed code
- From: Jack P
- Re: QASetWindowsJournalHook by managed code
- References:
- QASetWindowsJournalHook by managed code
- From: Jack P
- Re: QASetWindowsJournalHook by managed code
- From: Chris Tacke, eMVP
- Re: QASetWindowsJournalHook by managed code
- From: Jack P
- Re: QASetWindowsJournalHook by managed code
- From: Chris Tacke, eMVP
- Re: QASetWindowsJournalHook by managed code
- From: Jack P
- Re: QASetWindowsJournalHook by managed code
- From: Jack P
- QASetWindowsJournalHook by managed code
- Prev by Date: Re: QASetWindowsJournalHook by managed code
- Next by Date: Re: QASetWindowsJournalHook by managed code
- Previous by thread: Re: QASetWindowsJournalHook by managed code
- Next by thread: Re: QASetWindowsJournalHook by managed code
- Index(es):
Loading