开发者

How to know if module is loaded on any process of system? C#

So... someone know how to make it?

I was thinking in something like:

    private bool IsModuleLoaded(String ModuleName)
    {
        bool loaded = false;
        Process[] processes = Process.GetProcesses();
        ProcessModule myProcessModule = null;
        ProcessModuleCollection myProcessModuleCollection;

        for (int i = 0; i < processes.Length; i++)
        {
            try
            {
                myProcessModuleCollection = processes[i].Modules;

                for (int j = 0; j < myProcessModuleCollection.Count; j++)
                {
                    myProcessModule = myProcessModuleCollection[j];

                    if (myProcessModule.ModuleName.Contains(ModuleName))
                    {
                        loaded = true;
                        break;
                    }
                }

            }
            catch { loaded = false; }
        }

        return loaded;
    }

But it doesn't works, because everytime returns true. Even if the module is not loaded in the memory of any process.

Thanks in advance!.

FIXED CODE:

    private bool IsModuleLoaded(String ModuleName)
    {
        bool loaded = false;
        Process[] processes = Process.GetProcesses();
        ProcessModule myProcessModule = null;
        ProcessModuleCollection myProcessModuleCollection;

        for (int i = 0; i < processes.Length; i++)
        {
            try
            {
                myProcessModuleCollection = processes[i].Modules;

                for (int j = 0; j < myProcessModuleCollection.Count; j++)
                {
                    myProcessModule = myProcessModuleCollection[j];

                    if (myProcessModule.ModuleName.Contains(ModuleName))
                    {
                        loaded = true;
                        break;
                    }
                }
            }
            catch { loaded = false; }
          开发者_StackOverflow社区  if (loaded)
                break;
        }

        return loaded;
    }


According to MSDN the Process.Modules property contains the modules that have been already loaded. So if a module is not present in the Modules collection, it is not loaded.


Well, you can always use the linq to help you with this, it will break out as soon as it hits a name

    private bool IsModuleLoaded2(String ModuleName)
    {
        var q = from p in Process.GetProcesses()
                from m in p.Modules.OfType<ProcessModule>()
                select m;
        return q.Any(pm => pm.ModuleName.Contains(ModuleName));
    }

Also, keep in mind that if you run on 64bit platform you MUST run this inside 64bit process. A 32 bit process cannot access 64 bit process's bits and pieces including a module list. I see that you are catching an exception which is probably the one I speak of, making your (and above) code invalid as it will not enumerate all processes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