Click here to Skip to main content
15,905,028 members
Home / Discussions / C#
   

C#

 
QuestionC# takes a long time to connect to SQL 7 ? Pin
rmhewa7-Apr-06 0:22
rmhewa7-Apr-06 0:22 
GeneralRe: C# takes a long time to connect to SQL 7 ? Pin
Guffa7-Apr-06 1:06
Guffa7-Apr-06 1:06 
GeneralRe: C# takes a long time to connect to SQL 7 ? Pin
rmhewa7-Apr-06 20:31
rmhewa7-Apr-06 20:31 
QuestionWorking with Files Pin
hasanali006-Apr-06 23:53
hasanali006-Apr-06 23:53 
AnswerRe: Working with Files Pin
scoroop7-Apr-06 0:20
scoroop7-Apr-06 0:20 
GeneralRe: Working with Files Pin
hasanali007-Apr-06 0:33
hasanali007-Apr-06 0:33 
GeneralRe: Working with Files Pin
scoroop7-Apr-06 0:38
scoroop7-Apr-06 0:38 
QuestionKeyboard hooks Pin
LMHP6-Apr-06 23:12
LMHP6-Apr-06 23:12 
hi All,
I am attaching the code of my C# console application. it is a keyboard hook. but its not working.. the program exits b4 any key is pressed.

using System;

namespace hooksample
{
	using System;
	using System.Runtime.InteropServices;

	namespace hooks
	{
		/// <summary>
		/// Summary description for Class1.
		/// </summary>
		public class Class1
		{
			#region Hook Variables

			public struct KeyboardHookStruct
			{
				public long vkCode;
				public long scanCode;
				public int flags;
				public long time;
				public long dwExtraInfo;

			};
			private const int HC_ACTION = 0;
			private const int LLKHF_EXTENDED = 0x01;
			private const int LLKHF_ALTDOWN = 0x20;
			private const long VK_T = 0x54;
			private const long VK_P = 0x50;
			private const long VK_W	= 0x57;
			private const int VK_TAB = 0x9;
			private const int VK_CONTROL = 0x11; // tecla Ctrl
			//Private Const VK_MENU As Long = &H12        ' tecla Alt
			private const int VK_ESCAPE = 0x1B;

			private const int WH_KEYBOARD_LL=13;

			protected IntPtr KeyboardHandle = IntPtr.Zero;
			private static int mHook;
			#endregion

			public Class1()
			{
				Install();	
			}

			public static void Main()
			{
				Class1 classnew =new Class1();
			}

			#region Hook Functions

			private delegate int KeyboardHookProcDelegate(int nCode, int wParam, int lParam); 

			public static int KeyboardHookProc(int nCode, int wParam, int lParam) 
			{
				KeyboardHookStruct HookStruct;
				int ret = 0;
				Console.WriteLine("inside HookProc");

				HookStruct = ((KeyboardHookStruct) Marshal.PtrToStructure(new IntPtr(lParam), typeof(KeyboardHookStruct)));
				long vkCode= HookStruct.vkCode;
				int flag = HookStruct.flags;

				if(nCode==HC_ACTION)
				{
					Console.WriteLine("inside nCode==HC_ACTION");
					//System.Windows.Forms.Keys  KeyPressed = (Keys)wParam.ToInt32();

					// Insert + T  OR Alt + T
					if(vkCode == VK_T )
					{
						Console.WriteLine(" found T ");
						if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
						{
							Console.WriteLine("Play");
							ret=1;
						}
						else if((flag & LLKHF_ALTDOWN)!=0)
						{
							Console.WriteLine("Stop");
							ret=1;
						}
					}
					else if(vkCode == VK_P)
					{
						Console.WriteLine(" found P ");
						if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
						{ //Insert + P
							Console.WriteLine("pause");
							ret = 1 ;
						}
					}
					else if(vkCode == VK_W) 
					{
						Console.WriteLine(" found W ");
						if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
						{
							// Alt + S
							Console.WriteLine("Save");
							ret = 1 ;
						}
					}
            	
					/*/	CopyMemory(HookStruct,lParam,sizeof(HookStruct);
						if(IsHooked(HookStruct))
							{
								MyKeyboardProc=1;
							}*/

					//	MyKeyboardProc=1;
				}
				if( ret == 0 )
				{
					ret = CallNextHookEx(mHook,nCode,wParam,lParam);
				}
				return ret;
			}
		

