使用 WMI,我如何确定远程进程是32位还是64位?

5

我有一个从远程机器使用WMI查询到的win32_process对象集合。如何确定每个进程是32位还是64位?


2个回答

1

WMI没有这个功能。解决方案是使用P/Invoke通过IsWow64Process测试每个进程的Handle这段代码应该能帮助你理解。


谢谢。我会试一下的。很奇怪的是,它们在进程类中没有识别这个方法,甚至在.NET API中也没有。 - musaul

0

试试这个:

/// <summary>
/// Retrieves the platform information from the process architecture.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetPlatform(string path)
{
    string result = "";
    try
    {
        const int pePointerOffset = 60;
        const int machineOffset = 4;
        var data = new byte[4096];
        using (Stream s = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            s.Read(data, 0, 4096);
        }
        // Dos header is 64 bytes, last element, long (4 bytes) is the address of 
        // the PE header
        int peHeaderAddr = BitConverter.ToInt32(data, pePointerOffset);
        int machineUint = BitConverter.ToUInt16(data, peHeaderAddr +
                                                      machineOffset);
        result = ((MachineType) machineUint).ToString();
    }
    catch { }

    return result;
}



public enum MachineType
{
    Native = 0,
    X86 = 0x014c,
    Amd64 = 0x0200,
    X64 = 0x8664
}

请记住,这个过程是准确的,但在足够多的进程排队时会有点沉重。我在另一个线程中调用它以缓解用户界面。 - Xcalibur37
你正在使用C#编写此代码。这是一种可以在32位或64位模式下运行的语言。你无法从EXE头文件中判断。 - Hans Passant
你可以编译到特定的平台上。否则,为什么要有两个相同可执行文件的不同版本呢? - Xcalibur37
为什么要分成两个,当你可以拥有一个能在两个平台上运行的呢?AnyCPU很酷。 - Hans Passant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接