Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中检测特定进程的CPU结构#_C#_.net_Process_Detect_Cpu Architecture - Fatal编程技术网

C# 在C语言中检测特定进程的CPU结构#

C# 在C语言中检测特定进程的CPU结构#,c#,.net,process,detect,cpu-architecture,C#,.net,Process,Detect,Cpu Architecture,我用C#写代码。 我的代码将在任何CPU模式下运行并提升 我的目标是使用Process.GetProcesses()枚举机器中的所有进程,并为每个进程检测其CPU体系结构:x86、x64或IA64 我正在用C#实现代码注入,需要检测目标进程的体系结构,以决定注入哪些操作码 怎么做 谢谢。您可以尝试调用: BOOL WINAPI IsWow64Process( __in HANDLE hProcess, __out PBOOL Wow64Process); 您必须调用Win32才能获取此

我用C#写代码。 我的代码将在
任何CPU
模式下运行并提升

我的目标是使用
Process.GetProcesses()
枚举机器中的所有进程,并为每个进程检测其CPU体系结构:x86x64IA64

我正在用C#实现代码注入,需要检测目标进程的体系结构,以决定注入哪些操作码

怎么做


谢谢。

您可以尝试调用:

BOOL WINAPI IsWow64Process(  __in   HANDLE hProcess, __out  PBOOL Wow64Process);

您必须调用Win32才能获取此信息:

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern bool IsWow64Process(System.IntPtr hProcess, out bool lpSystemInfo);

public bool IsWow64Process(System.Diagnostics.Process process)
{
    bool retVal = false;
    IsWow64Process(process.Handle, out retVal);
    return retVal;
}

为每个进程调用
IsWow64Process(process)
将告诉您它是否为64位。我没有遇到一种方法来确定进程是x64还是IA64,只是确定它的“位”。

您可以p/调用
QueryFullProcessImageName
GetProcessImageFileName
,然后读取.exe文件的PE头。

定义:

    [DllImport("kernel32.dll")]
    internal static extern void GetNativeSystemInfo(ref SystemInfo lpSystemInfo);

    [DllImport("kernel32.dll")]
    internal static extern void GetSystemInfo(ref SystemInfo lpSystemInfo);

    [StructLayout(LayoutKind.Sequential)]
    internal struct SystemInfo
    {
        public ushort wProcessorArchitecture;
        public ushort wReserved;
        public uint dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public UIntPtr dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public ushort wProcessorLevel;
        public ushort wProcessorRevision;
    }

    internal const ushort ProcessorArchitectureIntel = 0;
    internal const ushort ProcessorArchitectureIa64 = 6;
    internal const ushort ProcessorArchitectureAmd64 = 9;
    internal const ushort ProcessorArchitectureUnknown = 0xFFFF;
GetNativeSystemInfo将返回有关正在运行的计算机的信息。 GetSystemInfo将返回有关正在其中运行的虚拟化环境的信息(如果没有,则与GetNativeSystemInfo相同)

即: 在32位Windows上,您将始终使用wProcessorArchitecture==ProcessorArchitectureIntel

在64位Windows上,如果以32位进程运行,则GetSystemInfo的WPProcessorArchitecture==ProcessorArchitectureIntel,GetNativeSystemInfo的WPProcessorArchitecture==ProcessorArchitectureAmd64


如果您是64位Windows上的64位进程,它们显然都是Processor ArchitectureAMD64。

Alastair的正确之处在于,您不能只调用IsWow64Process,也不需要直接调用另一个WinApi函数。这里有一个简短的解决方案

    /// <summary>
    /// TRUE if the process is running under WOW64. That is if it is a 32 bit process running on 64 bit Windows.
    /// If the process is running under 32-bit Windows, the value is set to FALSE. 
    /// If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE.
    /// </summary>
    [DllImport( "kernel32.dll" )]
    static extern bool IsWow64Process( System.IntPtr aProcessHandle, out bool lpSystemInfo );

    /// <summary>
    /// Indicates if the process is 32 or 64 bit.
    /// </summary>
    /// <param name="aProcessHandle">process to query</param>
    /// <returns>true: process is 64 bit; false: process is 32 bit</returns>
    public static bool Is64BitProcess( System.IntPtr aProcessHandle )
    {
        bool lIs64BitProcess = false;
        if ( System.Environment.Is64BitOperatingSystem ) {
            IsWow64Process( aProcessHandle, out lIs64BitProcess );
        }
        return lIs64BitProcess;
    }
