Drop-in replacement of GetModuleHandle and GetProcAddress Win32 API.
Use ExportResolver.GetModuleHandle function.
varaddress=ExportResolver.GetModuleHandle("kernel32.dll");Console.WriteLine(address.ToString());There are multiple solutions.
If you want to just resolve that one function, you can use ExportResolver.ResolveExport utility function.
privatedelegateIntPtrVirtualAllocDelegate(IntPtrlpAddress,UIntPtrdwSize,AllocationTypeflAllocationType,MemoryProtectionflProtect);privatevoidResolve_Address_Then_Call_Delegate(){varpfnVirtualAlloc=ExportResolver.ResolveExports("kernel32.dll","VirtualAlloc");varvirtualAlloc=Marshal.GetDelegateForFunctionPointer<VirtualAllocDelegate>(pfnVirtualAlloc);varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}privatevoidDirect_Get_Delegate(){varvirtualAlloc=(VirtualAllocDelegate)ExportResolver.ResolveExports("kernel32.dll","VirtualAlloc",typeof(VirtualAllocDelegate));varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}privatevoidDirect_Get_Delegate_Generic(){varvirtualAlloc=ExportResolver.ResolveExports<VirtualAllocDelegate>("kernel32.dll","VirtualAlloc");varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}If you want to resolve multiple or batch exports, you can use ExportResolver type itself.
privatedelegateIntPtrVirtualAllocDelegate(IntPtrlpAddress,UIntPtrdwSize,AllocationTypeflAllocationType,MemoryProtectionflProtect);privatevoidResolve_Address_Then_Call_Delegate(){varvirtualAlloc=newExportResolver("kernel32.dll").GetExport<VirtualAllocDelegate>("VirtualAlloc");varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}You can even specify the Module Handle (also known as Module Base Address) instead of the module name.
The module handle can be resolved using Win32 API GetModuleHandle or ExportResolver.GetModuleHandle.
privatevoidDirect_Get_Delegate_Generic(){varmoduleHandle=ExportResolver.GetModuleHandle("kernel32.dll");varvirtualAlloc=newExportResolver(moduleHandle).GetExport<VirtualAllocDelegate>("VirtualAlloc");varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}You can specify the function ordinal instead of the function name to choose export.
privatedelegateIntPtrVirtualAllocDelegate(IntPtrlpAddress,UIntPtrdwSize,AllocationTypeflAllocationType,MemoryProtectionflProtect);privatevoidDirect_Get_Delegate_Generic(){varvirtualAlloc=ExportResolver.ResolveExports<VirtualAllocDelegate>("kernel32.dll",0x05DA);// Windows 10 22H2; kernel32 version 6.2.19041.3636varmemory=virtualAlloc(IntPtr.Zero,(UIntPtr)1024,AllocationType.COMMIT|AllocationType.Reserved,MemoryProtection.READWRITE);
...}