c++ - DLL Loading in Windows Programming -


I wanted to use a set of API native GetProcAddressto. The method that was previously used by GetModuleHandle was considered on DLLHandle then the API address which is my wish, is taken by GetProcAddress. This works for ntdll functions, but the user32 and advapi32 give zero value for the GetModuleHandle function. To solve this problem, what is your suggestion?

  Enter code here Handle Process = GateCurrent Process (); HMODULE hNtdll = GetModuleHandle (TEXT ("ntdll.dll")); FARPROC function_address = GetProcAddress (hNtdll, function_name);  

requires DLL to be preloaded:

Recovers a module handle for the specified module The module should be loaded by the calling process.

ntdll.dll will always load but user32.dll and advapi32.dll Is not loaded originally. Based on your details, it looks like code calls GetModuleHandle () first, which will fail if DLL is not already loaded (check the value in case of failure).

Use LoadLibrary () for the DLL not loaded by default. However, using LoadLibrary () in that event that the GetModuleHandle () failure is a dangerous strategy because some other thread in the process FreeLibrary () DLL, which you have obtained through a handle via GetModuleHandle () (which means that the addresses of a function you receive through that handle are invalid because the DLL is no longer in memory) Using LoadLibrary () , later FreeLibrary () , is a safe way , Because it ensures that the DLL will not be loaded as long as you FreeLibrary () .


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -