从 x86 应用程序获取 x64 进程主模块位置?

2023-12-06

我正在尝试获取操作系统上正在运行的进程的所有文件路径Process.GetProcesses()方法,它在 x64 .NET 应用程序下工作得很好,但是如果我尝试从 x86 .NET 应用程序迭代进程列表,情况就会发生变化,因为Process.MainModule.FileName属性抛出一个 Win32 异常(用我的本地语言),内容如下:A 32 bit process can't access to 64 bit process modules,好吧,我明白这个问题,但是我该如何解决它呢?

抛出此异常的代码示例(在x86.NET 解决方案,而不是AnyCPU):

Dim path As String = 
    Process.GetProcessesByName("Myx64Process").First.MainModule.FileName

我看到了使用 WMI 查询获取 64 位进程文件路径的替代方法,但这种方法似乎不是更有效的方法,我正在寻找更好的方法,也许通过 .NET 框架类库,而不用弄乱 WMI 接口(如果可能的话)。


查询每个进程的WMIis将会很痛苦slow。你应该做的是返回all类,然后通过进程 ID 进行“托管比较”。在下面的例子中我读到all每个类的属性并将每个类映射到其对应的Process。执行时间处理时间:165.53 ms。注意,这包括Process.GetProcesses()。不,我没有超级计算机。

(注意:您需要添加对系统管理.dll)