//
///如果进程在WOW64下运行,则为TRUE。也就是说,如果它是一个运行在64位Windows上的32位进程。
///如果进程在32位窗口下运行,则该值设置为FALSE。
///如果进程是在64位Windows下运行的64位应用程序,则该值也设置为FALSE。
/// 
[DllImport(“kernel32.dll”)]
静态外部bool iswow64进程(System.IntPtr a进程句柄,out bool lpSystemInfo);
/// 
///指示进程是32位还是64位。
/// 
///要查询的进程
///正确:进程为64位;错误:进程是32位的
公共静态bool为64位进程(System.IntPtr a进程句柄)
{
bool lIs64BitProcess=false;
if(System.Environment.is64位操作系统){
ISWOW64进程(进程句柄,输出LIS64位进程);
}
返回lIs64BitProcess;
}

我认为你们都在正确的轨道上,但返回值缺少not运算符。如果您在一台64位计算机上,并且进程是WOW64,那么它是一个32位进程(请参阅上面的注释IsWow64Process)。此外,ISWOW64进程返回错误的可能性也没有得到处理。这里有一个固定版本:

/// <summary>
/// TRUE if the process is running under WOW64. That is if it is a 32 bit process running on 64 bit Windows.
/// If the process is running under 32-bit Windows, the value is set to FALSE. 
/// If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE.
/// </summary>
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool IsWow64Process(System.IntPtr aProcessHandle, out bool isWow64Process);

/// <summary>
/// Indicates if the process is 32 or 64 bit.
/// </summary>
/// <param name="aProcessHandle">process to query</param>
/// <returns>true: process is 64 bit; false: process is 32 bit</returns>
public static bool Is64BitProcess(System.IntPtr aProcessHandle)
{
    if (!System.Environment.Is64BitOperatingSystem)
        return false;

    bool isWow64Process;
    if (!IsWow64Process(aProcessHandle, out isWow64Process))
        throw new Win32Exception(Marshal.GetLastWin32Error());

    return !isWow64Process;
}
//
///如果进程在WOW64下运行,则为TRUE。也就是说,如果它是一个运行在64位Windows上的32位进程。
///如果进程在32位窗口下运行,则该值设置为FALSE。
///如果进程是在64位Windows下运行的64位应用程序,则该值也设置为FALSE。
/// 
[DllImport(“kernel32.dll”,SetLastError=true)]
静态外部bool iswow64进程(System.IntPtr a进程句柄,out bool iswow64进程);
/// 
///指示进程是32位还是64位。
/// 
///要查询的进程
///正确:进程为64位;错误:进程是32位的
公共静态bool为64位进程(System.IntPtr a进程句柄)
{
如果(!System.Environment.is64位操作系统)
返回false;
bool-iswow64过程;
if(!IsWow64Process(一个进程句柄,输出IsWow64Process))
抛出新的Win32Exception(Marshal.GetLastWin32Error());
return!isWow64Process;
}

IA64处理器不能运行x86\u 64代码,是吗?因此,本地机器架构将区分这些。或者找到进程对应的可执行文件并检查PE头。这是不对的。IsWow64Process告诉您是否在64位操作系统下运行的32位进程中。对于32位应用程序,这将在32位Windows上返回false;对于64位应用程序,这将在64位Windows上返回false。请参阅下面我的答案,了解如何正确执行。如果您正在实现代码注入,您可能正在调用大量Win32 API?我是否可以建议使用C++/CLI来实现这一点要容易得多。。。最后,你可以使用.NET类,就像你在C语言中写的一样,但是你不必把所有的结构都翻译出来,因为C++编译器直接从Win32头文件中为你做这些操作。+ 1到本。另外,不要忘记1.0/1.1/2.0 CLR不能在进程中承载2个运行时,因此您需要确定进程是否运行托管代码(或者稍后是否运行不同版本的托管代码),并注入匹配的代码(除了4.0之外,最好至少有2.0版本的代码)@阿列克谢:同一个进程不能同时拥有.NET4和1.0/1.1/2.0吗?所以,如果您总是注入.NET 4代码,那么是否已经加载了CLR版本就无关紧要了。我正在注入本机操作码,所以我不关心目标进程中加载的.NET framework(如果有)。我更喜欢这种方法。请看这里关于如何从PE头中获取架构的内容,虽然这是找到头的经得起未来考验的方法。我真的不喜欢这样。您需要支持PE格式的任何未来扩展。例如,您还需要了解.net标题。如果你击中了像“任何CPU”这样的目标,它会变得更加复杂。@CodeInChaos:AnyCPU代码仍然有标准的头。您可能希望使用process helper函数查找加载了哪个
kernel32.dll
,并检查其位。如果标题在将来被更改,你不认为微软会更新ImageNtHeader吗