【问题标题】:Get "ALL" Filename and Path from a Running Process VB.net从正在运行的进程 VB.net 中获取“全部”文件名和路径
【发布时间】:2015-07-16 10:57:00
【问题描述】:

IT;S VB.NET 不是 C#,我不知道如何将源代码从 C# 交换到 VB.NET,谢谢

我想从所有正在运行的进程中获取所有路径。
到目前为止,这是我的来源:

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

但我无法获取正在运行的进程的所有路径文件夹。

如果我检查ex.Message,响应是这样的

无法枚举进程模块。

但如果我不使用ex.Message,响应是这样的:

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: 

谁能帮帮我?

【问题讨论】:

标签: vb.net md5 filenames


【解决方案1】:

看一下so-questionHow to get the full path of running process?

代替

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

试试

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

编辑:

您是否尝试过直接评估不同进程的属性?可能是某个进程无法访问,导致出现描述的错误。

您可以尝试通过创建以下循环来逐个迭代流程:

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

通过这样做,您应该能够确定您不被允许访问的进程并获得几个您应该能够试验的进程。

【讨论】:

  • 需要错误 1 ​​标识符。 c:\users\adiyatma\documents\visual studio 2012\Projects\\Form1.vb 40 44,如果我运行调试,它会显示错误代码,因为无效,您能帮帮我吗? prntscr.com/7tcty1,它是 VB.net xD
  • 请尝试Modules[0] 而不是MainModule[0]。
  • 抱歉,C# 习惯:在 VB.NET 上,您当然必须使用圆括号来访问数组字段。我编辑了我的答案(将 Modules[0] 更改为 Modules(0))并添加了另一种方法来帮助您分析错误。
  • 如何在列表框中写入?
  • 只需将Console.WriteLine(p.Modules(0).FileName) 更改为ListBox1.Items.Add(p.Modules(0).FileName)
【解决方案2】:

问题很明显,您的目标是 32 位,但正在安装了 64 位系统的计算机上测试您的应用程序,这就是您收到错误的原因。 Message=32 位进程无法访问 64 位进程的模块。

要解决此问题,您应该将目标设为 64 位,没有解决方案... 如果你知道著名的 procexp (sysinternals) 它有两个独立的应用程序,当系统是 32 位时,它会启动一个 32 位的实例,但是当它是 64 位时,它会为系统启动另一个单独的进程...... 所以如果你想处理这个问题,你必须为系统兼容性做两个实例, 希望对您有所帮助

【讨论】:

    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多