Process.GetProcessesByName在exe上不起作用

时间:2020-06-05 23:32:17

标签: c#

if (!Main.isAttached)
                {
                    num++;
                    if (num <= 15)
                    {  
                        try
                        {
                            Main.gameProcess = Process.GetProcessesByName("csgo")[0];
                            if ((Main.gameProcess == null || !Main.IsModuleLoaded(Main.gameProcess, "client_panorama.dll") ? true : !Main.IsModuleLoaded(Main.gameProcess, "engine.dll")))
                            {
                                continue;
                            }
                        }
                        catch (Exception exception)
                        {
                            Debug.WriteLine(exception);
                            continue;
                        }
                        while (true)
                        {
                            if ((Main.clientPanoramaDll == null ? false : Main.engineDll != null))
                            {
                                break;
                            }
                            Thread.Sleep(100);
                            Main.clientPanoramaDll = Main.GetModuleHandle(Main.gameProcess, "client_panorama.dll");
                            Main.engineDll = Main.GetModuleHandle(Main.gameProcess, "engine.dll");
                        }
                        Main.isAttached = true;
                    }
                    else
                    {
                        MessageBox.Show("CSGO Not Open!", "Open the game first.", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        base.Close();
                        break;
                    }
                }

我正在尝试为CSGO编程一个mod,但是它说它已经打开时无法检测到游戏已打开!我尝试将其更改为csgo.exe,csgo.exe32,但没有任何效果。有什么提示吗?

1 个答案:

答案 0 :(得分:0)

您没有与我们共享您的IsModuleLoaded()函数,因此我们无法回答此问题,但这是我使用的代码可以解决您的问题:

public static IntPtr GetModuleBaseAddress(Process proc, string modName)
{
    IntPtr addr = IntPtr.Zero;

    foreach (ProcessModule m in proc.Modules)
    {
        if (m.ModuleName == modName)
        {
            addr = m.BaseAddress;
            break;
        }
    }
    return addr;
}

Process proc = Process.GetProcessesByName("csgo")[0];

var modBase = ghapi.GetModuleBaseAddress(proc, "client.dll");

您可以检查GetModuleBaseAddress的返回值,如果它等于IntPtr.Zero,则说明模块未加载。它也将用作模块句柄,因此您不需要其他的冗余代码。