Imports System.Management 
Public Class Win32Process

    Public Property Caption() As String
    Public Property CommandLine() As String
    Public Property CreationClassName() As String
    Public Property CreationDate() As DateTime?
    Public Property CSCreationClassName() As String
    Public Property CSName() As String
    Public Property Description() As String
    Public Property ExecutablePath() As String
    Public Property ExecutionState() As UInt16?
    Public Property Handle() As String
    Public Property HandleCount() As UInt32?
    Public Property InstallDate() As DateTime?
    Public Property KernelModeTime() As UInt64?
    Public Property MaximumWorkingSetSize() As UInt32?
    Public Property MinimumWorkingSetSize() As UInt32?
    Public Property Name() As String
    Public Property OSCreationClassName() As String
    Public Property OSName() As String
    Public Property OtherOperationCount() As UInt64?
    Public Property OtherTransferCount() As UInt64?
    Public Property PageFaults() As UInt32?
    Public Property PageFileUsage() As UInt32?
    Public Property ParentProcessId() As UInt32?
    Public Property PeakPageFileUsage() As UInt32?
    Public Property PeakVirtualSize() As UInt64?
    Public Property PeakWorkingSetSize() As UInt32?
    Public Property Priority() As UInt32?
    Public Property PrivatePageCount() As UInt64?
    Public Property ProcessId() As UInt32?
    Public Property QuotaNonPagedPoolUsage() As UInt32?
    Public Property QuotaPagedPoolUsage() As UInt32?
    Public Property QuotaPeakNonPagedPoolUsage() As UInt32?
    Public Property QuotaPeakPagedPoolUsage() As UInt32?
    Public Property ReadOperationCount() As UInt64?
    Public Property ReadTransferCount() As UInt64?
    Public Property SessionId() As UInt32?
    Public Property Status() As String
    Public Property TerminationDate() As DateTime?
    Public Property ThreadCount() As UInt32?
    Public Property UserModeTime() As UInt64?
    Public Property VirtualSize() As UInt64?
    Public Property WindowsVersion() As String
    Public Property WorkingSetSize() As UInt64?
    Public Property WriteOperationCount() As UInt64?
    Public Property WriteTransferCount() As UInt64?

    Public Shared Function GetProcesses() As Win32Process()
        Using searcher As New ManagementObjectSearcher("select * from Win32_Process")
            Return (
                From
                    item As ManagementObject
                In
                    searcher.[Get]().Cast(Of ManagementObject)()
                Select New Win32Process() With {
                    .Caption = CType(item.Properties("Caption").Value, String),
                    .CommandLine = CType(item.Properties("CommandLine").Value, String),
                    .CreationClassName = CType(item.Properties("CreationClassName").Value, String),
                    .CreationDate = ManagementUtils.ToDateTime(item.Properties("CreationDate").Value),
                    .CSCreationClassName = CType(item.Properties("CSCreationClassName").Value, String),
                    .CSName = CType(item.Properties("CSName").Value, String),
                    .Description = CType(item.Properties("Description").Value, String),
                    .ExecutablePath = CType(item.Properties("ExecutablePath").Value, String),
                    .ExecutionState = CType(item.Properties("ExecutionState").Value, UInt16?),
                    .Handle = CType(item.Properties("Handle").Value, String),
                    .HandleCount = CType(item.Properties("HandleCount").Value, UInt32?),
                    .InstallDate = ManagementUtils.ToDateTime(item.Properties("InstallDate").Value),
                    .KernelModeTime = CType(item.Properties("KernelModeTime").Value, UInt64?),
                    .MaximumWorkingSetSize = CType(item.Properties("MaximumWorkingSetSize").Value, UInt32?),
                    .MinimumWorkingSetSize = CType(item.Properties("MinimumWorkingSetSize").Value, UInt32?),
                    .Name = CType(item.Properties("Name").Value, String),
                    .OSCreationClassName = CType(item.Properties("OSCreationClassName").Value, String),
                    .OSName = CType(item.Properties("OSName").Value, String),
                    .OtherOperationCount = CType(item.Properties("OtherOperationCount").Value, UInt64?),
                    .OtherTransferCount = CType(item.Properties("OtherTransferCount").Value, UInt64?),
                    .PageFaults = CType(item.Properties("PageFaults").Value, UInt32?),
                    .PageFileUsage = CType(item.Properties("PageFileUsage").Value, UInt32?),
                    .ParentProcessId = CType(item.Properties("ParentProcessId").Value, UInt32?),
                    .PeakPageFileUsage = CType(item.Properties("PeakPageFileUsage").Value, UInt32?),
                    .PeakVirtualSize = CType(item.Properties("PeakVirtualSize").Value, UInt64?),
                    .PeakWorkingSetSize = CType(item.Properties("PeakWorkingSetSize").Value, UInt32?),
                    .Priority = CType(item.Properties("Priority").Value, UInt32?),
                    .PrivatePageCount = CType(item.Properties("PrivatePageCount").Value, UInt64?),
                    .ProcessId = CType(item.Properties("ProcessId").Value, UInt32?),
                    .QuotaNonPagedPoolUsage = CType(item.Properties("QuotaNonPagedPoolUsage").Value, UInt32?),
                    .QuotaPagedPoolUsage = CType(item.Properties("QuotaPagedPoolUsage").Value, UInt32?),
                    .QuotaPeakNonPagedPoolUsage = CType(item.Properties("QuotaPeakNonPagedPoolUsage").Value, UInt32?),
                    .QuotaPeakPagedPoolUsage = CType(item.Properties("QuotaPeakPagedPoolUsage").Value, UInt32?),
                    .ReadOperationCount = CType(item.Properties("ReadOperationCount").Value, UInt64?),
                    .ReadTransferCount = CType(item.Properties("ReadTransferCount").Value, UInt64?),
                    .SessionId = CType(item.Properties("SessionId").Value, UInt32?),
                    .Status = CType(item.Properties("Status").Value, String),
                    .TerminationDate = ManagementUtils.ToDateTime(item.Properties("TerminationDate").Value),
                    .ThreadCount = CType(item.Properties("ThreadCount").Value, UInt32?),
                    .UserModeTime = CType(item.Properties("UserModeTime").Value, UInt64?),
                    .VirtualSize = CType(item.Properties("VirtualSize").Value, UInt64?),
                    .WindowsVersion = CType(item.Properties("WindowsVersion").Value, String),
                    .WorkingSetSize = CType(item.Properties("WorkingSetSize").Value, UInt64?),
                    .WriteOperationCount = CType(item.Properties("WriteOperationCount").Value, UInt64?),
                    .WriteTransferCount = CType(item.Properties("WriteTransferCount").Value, UInt64?)
                }
            ).ToArray()
        End Using
    End Function

End Class

