0

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?

  • First of all, the `Exception.Message` gives you a hint about what could possible be wrong with your code in a rather clear text. The other big code block you posted is the `Exception.Stacktrace`, containing detailed information about the origin of your error. – M463 Jul 16 '15 at 12:09
  • @M463 what must i do now sir? :( – Donquixote Adiyatma Jul 16 '15 at 12:17
  • Try my answer below. – M463 Jul 16 '15 at 12:18
  • possible duplicate of [How to avoid a Win32 exception when accessing Process.MainModule.FileName in C#?](http://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c) – GSerg Jul 16 '15 at 12:33

2 Answers2

2

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.

Community
  • 1
  • 1
M463
  • 2,003
  • 3
  • 23
  • 39
  • Error 1 Identifier expected. c:\users\adiyatma\documents\visual studio 2012\Projects\\Form1.vb 40 44, if i run debugging, it's show error code because not valid, can you help me sir? http://prntscr.com/7tcty1 , and it's VB.net xD – Donquixote Adiyatma Jul 16 '15 at 12:20
  • Please try `Modules[0]` instead of `MainModule[0]`. – M463 Jul 16 '15 at 12:23
  • Sorry, C#-habit: on VB.NET you have to use round brackets to access an array field of course. I edited my answer (changed `Modules[0]` to `Modules(0)`) and added another approach that may help you with analyzing your error. – M463 Jul 16 '15 at 12:48
  • how to write that in listbox sir? – Donquixote Adiyatma Jul 16 '15 at 12:53
  • Just change `Console.WriteLine(p.Modules(0).FileName)` to `ListBox1.Items.Add(p.Modules(0).FileName)` – M463 Jul 16 '15 at 12:57
  • It seems like the process throwing that error is just the **Idle**-process, as it is the last entry in your `ListBox1`. As the purpose of the **Idle**-process is just to count the percentage of time, at which the processor is in *idle state*, it seems ok to me that there are no modules to enumerate on that process. So, you're code looks working to me, just ignore the *Idle*-process and you should be fine. – M463 Jul 16 '15 at 13:12
  • Hmm, why in some filepath the text is "Error - Access Denied" and "A 32 bit processes cannot access modules of a 64 bit process" so what must i do?, Thank you before :/ – Donquixote Adiyatma Jul 16 '15 at 13:16
  • Because you told your code to inform you with a `ListBox`-Item in the syntax of *Error - `Exception.Message`* about the reason why your code failed on a particular process. And the reasons are right there - **access restriction** and **the attempt to access a 64Bit-process (some of the processes you retrieved with `.GetProcesses()`) from an 32Bit-process (your VB.NET program)**. – M463 Jul 16 '15 at 13:23
1

Mate the problem is obvious, you are targeting 32-bit but are testing your app on a computer that has 64-bit installed system, that's why you get the error. Message=A 32 bit processes cannot access modules of a 64 bit process.

to fix this issue you should make the target 64-bit, there IS NO SOLUTION for this... if you know the famous procexp (sysinternals) it has two separate application, when the system is 32-bit it lauches an instance of 32-bit, but when it is 64-bit it lauches another separate process for the system... so if you want to deal with this issue, you have to make two instances for system compability, Hope this is helpful