C#只有ReadProcessMemory或WriteProcessMemory请求的一部分在Process.Kill()中完成 [英] C# Only part of a ReadProcessMemory or WriteProcessMemory request was completed during Process.Kill()

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

问题描述

我已经很广泛地研究了这个问题,似乎找不到答案。

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

我知道只有ReadProcessMemory或WriteProcessMemory请求已完成当32位进程尝试访问64位进程时,异常被抛出,64位进程修改32位进程。

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.

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

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);
    }
}

这些进程可能而且很有可能是32位和64位,混合在一起。

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?

推荐答案

Process.Modules的MSDN页面这个线程在枚举32中的 Process.Modules 中有一个已知的问题64位进程的位进程,反之亦然:

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:


内部.NET的Process.Modules正在使用来自PSAPI的函数EnumProcessModules
.dll文件。这个功能有一个已知的问题,它不能在32/64位进程边界上工作
。因此,从32位进程枚举另一个
64位进程,反之亦然,
正确无效。

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.

解决方案似乎是使用 EnumProcessModulesEx 函数(必须通过P / Invoke调用),但此功能仅适用于更高版本的Windows。

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.


我们通过将
a新功能称为EnumProcessModulesEx修复到PSAPI.dll
(http://msdn2.microsoft.com/en-us/)库/ ms682633.aspx),但是我们
目前在这种情况下不能使用:

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:


  • 它只适用于Windows Vista或Windows Server 2008

  • 目前,.NET 2.0 Framework没有Service Pack或修补程序来使Process.Modules使用这个新的API

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

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