Friend Class ManagementUtils

    Friend Shared Function ToDateTime(value As Object) As DateTime?
        If (value Is Nothing) Then
            Return CType(Nothing, DateTime?)
        End If
        Return ManagementDateTimeConverter.ToDateTime(CType(value, String))
    End Function

End Class

Test

Dim watch As New Stopwatch()

watch.[Start]()

Dim result As New Dictionary(Of Process, Win32Process)
Dim processes As Win32Process() = Win32Process.GetProcesses()

Process.GetProcesses().AsParallel().ForAll(
    Sub(p As Process)
        SyncLock result
            result.Add(p, (From item In processes.AsEnumerable() Where (item.ProcessId.HasValue AndAlso (CUInt(p.Id) = item.ProcessId.Value)) Select item).FirstOrDefault())
        End SyncLock
    End Sub)

watch.[Stop]()

Debug.WriteLine("Time: {0} ms, Win32ProcessCount={1}, ProcessCount={1}", watch.Elapsed.TotalMilliseconds, processes.Length, result.Count)
Debug.WriteLine("**************")
Debug.WriteLine(String.Join(Environment.NewLine, (From pair As KeyValuePair(Of Process, Win32Process) In result Select String.Format("Id={0}, Matched={1}", pair.Key.Id.ToString("X8"), (Not pair.Value Is Nothing)))))

Result

