-3

Besides the wrong use of DLLs, when I try to use the theTestValue inside the IntPtr method the IntelliSense marks it as fail. I would like to know why this is happening because I need to use a bool from outside inside this method.

public partial class Form1 : Form
{

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

    private static LowLevelKeyboardProc _proc = HookCallback;
    private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, IntPtr lParam);

    public bool theTestValue = false; //This is the value

    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        theTestValue = true; //Red marked

        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
Idos
  • 15,053
  • 14
  • 60
  • 75
user3772108
  • 854
  • 2
  • 14
  • 32
  • 1
    Read the error, perhaps? You're trying to set an instance member from a static method. – CodeCaster Feb 20 '16 at 09:01
  • Thanks for this glory hint. When in real I don't want to spent more time on this, because this IntPtr Dll MSDN stuff making me sick. I just want to use this value inside this code I got from an code example. – user3772108 Feb 20 '16 at 09:05
  • 1
    Not related at all to int pointers at all, basic C# syntax. I mean you also have a dangling `}`, causing your `return` statement to be outside a method. You also don't seem to use `_proc` anywhere. – CodeCaster Feb 20 '16 at 09:08
  • Thanks this is because I just copy and pasted a part of the full code that bugs me. I have done a C# tutorial before but this wasn't that good. Now I try to realise some program for my brother. But this IntPtr stuff is to high for me now. After that I search for some basic knowledge. – user3772108 Feb 20 '16 at 09:28
  • 1
    Any reason why you are toying with hooks if as you say, it is "making you sick"? Seems a bit advanced just now. Anyway, let us know how you go and if you have any other probs post them here and we will be happy to help. Wishing you well :) –  Feb 20 '16 at 09:35
  • It is because my programm need to listen to all key input. Then it should display this pressed keys. Unfortunately my first version with the c# way didn't work outside the active window. Thanks this is really nice. :) – user3772108 Feb 25 '16 at 21:31

2 Answers2

3

You cannot access the field because the method is static and the field has been declared on instance level (not static). If you change the code so that both are either static or not, the error will be gone.

Markus
  • 20,838
  • 4
  • 31
  • 55
1

Make theTestValue static or remove the static modifier from your HookCallBack function.If the class is declared static all of the members must be static too.

kopeue
  • 48
  • 4