C# 在 Process.Kill() 期间仅完成部分 ReadProcessMemory 或 WriteProcessMemory 请求 [英] C# Only part of a ReadProcessMemory or WriteProcessMemory request was completed during Process.Kill()

查看:87
本文介绍了C# 在 Process.Kill() 期间仅完成部分 ReadProcessMemory 或 WriteProcessMemory 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在广泛研究这个问题,但似乎找不到答案.

我知道当 32 位进程尝试访问 64 位进程时会抛出 Only一部分 ReadProcessMemory 或 WriteProcessMemory 请求 异常被抛出,对于 64 位修改也是如此一个 32 位进程.

该问题的解决方案是将平台目标更改为任何 CPU".我已经试过了,不幸的是这并不能解决我的问题.

下一段代码是不断抛出异常的.运行此代码的程序用于打开远程计算机上的应用程序,并保留程序本身打开的所有进程的列表,这样我就不必遍历所有进程.

处理 processToRemove = null;锁(_runningProcesses){foreach(_runningProcesses 中的进程 p){foreach(p.Modules 中的 ProcessModule 模块){string[] strs = text.Split('\');if (module.ModuleName.Equals(strs[strs.Length - 1])){processToRemove = p;休息;}}如果 (processToRemove != null){休息;}}如果 (processToRemove != null){processToRemove.Kill();_runningProcesses.Remove(processToRemove);}}

这些进程可以并且很可能是 32 位和 64 位,混合在一起.

有什么我不应该做的事情,或者有没有更好的方法来做这一切?

解决方案

Process.Modules 的 MSDN 页面this thread 在从 64 位进程枚举 32 位进程时,Process.Modules 中存在一个已知问题,反之亦然:<块引用>

.NET 的 Process.Modules 在内部使用函数 EnumProcessModules来自 PSAPI.dll.此功能存在无法运行的已知问题跨越 32/64 位进程边界.因此枚举另一个64 位进程从 32 位进程或反之亦然不起作用正确.

解决方案似乎是使用 EnumProcessModulesEx 函数,(必须通过 P/Invoke 调用),但此函数仅在更高版本的 Windows 上可用.

<块引用>

我们通过添加解决了这个问题一个名为 EnumProcessModulesEx 到 PSAPI.dll 的新函数(http://msdn2.microsoft.com/en-us/library/ms682633.aspx),但我们在这种情况下目前无法使用它:

  • 它仅适用于 Windows Vista 或 Windows Server 2008
  • 目前 .NET 2.0 Framework 没有服务包或修补程序来让 Process.Modules 使用这个新的 API

I have been researching this issue pretty extensively and cannot seem to find an answer.

I know that the Only part of a ReadProcessMemory or WriteProcessMemory request was completed exception is thrown when a 32-bit process tries to access a 64-bit process and the same for a 64-bit modifying a 32-bit process.

The solution to that issue is to change the Platform Target to 'Any CPU'. I have tried this and unfortunately this does not solve my issue.

The next block of code is what keeps throwing the exception. The program that runs this code is used to open up applications on remote computers and keeps a list of all the processes that the program itself opened so that I don't have to loop through all the processes.

Process processToRemove = null;
lock (_runningProcesses)
{
    foreach (Process p in _runningProcesses)
    {
        foreach (ProcessModule module in p.Modules)
        {
            string[] strs = text.Split('\');

            if (module.ModuleName.Equals(strs[strs.Length - 1]))
            {
                processToRemove = p;
                break;
            }
        }
        if (processToRemove != null)
        {
            break;
        }
    }
    if (processToRemove != null)
    {
        processToRemove.Kill();
        _runningProcesses.Remove(processToRemove);
    }
}

These processes can and most likely will be 32-bit and 64-bit, mixed together.

Is there anything I am doing that I shouldn't be doing, or is there just a better way to do all of this?

解决方案

As detailed in the comments of the MSDN page for Process.Modules and this thread there is a known issue in Process.Modules when enumerating 32 bit processes from a 64 bit process and visa-versa:

Internally .NET's Process.Modules is using function EnumProcessModules from PSAPI.dll. This function has a known issue that it cannot work across 32/64 bit process boundary. Therefore enumerating another 64-bit process from 32-bit process or vice versa doesn't work correctly.

The solution seems to be to use the EnumProcessModulesEx function, (which must be called via P/Invoke), however this function is only available on later versions of Windows.

We fixed this issue by adding a new function called EnumProcessModulesEx to PSAPI.dll (http://msdn2.microsoft.com/en-us/library/ms682633.aspx), but we currently cannot use it in this case:

  • it only works on Windows Vista or Windows Server 2008
  • currently .NET 2.0 Framework don't have a service pack or hotfix to make Process.Modules use this new API

这篇关于C# 在 Process.Kill() 期间仅完成部分 ReadProcessMemory 或 WriteProcessMemory 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