How could one pass arguments to the initialization function of a DLL loaded via LoadLibrary? Is it possible at all? Without resorting to some kind of exported function or shared memory, that is.
相关问题
- Multiple sockets for clients to connect to
- How to know full paths to DLL's from .csproj f
- the application was unable to start correctly 0xc0
- What is the best way to do a search in a large fil
- Inheritance impossible in Windows Runtime Componen
Another possible solution: create a second DLL that only exposes a "setparam" and a "getparam" method, than use it in both the application (setparam) and the dll's DllMain (getparam). In their basic form, the methods are implemented with static variables, but you can use more sophisticated technologies. This solution, although slightly more difficult, has some advantages:
it doesn't use any "global" convention (except the name of the common DLL!)
it doesn't consume possibly limited resources (e.g. environment variables)
it's general: you can use the same DLL wherever you want.
it may be made as powerful and complicated as you need: e.g. you can make it thread-safe if required. It's only a matter of implementation.
Here's a minimal example:
There is no direct way.
The easiest may be through environment variables. They can be set easily before calling
LoadLibray
withsetenv
, and then the DLL (in the same process) can retrieve them withgetenv
.It is "bad" and "ugly" but you can push arguments on to the stack with in-line ASM before you make your call and pop them back off in much the same way. A very hack-ish solution, but it can work. I only note it because it's POSSIBLE, not because it's a good way to do things.
An alternative means
While I'm not quite sure if this falls under "shared memory" (since you can also use this method to send data to DLLs loaded in separate processes, as well)... You could allocate some memory at a specific address using
VirtualAllocEx
, pass in a struct containing all the data the DLL will need usingWriteProcessMemory
, then lock it withVirtualLock
before loading the DLL.Then in the DLL's entry point function, I would use
VirtualUnlock
, grab that data usingReadProcessMemory
, thenVirtualFree
to clean up resources.Although a bit choppy, this is especially useful if you have more than just a simple string to pass on.
Note that you must have read/write access in the target process for this to work.
Example (pseudo-code)