[C#] CallbackOnCollectedDelegate-Exception

04/22/2010 13:51 n00byplay#1
Hallo epvp*^^

Ich versuch mich gerade in C# an globalen Maus- und Tastaturhooks. Der Tastatur-Hook funktioniert wie er soll, aber beim Maus-Hook habe ich ein Problem. Wenn man die Maus rumbewegt, nach dem man Hook aktiviert hat, gibt es plötzlich eine "CallbackOnCollectedDelegate-Exception".

Ich definiere ein Delegate, das ich dann der WinAPI-Funktion SetWindowsHookEx übergebe. Das Delegate wird auf Klassenebene deklariert, und im Konstruktor initialisiert. Trotzdem wird diese vom Garbage Collector eingesammelt und verursacht eine Exception, weil Windows die Funktion nicht mehr finden kann.

PHP Code:
class GlobalMouseHook    {
            [
UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
            public 
delegate int HookProc(int nCodeint wParamMouseHookStruct lParam);

            private 
HookProc _MouseHookProc;
            public const 
int WH_MOUSE_LL 14;

            public 
GlobalMouseHook()
            {
                       
_MouseHookProc = new HookProc(MouseHookProc);
            }

            private 
int MouseHookProc(int nCodeint wParamMouseHookStruct lParam)
            { ... }

            public 
bool Hook()
            {
                        
hHook SetWindowsHookEx(WH_MOUSE_LLMouseHookProc,(IntPtr)00);
                        if (
hHook == 0)
                        {
                            return 
false;
                        }
                        return 
true;
            }

            public 
bool UnHook()
            {
                        return 
UnhookWindowsHookEx(hHook);
            }

            [
DllImport("user32.dll"CharSet CharSet.Auto,
         
CallingConvention CallingConvention.StdCall)]
            public static 
extern int SetWindowsHookEx(int idHookHookProc lpfn,
        
IntPtr hInstanceint threadId);

            [
DllImport("user32.dll"CharSet CharSet.Auto,
         
CallingConvention CallingConvention.StdCall)]
            public static 
extern bool UnhookWindowsHookEx(int idHook);

            [
DllImport("user32.dll"CharSet CharSet.Auto,
         
CallingConvention CallingConvention.StdCall)]
            public static 
extern int CallNextHookEx(int idHookint nCode,
        
int wParamref MouseHookStruct lParam);

PHP Code:
[StructLayout(LayoutKind.Sequential)]
public class 
POINT
{
        public 
int x;
        public 
int y;
}

[
StructLayout(LayoutKind.Sequential)]
public class 
MouseHookStruct
{
        public 
POINT pt;
        public 
int hwnd;
        public 
int wHitTestCode;
        public 
int dwExtraInfo;

Ich hoffe ihr könnt mir helfen.

MfG,
noobyplay
04/24/2010 16:11 x]vIrus[x#2
dein moushookstruct is zu kurz, da fehlt ein int

[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int mouseData;
public int flags;
public int time;
public IntPtr dwExtraInfo;
}

hier meine testsolution:
04/24/2010 17:26 n00byplay#3
Vielen Dank für den Hinweis. Aber das Problem tritt immer noch auf, sogar bei deiner Test-Solution.

Ich hab Windows 7 64-bit, kann das damit zusammenhängen?

MfG,
noobyplay
04/24/2010 20:29 x]vIrus[x#4
ich hab auch win7 x64, und die testsolution funzt bei mir einwandfrei ,...
04/26/2010 15:54 scbiz#5
Quote:
Originally Posted by x]vIrus[x View Post
ich hab auch win7 x64, und die testsolution funzt bei mir einwandfrei ,...
Zwar unschön programmiert, jedoch ändert das nichts an der Tatsache, dass es bei mir ebenfalls einwandfrei funktioniert:
[Only registered and activated users can see links. Click Here To Register...]