SetWindowsHookEx failing in .NET 4.0 on 32-bit machine with “module not found”?

前端 未结 3 1941
离开以前
离开以前 2020-11-30 07:39

I have found similar questions on this page, but I can\'t seem to figure out how to interpret the answers or figure out if they are truly duplicates.

Here are the pos

3条回答
  •  孤街浪徒
    2020-11-30 08:26

    Here is my solution that works both in .net 2 and 4. hInstance is ProcessModule.BaseAddress.

    public static class ModuleHelper
        {
            public static ProcessModule GetCurrentModule()
            {
                // need instance handle to module to create a system-wide hook
                Module[] list = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
                System.Diagnostics.Debug.Assert(list != null && list.Length > 0);
    
                var currentProcess = Process.GetCurrentProcess();
                var modules = currentProcess.Modules;
                ProcessModule mod = null;
                foreach (ProcessModule m in modules)
                                //for .net 2 we will find module here
                    if (m.ModuleName == list[0].Name)
                    {
                        mod = m;
                        break;
                    }
    
                        //for .net 4 take current module
                if (mod == null)
                    mod = Process.GetCurrentProcess().MainModule;
    
                return mod;
            }
        }
    

提交回复
热议问题