Donquixote Adiyatma Donquixote Adiyatma -3 years ago 160
Vb.net Question

Get "ALL" Filename and Path from a Running Process VB.net

IT;S VB.NET not C# and i don't know how to exchange source from C# to VB.NET, THANKS


I want to get all paths from all running processes.

This is my source so far:

For Each p As Process In Process.GetProcesses
Try
ListBox1.Items.Add(p.MainModule.FileName.ToString)
Catch ex As Exception
RichTextBox1.Text = ex.Message
End Try
Next


But I can't get all path folders of the running processes.

If I examine the
ex.Message
, the response is like this


Unable to enumerate the process modules.


But if I not using
ex.Message
, the response is like this :

System.ComponentModel.Win32Exception was unhandled

ErrorCode=-2147467259
HResult=-2147467259
Message=A 32 bit processes cannot access modules of a 64 bit process.
NativeErrorCode=299
Source=System
StackTrace:
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at Anti_Cheat.Form1.Button6_Click(Object sender, EventArgs e) in c:\users\adiyatma\documents\visual studio 2012\Projects\Anti Cheat\Anti Cheat\Form1.vb:line 40
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Anti_Cheat.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


Can anyone help me?

Answer Source

Take a look at the so-question How to get the full path of running process?

Instead of

ListBox1.Items.Add(p.MainModule.FileName.ToString)

try

ListBox1.Items.Add(p.Modules(0).FileName.ToString)

Edit:

Have you tried to evaluate the property of different processes directly? Maybe there is a certain process you cannot access, resulting in the error described.

You can try to iterate trough the processes one by one by creating the following loop:

For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
    Try
        Console.WriteLine(p.Modules(0).FileName)
    Catch ex As Exception
        Console.WriteLine(String.Format("{0}: Error - {1}", p.ProcessName, ex.Message))
    End Try
Next

By doing so, you should be able to determine the processes you're not allowed to access and get a couple of processes you should be able to experiment with.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download