Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In Microsoft c# 2008 I have the following code into a Form:

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Media;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenShots
{
    public partial class ScreenShotConfigurationForm : Form
    {

        // ----- some other stuff (code, methods, etc...) is placed in here -----

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private LowLevelKeyboardProc _proc = HookCallback; // <<<< This is the row that is causing the error
        private IntPtr _hookID = IntPtr.Zero;

        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if (vkCode == 44) // 44 is the key code of PrintScreen button
                {
                    MakeScreenShot();
                }
            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        private IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())

            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]

        private extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr GetModuleHandle(string lpModuleName);
    }
}


That is giving me the following error on Microsoft C# 2008:

A field initializer cannot reference the non-static field, method, or property 'ScreenShots.ScreenShotConfigurationForm.HookCallback(int, System.IntPtr, System.IntPtr)'

What does it mean?
How can I solve it?

What I have tried:

I don't know what to try to solve the problem.
Posted
Updated 10-Aug-17 17:44pm
v3

1 solution

The compiler is telling you that it has not completed compiling the Class, and the Method body you try to reference is not available.

Create a Constructor for the Class and set the reference to the Method there:
public partial class ScreenShotConfigurationForm : Form
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    
    private LowLevelKeyboardProc _proc; 
    
    public ScreenShotConfigurationForm()
    {
         _proc = HookCallback;
    }
    
    // ----- some other stuff (code, methods, etc...) is placed in here -----
}
 
Share this answer
 
Comments
willy wonka 2 11-Aug-17 8:38am    
Thanks it works but: the following would be better because the routine will launch the initialization of the component as well:

public ScreenShotConfigurationForm()
{
InitializeComponent();
_proc = HookCallback;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900