.net - Process.Modules 随机给出不同的进程名称
问题描述
我试图启动 %PATH% 环境变量中存在的进程(没有完整路径),并通过 Process Objects Modules 属性获取其路径。
大多数情况下它都有效,但有时 Modules 属性包含路径C:\Windows\SYSTEM32\ntdll.dll
这是一个简单的 vb.net 代码来重现该问题
Sub checkNtdllError()
' Run in a While Loop as this issues is reproduced rarely
While True
Dim psi = New ProcessStartInfo
With psi
.FileName = "cmd.exe"
.Arguments = "/c echo 'Hello'"
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.UseShellExecute = False
End With
Try
'Start a new Process of cmd.exe (from the %PATH% environment variable)
Dim proc = Process.Start(psi)
Dim moduleName = "Unknown"
If proc.Modules.Count > 0 Then
moduleName = proc.Modules(0).FileName
Else
If Debugger.IsAttached Then Debugger.Break()
' Question: Why proc.Modules.Count was 0?
Continue While
End If
Console.WriteLine(moduleName) ' This should print the full path of cmd.exe in the console
If Not moduleName.Contains("cmd") Then
' Question: Why was the ModuleName ntdll.dll?
Dim stdOut = proc.StandardOutput.ReadToEnd()
Dim stdErr = proc.StandardError.ReadToEnd
proc.WaitForExit()
proc.Dispose()
Dim procOut = stdOut & stdErr
Console.WriteLine(" " & procOut)
' Observer here that even when the Module Name was not cmd.exe, still the process output was correct
If Debugger.IsAttached Then Debugger.Break()
Console.WriteLine("--------------------------------------------------")
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End While
Console.Read()
End Sub
期望 如果在路径中找到 cmd.exe,它应该总是在 ModuleName 中打印 cmd.exe 的路径
问题:
- 为什么有时 proc.Modules 数组是空的?
- 为什么有时 proc.Modules 数组包含 ntdll.dll 的路径?
解决方案
推荐阅读
- node.js - 定义变量时无法读取未定义的属性“查找”
- typescript - ngrx - for 循环 - 完成当前元素的处理后获取下一个元素
- c++ - 在函数和线程安全中使用静态变量时的隐藏分支
- keras - 克隆使用 lambda 层和 Keras 中用户定义函数的模型时找不到函数
- python - 在 Azure Web Apps 中设置 Python 服务器
- javascript - 如何使用node js检查文件中有多少行代码
- php - 如何在codeigniter中显示应用于电子商务网站中产品的过滤器数量?
- c++ - VS Code C++ program does not display any output while debugging
- c# - 将一个以上的班级列表添加到一个班级列表中
- ethereum - 我的智能合约导致错误:气体不足错误