0
votes

I have the following code :


    Imports System.Management

    Module Module1
        Private MicrosoftProcs As New List(Of String)
        Private Sub FindMicrosoftProcs()
            Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
            For Each p2 As ManagementObject In searcher.Get()
                If p2("Name") <> "System" Or p2("Name") <> "System Idle Process" Or p2("Name") <> Process.GetCurrentProcess.ProcessName & ".exe" Then
                    Dim x As String = p2("ExecutablePath")
                    If Not x Is Nothing Then
                        If x.Length > 2 Then
                            Dim fvi As System.Diagnostics.FileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(p2("ExecutablePath").ToString)
                            Dim sDescription As String = fvi.CompanyName & "/" & fvi.LegalCopyright & "/" & fvi.LegalTrademarks & "/" & fvi.ProductName & "/" & fvi.FileDescription & "/"
                            If sDescription.ToLower.Contains("microsoft") Then
                                MicrosoftProcs.Add(p2("ExecutablePath"))
                                Debug.WriteLine("Microsoft process : " & p2("ExecutablePath"))
                            End If
                        End If
                    End If
                End If
            Next
        End Sub
    End Module

I am working on 64bit Windows but the code is compiled for 32bit Windows (for compatibility). If I run the code compiled for 64bit , i have not problems with the code, but if I run it compiled for 32bit I get the FileNotFoundException :


    A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.dll
    System.IO.FileNotFoundException: C:\Windows\system32\csrss.exe
       at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Maximus\Documents\Visual Studio 2010\Projects\rupe\rupe\Form1.vb:line 81

And I don't know how to fix it. Can you help me please ? Thanks in advance.

1
When i run your code in 32 or 64 bit mode i get the same results (found one file). But maybe it has something to do with this one line, did you copy and paste this or is this line a typo?: If p2("Name") "System" Or p2("Name") "System Idle Process" Or p2("Name") Process.GetCurrentProcess.ProcessName & ".exe" ThenJoe Uhren
I changed it to If p2("Name") = "System" Or p2("Name") = "System Idle Process" Or p2("Name") = Process.GetCurrentProcess.ProcessName & ".exe" Then otherwise it wouldn't comile.Joe Uhren
It should have been If p2("Name") <> "System" , but the site change it... @JoeyJoeJoeJrShabadooStoica Nicusor
If you used <> in that code, then your logic is wrong. The Or should be AndAlso instead.Chris Dunaway

1 Answers

0
votes

I cannot seem to find an exact answer to this but I am quite sure that the problem is that 32bit processes do not have permission to access the modules of a 64 bit process. This is why you have no problems with your 64bit app but the 32bit app doesn't like certain processes. The best resource I could find for this was another SO question which you can read here: System.ArgumentException and System.ComponentModel.Win32Exception when getting process information.

That being said there doesn't appear to be a way to get the file information for these 64bit processes when you are running a 32bit app. If you absolutely need this information you have no choice but to build your app as 64bit. If you don't really need this information and want to just carry on with the processes that you have access to in a 32bit app you can try something like this:

Private Sub FindMicrosoftProcs()
    Try
        Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")

        For Each p2 As ManagementObject In searcher.Get()
            If p2("Name") <> "System" Or p2("Name") <> "System Idle Process" Or p2("Name") <> Process.GetCurrentProcess.ProcessName & ".exe" Then
                Dim x As String = p2("ExecutablePath")
                If Not x Is Nothing Then
                    If x.Length > 2 Then
                        Dim fvi As System.Diagnostics.FileVersionInfo

                        Try
                            fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(p2("ExecutablePath").ToString)
                            Dim sDescription As String = fvi.CompanyName & "/" & fvi.LegalCopyright & "/" & fvi.LegalTrademarks & "/" & fvi.ProductName & "/" & fvi.FileDescription & "/"
                            If sDescription.ToLower.Contains("microsoft") Then
                                MicrosoftProcs.Add(p2("ExecutablePath"))
                                Debug.WriteLine("Microsoft process : " & p2("ExecutablePath"))
                            End If
                        Catch ex As Exception
                            nSkipped += 1
                        End Try
                    End If
                End If
            End If
        Next

        MessageBox.Show("Found " & MicrosoftProcs.Count & " and skipped " & nSkipped & " files.")
    Catch ex As Exception
        MessageBox.Show("Error:  " & ex.Message)
    End Try
End Sub