加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 特定进程按名称加载的DLL列表

发布时间:2020-12-15 20:56:18 所属栏目:百科 来源:网络整理
导读:我正在尝试使用以下代码列出加载到processe的dll: Process[] ObjModulesList = Process.GetProcessesByName("iexplore");foreach (Process prc in ObjModulesList){ ProcessModuleCollection ObjModules = prc.Modules; foreach (ProcessModule objModule i
我正在尝试使用以下代码列出加载到processe的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.

我尝试以管理员身份运行我的进程,并以64位和32位运行iexplore.没有人工作.

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

有任何想法吗?

解决方法

您可以使用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);
    }
}

这些是ProcInfo和ModInfo类:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读