Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't catch NullReferenceExcpetion

Status
Not open for further replies.

dcushnie

Programmer
Feb 2, 2005
28
CA
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.
 
Try removing the inner Try...Catch block and have the offending code be part of the outer Try...Catch.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
I just tried your suggestion and it didn't make any difference.
When I go through it line by line I can see that pn does get the name of the process put in it correctly but if I watch p I can see that the value for Responding is NullReferenceException.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top