I am completely in new territory here with what I am doing.
I need to get the process name of the application that was just opened. Using Windows Hooks I get the process id but then when I try to get the process name I get a NullReferenceExcpetion. Not only that, even though it is in a try catch block it doesn't go into the catch and just crashes at the end of the function.
Here is the function:
Public Overloads Function WndProc(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Try
If wMsg = uRegMsg Then
Dim lProcessID As Integer
Dim pn As String = ""
GetWindowThreadProcessId(lParam, lProcessID)
Select Case wParam
Case HSHELL_WINDOWCREATED
Dim p As Process
Try
p = Process.GetProcessById(lProcessID)
pn = p.ProcessName 'with this line commented out the program doesn't crash
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Select
Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Function
I don't care if an exception is thrown as long as I can capture the process name and have the program continue to run.
Thanks for any help.
I need to get the process name of the application that was just opened. Using Windows Hooks I get the process id but then when I try to get the process name I get a NullReferenceExcpetion. Not only that, even though it is in a try catch block it doesn't go into the catch and just crashes at the end of the function.
Here is the function:
Public Overloads Function WndProc(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Try
If wMsg = uRegMsg Then
Dim lProcessID As Integer
Dim pn As String = ""
GetWindowThreadProcessId(lParam, lProcessID)
Select Case wParam
Case HSHELL_WINDOWCREATED
Dim p As Process
Try
p = Process.GetProcessById(lProcessID)
pn = p.ProcessName 'with this line commented out the program doesn't crash
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Select
Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Function
I don't care if an exception is thrown as long as I can capture the process name and have the program continue to run.
Thanks for any help.