时间:165.53 毫秒,Win32ProcessCount=96,ProcessCount=96
**************
Id=00001B1C,匹配=True
Id=000019FC,匹配=True
Id=000006EC,匹配=True
Id=000007B0,匹配=True
Id=00001CC0,匹配=True
ID=00001024,匹配=True
Id=00000AC0,匹配=True
Id=0000078C,匹配=True
Id=00001BA8,匹配=True
Id=00000B7C,匹配=True
ID=00000304,匹配=True
Id=0000079C,匹配=True
Id=00000238,匹配=True
Id=00000F80,匹配=True
Id=000003C0,匹配=True
Id=00000170,匹配=True
ID=00000234,匹配=True
ID=00001634,匹配=True
ID=00000230,匹配=True
Id=00001B94,匹配=True
ID=00000540,匹配=True
ID=00001254,匹配=True
Id=00001A04,匹配=True
Id=000002EC,匹配=True
ID=00000474,匹配=True
ID=00000910,匹配=True
Id=000005B8,匹配=True
Id=000004F0,匹配=True
Id=00000114,匹配=True
Id=000015D8,匹配=True
Id=00000738,匹配=True
Id=0000144C,匹配=True
Id=0000133C,匹配=True
ID=00001384,匹配=True
Id=000007F8,匹配=True
Id=00000294,匹配=True
Id=000012BC,匹配=True
Id=00000D58,匹配=True
Id=00000B08,匹配=True
Id=00001F08,匹配=True
Id=00000AFC,匹配=True
Id=00000B04,匹配=True
ID=00001750,匹配=True
Id=000008B0,匹配=True
Id=0000199C,匹配=True
Id=000001C0,匹配=True
ID=00000970,匹配=True
ID=00000720,匹配=True
Id=0000136C,匹配=True
Id=000001B8,匹配=True
Id=000001B4,匹配=True
Id=000012A0,匹配=True
Id=00000D3C,匹配=True
Id=0000093C,匹配=True
ID=00001890,匹配=True
Id=000012D0,匹配=True
Id=000003F8,匹配=True
ID=00000330,匹配=True
Id=00000AE0,匹配=True
ID=00000954,匹配=True
Id=000002B4,匹配=True
Id=00000C64,匹配=True
Id=00000574,匹配=True
Id=00001FD4,匹配=True
Id=000018BC,匹配=True
Id=00001A44,匹配=True
Id=00000B94,匹配=True
ID=00000630,匹配=True
Id=000003E0,匹配=True
Id=00000004,匹配=True
Id=0000102C,匹配=True
Id=000005C0,匹配=True
Id=00000000,匹配=True
Id=000009D0,匹配=True
Id=00000C1C,匹配=True
Id=00000218,匹配=True
Id=00000A88,匹配=True
Id=00000B70,匹配=True
Id=000002D4,匹配=True
Id=00000398,匹配=True
Id=0000020C,匹配=True
Id=000009B8,匹配=True
Id=0000082C,匹配=True
Id=00001298,匹配=True
Id=000009B0,匹配=True
ID=00000760,匹配=True
Id=00000F40,匹配=True
Id=00000758,匹配=True
Id=00001128,匹配=True
Id=000005C8,匹配=True
Id=00000C24,匹配=True
ID=00001900,匹配=True
Id=0000124C,匹配=True
Id=00001148,匹配=True
Id=0000120C,匹配=True
Id=00000CA8,匹配=True

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 x86 应用程序获取 x64 进程主模块位置? 的相关文章

  • 计算按月分隔的两个日期之间的天数

    我需要计算两个日期之间的天数 DateTime 但有一个转折 我想知道这两天跨越的每个月有多少天 两个人有简单的方法吗 Example 我的开始日期是 30 03 2011 结束日期是 05 04 2011 那么结果应该是这样的 var r
  • System.Drawing.Image.Save 抛出ExternalException:GDI 中发生一般错误

    我有一个函数 它需要一个位图 复制它的一部分并将其保存为 8bpp tiff 结果图像的文件名是唯一的并且文件不存在 程序有权写入目标文件夹 void CropImage Bitmap map Bitmap croped new Bitma
  • 正则表达式不等于字符串

    我用正则表达式用头撞墙 我正在尝试定义一个表达式 该表达式完全排除此文本 System 不区分大小写 但可以包含 系统 这个词提供的不仅仅是这个 例子 系统 无效 系统 无效 系统 无效 系统 无效 asd 系统 有效 asd 系统 asd
  • 在 DataGridView 中隐藏行非常慢

    我在 Winforms 应用程序中有一个 DataGridView 大约有 1000 行 未绑定 和 50 列 隐藏一列需要整整 2 秒 当我想隐藏大约一半的行时 这就成为一个问题 private void ShowRows string
  • StreamReader,C#,peek

    我有一个 StreamReader 它偶尔会检查它是否有更多内容可以从简单的文本文件中读取 它使用 peek 属性 问题是 当我使用 peek 时 位置发生了变化 尽管不应该发生 FileStream m fsReader new File
  • WinForms - 表单大小错误

    我们有以下代码 private void MainForm Shown object sender EventArgs e RepositionForm private void RepositionForm Rectangle rect
  • DotNET 应用程序中的 GDI 句柄

    我的纯 DotNET 库作为非托管桌面应用程序中的插件运行 我收到了稳定的 虽然低 崩溃报告流 这些报告似乎表明 GDI 句柄存在问题 错误消息中的字体等 恢复为系统字体 各种控件的显示崩溃 不久后发生大规模崩溃 我的窗体几乎没有控件 但我
  • ASP.NET MVC 中 ModelState.AddModelError 中的关键参数有什么意义?

    我在我的控制器中添加了验证检查来修改ModelState如果验证失败 例如 private bool ValidateMoney string raw string name decimal min decimal max try var
  • TreeView:仅在子节点中存在复选框

    我需要一个树视图控件 根节点没有复选框 只有图像 所有子节点都有一个复选框 图像 C net 2 0 winforms 不是 wpf WinForms树视图默认不支持混合复选框 非复选框节点 您可以在树视图上全局启用复选框 并使用以下命令在
  • EasyNetQ 模型关闭

    我使用 EasyNetQ 实现了一个简单的 RabbitMQ 客户端 连接后 我收到一条通知 队列模型关闭 这是我的代码 var bus RabbitHutch CreateBus String Format host 0 hostName
  • 在 .Net 托管的 IronPython 脚本中设置和获取变量

    我正在尝试使用 Net 控制台应用程序中托管的 IronPython 来构建验证规则引擎的原型 我已经将脚本精简到我认为的基础内容 var engine Python CreateEngine engine Execute from Sys
  • CompileAssemblyFromDom 抛出访问被拒绝异常

    代码 using var codeProvider new CSharpCodeProvider var compilerParameter new CompilerParameters assemblies assemblyName fa
  • 使用实体框架重叠约会

    我将 asp net mvc 与实体框架一起使用 我有一个包含 startat 字段 endat 字段和 roomid 字段 称为 SpaceConfigurationId 的约会列表 并且希望查找给定房间已重复预订的约会列表 可以假设 e
  • 删除DataGridView(表)中的多行

    我有一个数据表 myTable 它与 DataGridView dgv 绑定 DataGridView dgv 有一个复选框列 我的目标是删除在按钮事件中选中的行 数据表当然会更新 现在我的代码仅适用于删除一行 不适用于多行 感谢帮助 pr
  • 为什么当要求修剪“PRN.NUL”时,TrimStart 会更多地修剪字符?

    这是代码 namespace TrimTest class Program static void Main string args string ToTrim PRN NUL Console WriteLine ToTrim string
  • 在一个数据访问层中处理多个连接字符串

    我有一个有趣的困境 我目前有一个数据访问层 它必须与多个域一起使用 并且每个域都有多个数据库存储库 具体取决于所调用的存储过程 目前 我只需使用 SWITCH 语句来确定应用程序正在运行的计算机 并从 Web config 返回适当的连接字
  • 使用 C# 中的 CsvHelper 将不同文化的 csv 解析为十进制

    C 中 CsvHelper 解析小数的问题 我创建了一个从 byte 而不是文件获取 csv 文件的类 并且它工作正常 public static List
  • VB.NET 中的静态方法实现

    我很困惑Static在 VB NET 中的实现 在 C 中 我们可以创建静态类和静态方法来为我们的应用程序编写实用方法 现在 VB NET 让我们创建Module代替静态类 如果我们在模块中创建一个方法 默认情况下它会变成静态的 但在我的应
  • 在 WPF 中使用 ReactiveUI 提供长时间运行命令反馈的正确方法

    我有一个 C WPF NET 4 5 应用程序 用户将用它来打开某些文件 然后 应用程序将经历很多动作 读取文件 通过许多插件和解析器传递它 这些文件可能相当大 gt 100MB 因此这可能需要一段时间 我想让用户了解 UI 中发生的情况
  • 从 mvc 控制器使用 Web api 控制器操作

    我有两个控制器 一个mvc控制器和一个api控制器 它们都在同一个项目中 HomeController Controller DataController ApiController 如果我想从 HomeController 中使用 Dat

