由特定进程按其名称加载的 DLL 列表

List of DLLs loaded by specific process by its name

我正在尝试使用以下代码列出要处理的加载 dll:

Process[] ObjModulesList = Process.GetProcessesByName("iexplore");

foreach (Process prc in ObjModulesList)
{
    ProcessModuleCollection ObjModules = prc.Modules;
    foreach (ProcessModule objModule in ObjModules)
    {
        string strModulePath = objModule.FileName.ToString();
        Console.WriteLine(strModulePath);
    }
}

我遇到错误

A 32 bit processes cannot access modules of a 64 bit process.

我尝试 运行 我的进程作为管理员,运行宁 iexplore 作为 64 位和 32 位。 None 的工作人员。

顺便说一句,我必须将我的程序编译为 32 位。

有什么想法吗?

您必须使用 x64 目标编译您的应用程序。

另外可以咨询: http://www.codeproject.com/Articles/301/Display-Loaded-Modules-v

可以使用WMI,这里有一段获取所有进程和相关模块的代码:

var wmiQueryString = string.Format("select * from CIM_ProcessExecutable");
Dictionary<int, ProcInfo> procsMods = new Dictionary<int, ProcInfo>();
using (var searcher = new ManagementObjectSearcher(string.Format(wmiQueryString)))
using (var results = searcher.Get())
{
    foreach (var item in resMg.Cast<ManagementObject>())
    {
        try
        {
            var antecedent = new ManagementObject((string)item["Antecedent"]);
            var dependent = new ManagementObject((string)item["Dependent"]);
            int procHandleInt = Convert.ToInt32(dependent["Handle"]);
            ProcInfo pI = new ProcInfo { Handle = procHandleInt, FileProc = new FileInfo((string)dependent["Name"]) };
            if (!procsMods.ContainsKey(procHandleInt))
            {
                procsMods.Add(procHandleInt, pI);
            }
            procsMods[procHandleInt].Modules.Add(new ModInfo { FileMod = new FileInfo((string)antecedent["Name"]) });
        }
        catch (System.Management.ManagementException ex)
        {
            // Process does not exist anymore
        }
    }
}

procsMods中我们已经存储了流程和模块,现在我们打印它们:

foreach (var item in procsMods)
{
    Console.WriteLine(string.Format("{0} ({1}):", item.Value.FileProc.Name, item.Key));
    foreach (var mod in item.Value.Modules)
    {
        Console.WriteLine("\t{0}", mod.FileMod.Name);
    }
}

这些是 ProcInfoModInfo 类:

class ProcInfo
{
    public FileInfo FileProc { get; set; }
    public int Handle { get; set; }
    public List<ModInfo> Modules { get; set; }

    public ProcInfo()
    {
        Modules = new List<ModInfo>();
    }
}

class ModInfo
{
    public FileInfo FileMod { get; set; }
}