Link to home
Create AccountLog in
Avatar of smnetserv
smnetserv

asked on

CallbackOnCollectedDelegate detected

Hi,
    I am using a class to handle the keystrokes, this class is named globalKeyboardHook which is downloaded from internet. In my application there are two forms, when the first form is shown then only alphabets and number are allowed, for this i use the globalKeyboardHook class.  When i show the second form then i will call globalKeyboardHook.unhook() and make my first form invisible. Similarly when i need to show the first form i will call globalKeyboardHook.hook(). So all keys other than alphabets and numbers are blocked.  I have attached the globalKeyboardHook.cs
    This works fine but some times it is throwing an exception. The exception is CallbackOnCollectedDelegate was detected. In detail it says "A callback was made on a garbage collected delegate of type 'CafeControl!Utilities.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."
  Please help me
globalKeyboardHook.doc
Avatar of bigbadger1
bigbadger1

First get rid of globalKeyboardHook

global keyboard hook is GLOBAL meaning it catches all keystrokes the entire windows system. Unless you are trying to write a kiosk application or a keystroke logger this is NOT the code to be using. Try opening notepad and typing &%#! while your form alphanumeric form is showing: this is bound to get a reaction if you use it.

The exception speaks for itself globalKeyboardHook is getting invoked after you've made a call to unhook. The application no longer has access to the unmanaged sub system and your app is dead.


Now for the real question what should you be doing.... put this code (below) in the <form>.cs of the form that you want to allow alpha numeric only on. This will cause the form to constrain to upper and lower case alpha and numeric keys. I included some code to check numlock and if set allow the numeric keypad as well. I don't allow for . or - as being numeric you may need to add a bit of code for those cases if you want them.


oh and one more thing, STOP using globalKeyboardHook



// Add this line to the top of your form .cs file
using System.Runtime.InteropServices;
 
// An umanaged function that retrieves the states of each key (used to get numlock status)
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode); 
 
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	bool allow = false;
 
	// Extract the key stroke from the control keys
	int iKey	= ((int)keyData) & ((int)Keys.KeyCode);
	int iShift	= ((int)keyData) & ((int)Keys.Shift);
 
	// Check if numlock is set
	if ((((ushort)GetKeyState(0x90)) & 0xffff) != 0)
	{
		// numlock is set so allow for keypad numerics
		if (iKey >= (int)Keys.NumPad0 && iKey <= (int)Keys.NumPad9)
			allow = true;
	}
 
	// Allow numeric but not symbols
	if (iShift == 0 && iKey >= (int)Keys.D0 && iKey <= (int)Keys.D9)
		allow = true;
 
	// Allow upper and lower case alpha chars
	if (iKey >= (int)Keys.A && iKey <= (int)Keys.Z)
		allow = true;
 
	if(allow)
		return base.ProcessCmdKey(ref msg, keyData);			
	else
		return true;
}

Open in new window

Avatar of smnetserv

ASKER

   Thank you very much for trying out, and waiting for your advice.
    The tool which i am building is for a cyber cafe. In this case initially the system should be locked. For this i am using my first form, which covers entire screen. The user can key in the password (alphanumeric), and then after validation i will hide this form and the display the second form which will allow them to stop their usage. This second form is very small to hold a command button which i will show right top of screen, just before the form close button.
    In this case all the keys should be blocked, if you can give me some other solution that is appreciated. the user can enter only the password, all other keys such as windows key, ctrl+Alt+Del, ctrl+shift+Esc, internet keys.
ASKER CERTIFIED SOLUTION
Avatar of bigbadger1
bigbadger1

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer