Click here to Skip to main content
15,902,112 members
Home / Discussions / C#
   

C#

 
QuestionmaskedTextBox Format Pin
mostafa_h6-Apr-06 2:26
mostafa_h6-Apr-06 2:26 
AnswerRe: maskedTextBox Format Pin
scoroop6-Apr-06 3:46
scoroop6-Apr-06 3:46 
GeneralRe: maskedTextBox Format Pin
mostafa_h6-Apr-06 4:43
mostafa_h6-Apr-06 4:43 
GeneralRe: maskedTextBox Format Pin
scoroop6-Apr-06 4:54
scoroop6-Apr-06 4:54 
GeneralRe: maskedTextBox Format Pin
mostafa_h6-Apr-06 6:39
mostafa_h6-Apr-06 6:39 
AnswerRe: maskedTextBox Format Pin
Drew McGhie6-Apr-06 10:48
Drew McGhie6-Apr-06 10:48 
GeneralRe: maskedTextBox Format Pin
mostafa_h6-Apr-06 21:36
mostafa_h6-Apr-06 21:36 
Questionkeyboard hooks in C# Pin
LMHP6-Apr-06 0:45
LMHP6-Apr-06 0:45 
Author: hema

hello,
i have developed an application thats a BHO.The setsite method of IObjectWithSite installs keyboard hooks. though the hook is getting installed, its not executing the expected work. i am attaching the code below. please tell me wats wrong in this.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Web.Services;
using Microsoft.Win32;
using SHDocVw;

namespace KeyboardHooks_BHO
{
	
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	/// 
	[ComVisible(true)]
	[Guid("20C718C8-0D8C-42dc-B0CD-C7C53A23F116")]
	public class BHO	: IObjectWithSite
	{
		#region Member Variables
		protected SHDocVw.IWebBrowser2 m_pIWebBrowser2; // the browser class object
		protected SHDocVw.DWebBrowserEvents2_Event  m_pDWebBrowserEvents2; // browser events
//		private ComImports KeyHook= new ComImports();
		
		#endregion

		#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_S	= 0x53;
		private const int WH_KEYBOARD_LL=13;

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

		
		public BHO()
		{
	
			MessageBox.Show("inside BHO's constructor");
		}


		#region COM interfaces
		#region IObjectWithSite Members

		public void SetSite(object newSite)
		{
			MessageBox.Show("inside SetSite");
			object site=newSite;
			string explorer= null;
			
			if (m_pIWebBrowser2!=null)
				Release();

			if( site != null )
				Marshal.ReleaseComObject( site );

			site = newSite;
			
			if( site != null )
			{
				MessageBox.Show("inside if site!=null");
				IServiceProvider sp = site as IServiceProvider;
				m_pIWebBrowser2 = site as SHDocVw.IWebBrowser2;
				Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
				Guid riid = ExplorerGUIDs.IID_IUnknown;

				object wba;
				sp.QueryService(ref guid,ref riid,out wba );
		
				/*explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
					wba as SHDocVw.IWebBrowser2,
					typeof(WebBrowserClass));*/

				explorer=m_pIWebBrowser2.FullName.ToUpper();

				if(explorer.EndsWith("IEXPLORE.EXE"))
				{
					MessageBox.Show("inside the IEXPLORE.EXE");
//					KeyHook.Install();
					Install();
					MessageBox.Show("hooking");

				}
			}
			else
			{
				if ( explorer != null )
				{
					IServiceProvider sp = site as IServiceProvider;
					m_pIWebBrowser2 = site as SHDocVw.IWebBrowser2;
					Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
					Guid riid = ExplorerGUIDs.IID_IUnknown;

					object wba;
					sp.QueryService(ref guid,ref riid,out wba );

					explorer=m_pIWebBrowser2.FullName.ToUpper();
					//Marshal.ReleaseComObject( explorer );

					//explorer is about to close, freeing resources
					if(explorer.EndsWith("IEXPLORE.EXE"))
					{
						//KeyHook.UnInstall();
						UnInstall();
						MessageBox.Show("UnHookMe");
					}

					explorer = null;
				}
				MessageBox.Show("end of setsite");
				return;
			}

		}

		public int GetSite(ref Guid guid, out IntPtr ppvSite)
		{
			// TODO:  Add BHO.GetSite implementation
			MessageBox.Show("inside getsite");
			ppvSite = new IntPtr ();
			return 0;
		}

		#endregion

		protected void Release()
		{
			if (m_pDWebBrowserEvents2!=null)
			{
				Marshal.ReleaseComObject(m_pDWebBrowserEvents2);
				m_pDWebBrowserEvents2=null;
			}

			if (m_pIWebBrowser2 != null)
			{
				Marshal.ReleaseComObject(m_pIWebBrowser2);
				m_pIWebBrowser2 = null;
			}
		}
		
		#endregion

		#region Hook Functions

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

		public void Install()
		{
			MessageBox.Show("inside Install");
			mHook=SetWindowsHookEx(WH_KEYBOARD_LL,new KeyboardHookProcDelegate(KeyboardHookProc),IntPtr.Zero,0);
			if(mHook!=0)
			{
				MessageBox.Show("unable to install");
			}
			else
				MessageBox.Show("Success in Installing!");
		}


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

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

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

				// Insert + T  OR Alt + T
				if(vkCode == VK_T )
				{
					if(flag==LLKHF_EXTENDED)
					{
						MessageBox.Show("Play");
						ret=1;
					}
					else if((flag & LLKHF_ALTDOWN)!=0)
					{
						MessageBox.Show("Stop");
						ret=1;
					}
				}
				else if((vkCode == VK_P)&&((flag & LLKHF_EXTENDED)!=0))
				{ // Insert + P
					MessageBox.Show("pause");
					ret = 1 ;
				}
				else if((vkCode == VK_S) && ((flag & LLKHF_ALTDOWN)!=0))
				{ // Alt + S
					MessageBox.Show("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 UnInstall()
		{
			MessageBox.Show("inside UnInstall");
			if(mHook!=0)
			{
				MessageBox.Show("inside mHook!=0 ");
				UnhookWindowsHookEx(KeyboardHandle);
			}
		}


		#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 int UnhookWindowsHookEx(IntPtr hhook); 


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


	}
}


My questions are:
1.why is the hooks not working?
2.is there any issues related to the virtual key values and extended flags?
3.the hook procedure is not getting called. Is the definition correct?

hoping this is clear.
thanks and regards,
hema

hema

-- modified at 0:51 Friday 7th April, 2006
AnswerRe: keyboard hooks in C# Pin
Judah Gabriel Himango6-Apr-06 4:40
sponsorJudah Gabriel Himango6-Apr-06 4:40 
AnswerRe: keyboard hooks in C# Pin
Judah Gabriel Himango7-Apr-06 4:10
sponsorJudah Gabriel Himango7-Apr-06 4:10 
QuestionHow to Stop Screen Saver Pin
Shajeel6-Apr-06 0:40
Shajeel6-Apr-06 0:40 
AnswerRe: How to Stop Screen Saver Pin
Judah Gabriel Himango6-Apr-06 4:41
sponsorJudah Gabriel Himango6-Apr-06 4:41 
GeneralRe: How to Stop Screen Saver Pin
Shajeel6-Apr-06 18:50
Shajeel6-Apr-06 18:50 
QuestionUserControl problem Pin
snouto5-Apr-06 23:43
snouto5-Apr-06 23:43 
AnswerRe: UserControl problem Pin
Judah Gabriel Himango6-Apr-06 4:43
sponsorJudah Gabriel Himango6-Apr-06 4:43 
QuestionNotifyIcon problem Pin
tiziacaia5-Apr-06 23:36
tiziacaia5-Apr-06 23:36 
AnswerRe: NotifyIcon problem Pin
russellsoft6-Apr-06 4:25
russellsoft6-Apr-06 4:25 
GeneralRe: NotifyIcon problem Pin
tiziacaia6-Apr-06 20:02
tiziacaia6-Apr-06 20:02 
QuestionSD card properties Pin
spif20015-Apr-06 23:34
spif20015-Apr-06 23:34 
AnswerRe: SD card properties Pin
spif200111-Apr-06 22:29
spif200111-Apr-06 22:29 
QuestionCreating Enums for XML-Elements using C# Pin
Muecahit Goeksu5-Apr-06 23:17
Muecahit Goeksu5-Apr-06 23:17 
AnswerRe: Creating Enums for XML-Elements using C# Pin
russellsoft6-Apr-06 4:28
russellsoft6-Apr-06 4:28 
GeneralRe: Creating Enums for XML-Elements using C# Pin
Muecahit Goeksu6-Apr-06 9:52
Muecahit Goeksu6-Apr-06 9:52 
QuestionConnect to mysql using ByteFX Pin
wistiti55-Apr-06 23:08
wistiti55-Apr-06 23:08 
AnswerRe: Connect to mysql using ByteFX Pin
Stefan Troschuetz6-Apr-06 0:51
Stefan Troschuetz6-Apr-06 0:51 

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.