			public void Install()
			{
				Console.WriteLine("inside Install");
			
				//mHook=SetWindowsHookEx(WH_KEYBOARD_LL,new KeyboardHookProcDelegate(KeyboardHookProc),((Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0])).ToInt32()),0);
				mHook=SetWindowsHookEx(WH_KEYBOARD_LL,new KeyboardHookProcDelegate(KeyboardHookProc),IntPtr.Zero,(int)AppDomain.GetCurrentThreadId());
				if(mHook!=0)
				{
					Console.WriteLine("unable to install");
				}
				else
					Console.WriteLine("Success in Installing!");
			}



			public void UnInstall()
			{
				Console.WriteLine("inside UnInstall");
				if(mHook!=0)
				{
					Console.WriteLine("inside mHook!=0 ");
					UnhookWindowsHookEx(mHook);
				}
			}


			#endregion

			#region Hook Win32Imports

			// Win32: SetWindowsHookEx()
			[DllImport("user32.dll")]
			private static extern int SetWindowsHookEx(int code, 
				KeyboardHookProcDelegate func,
				IntPtr hInstance,
				int threadID);

			// Win32: UnhookWindowsHookEx()
			[DllImport("user32.dll")]
			protected static extern bool UnhookWindowsHookEx(int hhook); 


			// Win32: CallNextHookEx()
			[DllImport("user32.dll")]
			protected static extern int CallNextHookEx(int hhook, 
				int code, int wParam, int lParam);

			// Win32 : GetAsyncKeyState
			[DllImport("user32.dll")]
			private static extern short GetAsyncKeyState(int vKey);  
 
			#endregion

		
		}
	}

}


i need to know whether i have done the hook correctly?
y its not working?

waiting for a reply.
thanks in advances.
lmhp
AnswerRe: Keyboard hooks Pin
alexey N6-Apr-06 23:26
alexey N6-Apr-06 23:26 
GeneralRe: Keyboard hooks Pin
LMHP6-Apr-06 23:31
LMHP6-Apr-06 23:31 
AnswerRe: Keyboard hooks Pin
Eric Dahlvang7-Apr-06 4:29
Eric Dahlvang7-Apr-06 4:29 
QuestionReading PDF file Pin
May Thu san6-Apr-06 22:59
May Thu san6-Apr-06 22:59 
AnswerRe: Reading PDF file Pin
visalak6-Apr-06 23:23
visalak6-Apr-06 23:23 
QuestionCompare Two XML files Using C# Pin
visalak6-Apr-06 22:56
visalak6-Apr-06 22:56 
AnswerRe: Compare Two XML files Using C# Pin
sathish s6-Apr-06 23:40
sathish s6-Apr-06 23:40 
GeneralRe: Compare Two XML files Using C# Pin
visalak9-Apr-06 18:18
visalak9-Apr-06 18:18 
QuestionEvents through .NET Remoting Pin
Dario Solera6-Apr-06 21:31
Dario Solera6-Apr-06 21:31 
AnswerRe: Events through .NET Remoting Pin
S. Senthil Kumar7-Apr-06 18:42
S. Senthil Kumar7-Apr-06 18:42 
GeneralRe: Events through .NET Remoting Pin
Dario Solera7-Apr-06 20:31
Dario Solera7-Apr-06 20:31 
GeneralRe: Events through .NET Remoting Pin
S. Senthil Kumar8-Apr-06 18:58
S. Senthil Kumar8-Apr-06 18:58 
Questionany one know why the key down don't work? Pin
abstar6-Apr-06 21:24
abstar6-Apr-06 21:24 
AnswerRe: any one know why the key down don't work? Pin
scoroop6-Apr-06 21:32
scoroop6-Apr-06 21:32 
AnswerRe: any one know why the key down don't work? Pin
Roy Heil8-Apr-06 14:55
professionalRoy Heil8-Apr-06 14:55 
Questionbinary to decimal conversion Pin
eggie56-Apr-06 20:36
eggie56-Apr-06 20:36 
AnswerRe: binary to decimal conversion Pin
Guffa6-Apr-06 21:00
Guffa6-Apr-06 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.