随机推荐

  • facebook graph api 获取不同帐户的关注者数量

    我需要通过 facebook graph api 获取不同的用户关注者数量 我通过用户名获取用户 ID 但如何获取该用户的关注者数量 提前致谢 您在这里至少面临两个问题 1 The usernameGraph API v2 0 已弃用该字段
  • 重定向动作中的Struts2动态参数名称

    我成功地对我的 struts2 映射文件之一使用重定向操作 如下所示
  • android 调整ViewBounds 错误?

    当我在 imageView xml 中使用 android adjustViewBounds true 时 它不起作用 如果我把 setAdjustViewBounds true 在我的 ImageView 的构造函数中 它工作正常 有什么
  • 从任何角度来看 ++i 和 i+=1 有什么区别

    这是 kn king 的 c 编程 现代方法 中的一个问题 我无法理解他给出的解决方案 The expression i is equivalent to i 1 The value of both expressions is i aft
  • Django 中查询集中与多行记录相关的字段的串联

    我必须建立一对多关系的模型 我试图用它来区分记录的类型 假设第一个模型专用于书籍信息 第二个模型是某些类型 例如 A B C 并且类型表与书籍之间存在间接关系 因此每本书可以是 A B 或 C 或任何可能的组合类型 我想使用串联 或注释中的
  • 有没有办法将 twilio 中的 mulaw 音频流保存在文件中

    我正在使用 Twilio 语音流功能 但我不想使用 Twilio 录音功能 当 Twilio 开始向我的服务器发送语音流时 我想将其作为音频文件实时存储到磁盘中 我今天遇到了同样的问题 并找到了一种为 mu law 标头生成 WAVE 标头
  • 读取行时,awk $line 带有多个分隔符

    我正在尝试一个小的变化this 除了我告诉 awk 要根据第 5 个字段分割的文件的分隔符可以是冒号 或制表符 t 我做的是awk F t 单独来看 它确实打印了正确的 5 字段 但是 当我尝试将其合并到更大的命令中时 它返回以下错误 pr
  • Bash shell 十进制到二进制以 2 为基数的转换

    我正在 Bash 中寻找一种将十进制数转换为二进制数的简单方法 我有需要转换的变量 ip1 ip2 ip3 ip4 有没有一种简单的方法可以做到这一点 而无需查看每个单独的数字 我宁愿不必编写大量代码 您可以使用bc as echo oba
  • 连接来自位于不同服务器上的多个 SQL Server 数据库的表

    连接位于不同服务器上的数据库上的 SQL Server 数据库表的推荐方法是什么 所有数据库都将位于同一网络上 链接服务器可以工作 但有一些问题让我试图避免它们 随着时间的推移 它们会让从高层管理您的环境变成一场噩梦 服务器来来去去 升级等
  • 如何测试列表是否按升序排序

    这是练习的问题 编写一个函数来检查列表是否按升序排序 def ascending lst for k in range 0 len lst if lst k lt lst k 1 print Ok else print NOk the nu
  • Flask SocketIO 不会向特定房间发送数据

    我正在创建一个程序 该程序从 Flask 应用程序获取数据 并且可以将数据发送到 Flask 应用程序 并且我正在使用 Socket IO 来执行此操作 socketio emit receive data data 当发送到此时 这最终会
  • 表情符号替换 - PHP

    我需要将文本表情符号替换为 html 图像标签 我整理了以下数据 private smile array gt o 3 c gt 8 private laugh array gt D D D 8 D x D X D D D 3 8 priv
  • AutoMapper - 类型的条件映射

    我想做类似以下的事情 我想知道是否有人知道该怎么做 Mapper CreateMap
  • 在phonegap中将base64字符串转换为pdf

    在我的应用程序中 我收到了代表 PDF 的 Base64 字符串 我希望用户能够将 base64 作为 pdf 保存到他的手机上 我一直在寻找科尔多瓦文件传输插件 但需要一个可以下载文件的 服务器 路径 而不是转换 base64 字符串 有
  • swagger.json 路径和定义为空。规范中没有定义操作

    我正在开发一个 net core Web 应用程序 我正在使用 swagger 并且我已经做了所有必要的调整 不幸的是它不起作用 我只是看到No operations defined in spec 在 swagger 输出页面中 swag
  • 如何使手势识别器在动画 UIImage 视图中工作

    我在图像视图中有 5 个动画图像 并且希望允许用户根据默认 ID 点击它们并将其推送到另一个视图 我尝试添加手势点击 但图像视图未检测到 有人可以给我一些建议吗 编辑 最终我没有使用它 而是设置了一个 UIButton 谢谢 viewDid
  • Excel 2013 VBA 清除所有筛选器宏

    看来旧的宏不起作用 我有适当的安全设置来运行 VBA 宏 但是当我尝试了几种清除工作表上所有过滤器的方法时 我收到编译错误 这是我尝试过的 Sub AutoFilter Remove This macro removes any filte
  • React Native 导航:重置堆栈导航器

    我正在使用 React Navigation 5 在顶部有一个抽屉导航器 带有以下屏幕
  • 使用 Python 操作其他程序的 GUI?

    我这里有一个程序 有一个输入框和一个按钮 我希望 python 在输入框中输入一个字符串 然后按下按钮 解决这个问题的最佳方法是什么 顺便说一句 这是针对 Windows 7 的 pyWinAuto 可以很好地解决这个问题 使用它 您可以根
  • 从 x86 应用程序获取 x64 进程主模块位置?

    我正在尝试获取操作系统上正在运行的进程的所有文件路径Process GetProcesses 方法 它在 x64 NET 应用程序下工作得很好 但是如果我尝试从 x86 NET 应用程序迭代进程列表 情况就会发生变化 因为Process M