Hey,
I'm trying to set up a hook with the Managed Windows API (http://mwinapi.sourceforge.net/); however, whenever I do, everything crashes, and I get an error message (0x0000000) for every program, which states that the memory could not be read or written to. It forces me to restart my computer (which my program is not designed to do :P).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManagedWinapi;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        ManagedWinapi.Hooks.Hook hook
            = new ManagedWinapi.Hooks.Hook(
                ManagedWinapi.Hooks.HookType.WH_CALLWNDPROC,
                true,
                true);

        public Form1()
        {
            InitializeComponent();
            hook.Callback +=new ManagedWinapi.Hooks.Hook.HookCallback(hook_Callback);
            try
            {
                hook.StartHook();
            }
            catch (Exception) { }
        }

        private int hook_Callback(int code, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (code >= 0)
                this.Text = wParam.ToString();
            return code;
        }
    }
}

Here is the code I'm using. Any help will be very appreciated. Thanks.

Edited 6 Years Ago by bbman: n/a

Is this related to what you were asking earlier? If so, you might want to change your project type to managed library (.dll) otherwise you will have the same problem as before.
You can always create a test console application to link your DLL into. (Unless this is what you're doing already)

Secondly, your callback requires you to post an invoke to the form UI thread, otherwise an exception will be thrown.

What line is the AccessViolationException being thrown on?

Is this related to what you were asking earlier? If so, you might want to change your project type to managed library (.dll) otherwise you will have the same problem as before.
You can always create a test console application to link your DLL into. (Unless this is what you're doing already)

Secondly, your callback requires you to post an invoke to the form UI thread, otherwise an exception will be thrown.

What line is the AccessViolationException being thrown on?

Hey, thanks for the reply.
The Managed Windows API is pretty much a DLL that does all the functions etc.
I have managed to narrow it down to hook.StartHook(); as the problem

Can you post the code for the method please.

I posted everything I have. I included the DLL from the site listed above, and I have both in the directory.
The API is open source.
Thanks.

I haven't ever used that API, so unfortunately you'll know more than me on this one ^^

You may have to email the developer of the API for more information.

I have not used this API either. However in your callback you are trying to change the forms Text property.
The callback is probably executing on a sepearte thread to the UI and thus causing a problem.
As this is just a test program try using Debug.Write or Console write methods instead.
Normaly you should invoke the UI delegate to set UI control values.

I have not used this API either. However in your callback you are trying to change the forms Text property.
The callback is probably executing on a sepearte thread to the UI and thus causing a problem.
As this is just a test program try using Debug.Write or Console write methods instead.
Normaly you should invoke the UI delegate to set UI control values.

I've told him to do that already.

However, if his app is crashing exactly when he calls hook.StartHook(); then there is a high probability something is going wrong with the API.

To eliminate the possibility you should change your Text update to an Invoke. this.Invoke(new MethodInvoker(delegate { //Change Text Here })); should do fine

@ketsuekiame: Sorry, I did not see that before.

I had a look at the online help for the API.
Its possible that the API expects a LocalMessageHook for WH_CALLWNDPROC processing.
Also, did you already try changing the wrapCallback and global parameters to false.

ketsuekiame is probably right.

You may have to email the developer of the API for more information.

Edited 6 Years Ago by nick.crane: n/a

Okay,
Thanks for the help guys,
It looks like it's the wrapCallback being set to true that is causing it to crash. I'll email the developer.

This question has already been answered. Start a new discussion instead.