I want to load special dll without execute dllmain function.
I think, set a breakpoint at dllmain can solve this problem.
But I don't know How can I do?
Also I want call dll's export function.
I have tried to use LoadLibraryEx with dont_resolve_dll_references, but it occurs error with dll's function call.
How can I solve this? Please give me your idea.
Thanks.
As explained in this question: Win32 API to enumerate dll export functions?
You can use LoadLibraryEx with the DONT_RESOLVE_DLL_REFERENCES flag, even though use of that flag is strongly discouraged.
If so you will likely have to free and reload the dll if you actually want to use it.
Well as explained here:
An optional entry point into a dynamic-link library (DLL). When the system starts or terminates a process or thread, it calls the entry-point function for each loaded DLL using the first thread of the process. The system also calls the entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibrary functions.
calling the DllMain is an OS feature mandatory (although implementing that function is optional) if you use the standard way in loading and executing a dynamic library. So there is no official way in doing this.
Related
I'm trying to run some NSWindow functions from another thread on OSX. I am doing this via ctypes so need to find the library files.
dispatch_sync I found in libc.dylib but I can't find dispatch_get_main_queue, does anyone know the library that is in? Is it not in libc? I thought to use this based on here: Objective C Multi thread NSWindow alternative?
I also couldn't find the docs of the types used on opensource.apple can someone also help me find that for this Dispatch module.
dispatch_get_main_queue() is an inline function, so it doesn't end up in any library. It's compiled into every [Objective-]C/C++ file that uses it.
It compiles down to just (dispatch_queue_t)&_dispatch_main_q, more or less. That is, there's a global variable _dispatch_main_q and dispatch_get_main_queue() just returns its address, type cast to dispatch_queue_t.
On my 10.9.5 system, _dispatch_main_q is exported by /usr/lib/system/libdispatch.dylib.
I'm compiling a COBOL program as a DLL in zOS using the compiler options
PGMN(LM),DLL,EXPORTALL
When I do this, it also forces the compile to be NODYNAM. In this context, is there some other parm I can use to force the CALLS to to other subprograms from this to be dynamic (i.e. resolved at run time).
I know I can use the CALL variable-name approach to accomplish this, but I can't do this with system routines like DSNELI, the DB2 call interface.
Does the IMPORT option have something to do with this?
Thanks!
All DLL's must be complied with NODYNAM. This cannot be avoided. As you pointed out using NODYNAM does
not preclude dynamic program calls using the CALL var-name approach. As long as you are using dynamic calls
to locally developed routines you will maintain all of the advantages of not having static linked modules in
your programs.
Be less concerned about static linked system modules such as CALL 'DSNELI'. These are
stub programs that will dynamically load the appropriate language interface module at
run time. See Universal Language Interface.
Generally speaking, you want the calls to those system routines to be static. The routines tend to be stubs that locate the "real" routine at runtime.
I have already added delay loading to my project, using the instructions in
http://msdn.microsoft.com/en-us/library/151kt790.aspx
In the "delayhlp.cpp" (a sample implementation of the DLL load helper) __HrLoadAllImportsForDll, I saw that the writer avoids using any Standard C Library (MSVCRT) functions. Do I need to do the same in my handler function, which will be called by the sample DLL load helper?
I think the writer's reason is that someone might try to delay-load MSVCRT itself. I'm not going to do this. Will it then be safe for me to use MSVCRT functions?
Background Info. The reason for delay-loading the 3rd party DLL is because there is a function signature change between two versions, and I need to run my program using either version. I then provide a simple wrapper function to adapt the DLL's function signature to the one needed. This function is registered by the Delay-Load Handler (__pfnDliFailureHook2), when GetProcAddress fails.
Some testing. I added a breakpoint at the beginning of my handler function. I found that when the breakpoint is hit, the msvcrt.dll and msvcr90d.dll etc are already loaded (from Visual Studio's Modules pane). Does it mean that I can call CRT functions safely?
// Check to see if it is the DLL we want to load.
// Intentionally case sensitive to avoid complication of using the CRT
// for those that don't use the CRT...the user can replace this with
// a variant of a case insenstive comparison routine.
//
That's the more relevant comment, for those that don't use the CRT. You won't have a problem, the CRT is always loaded by the startup code.
It can be used to run arbitary Dynamic Link Library in windows,
how can it possibly know the entry point of an arbitary dll?
The answer depends on how much details you need. Basically, it comes down to this:
A DLL can optionally specify an entry-point function. If present, the system calls the entry-point function whenever a process or thread loads or unloads the DLL.
[...] If you are providing your own entry-point, see the DllMain function. The name DllMain is a placeholder for a user-defined function. You must specify the actual name you use when you build your DLL.
(Taken from the MSDN article Dynamic-Link Library Entry-Point Function.)
So basically, the entry point can be specified inside the DLL, and the operating system's DLL loader knows how to look this up.
The IMAGE_OPTIONAL_HEADER (part of the portable executable's header on Windows machines) contains an RVA of the AddressOfEntryPoint that is called by programs looking for an entry point to call (e.g., the loader).
More information on the IMAGE_OPTIONAL_HEADER can be found here. And this paper is good for just general PE knowledge.
What do you mean by "run a DLL"? DLLs aren't normal programs, they are just a collection of functions. The entry point itself usually doesn't do much apart from initializing stuff required by other functions in the DLL. The entry point is automatically called when the DLL is loaded (you can use LoadLibrary to do this).
If you want to call a specific function after loading the DLL, you can use GetProcAddress to get a pointer to the function you want.
Is it possible for a function that is inside a DLL to unload the DLL? I need to do this so I can make sure the DLL is not in use, then write to the DLL's file.
As I understand it, it CAN be done and is MEANT to be done sometimes (for example in case of dll injection by CreateRemoteThread and other methods). So,
FreeLibraryAndExitThread(hModule, 0)
will do precisely that.
On the other hand, calling
FreeLibrary(hModule)
will not do here - from MSDN: "If they were to call FreeLibrary and ExitThread separately, a race condition would exist. The library could be unloaded before ExitThread is called." As a remark, ExitThread does some bookkeeping besides just returning from the thread function.
All this assumes that Your Dll obtained the hModule itself by calling LoadLibrary from inside the loaded Dll, or rather, by calling from inside the loaded Dll the following function:
GetModuleHandleEx
(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)DllMain,
&hModule
)
This increments the reference count of the Dll so You know that if You free the library later using that handle and if the library is really unloaded then You had the last reference to it.
If You instead skip incrementing the Dll's reference count and obtain the hModule just from the argument to DllMain during DLL_PROCESS_ATTACH then You should not call FreeLibraryAndExitThread since the code that loaded the Dll is still using it and this module handle really isn't Yours to manage.
Use this when the dll has done it job:
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FreeLibrary, &__ImageBase, 0, NULL);
// terminate if dll run in a separate thread ExitThread(0);
// or just return out the dll
And the __ImageBase is your dll's PE header structure:
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
If your asking if you can safely unload/unmap a DLL loaded in a process from code in the DLL itself, the answer is no - there isn't really a safe way to do this.
Think about it this way: Unloading a DLL is done by decrementing it's reference count using FreeLibrary(). The problem of course is that once the reference count of the DLL hits zero, the module is unmapped. Which means that the code in the DLL that called FreeLibrary() is gone.
Even if you could do this, you'd still need to ensure that there are no other threads executing any exported functions from the DLL.
I don't think it will work. Calling FreeLibrary with a handle from the outside (LoadLibrary would have been called from an area outside the DLL) as the code runs in a memory location that will not be valid anymore.
Even if this is possible, it smells like a bad design. Maybe you want to make some updater or alike. Explain a bit more what is the result you expect. Unloading a DLL from within itself is not the way to go.