What's the best way to load a 64-bit dynamic link library from a 32-bit one ? The LoadLibrary () function will not help in this case, since the bitness is different.
I saw many similar questions, where they write that you need to create a separate process in which to load a dll and interact with it using IPC (Inter-Process Communication) tools, but there are no examples anywhere. Is this the only way and is it really the best one? In response, I ask you to provide an example of program code in C ++ .
There is such an article, but there the case is exactly the opposite: Accessing 32-bit DLLs from 64-bit code and without examples.
This article writes that you can do without COM and IPC by using the LoadLibraryEx () function: Lesson 2. Support of 32-bit applications in the 64-bit Windows environment .
It is impossible to load a 32-bit DLL from a 64-bit process and execute its code. It is impossible due to the design of 64-bit systems. It is impossible fundamentally. And no tricks and undocumented means will help you. To do this you will have to load and initialize WoW64, not to speak of the kernel structures. Actually, it means that a 64-bit process must be made 32-bit "on the fly". This topic is described more thoroughly in the post "Why can't you thunk between 32-bit and 64-bit Windows?". The only thing we can recommend is to create a surrogate process and work with it through the COM technology. You may read about it in the article "Accessing 32-bit DLLs from 64-bit code".
But it is quite easy to load resources from a 32-bit DLL into a 64-bit process. You may do it specifying the flag LOAD_LIBRARY_AS_DATAFILE when calling LoadLibraryEx .
However, in the function description from here: The LoadLibraryEx function says that:
LOAD_LIBRARY_AS_DATAFILE. If this value is used, the system transforms and projects the file data into the virtual address space of the calling process, as if it were a data file. Nothing is done to execute the code or prepare for the execution of the mapped file. Therefore, you cannot call functions like GetModuleHandle or GetProcAddress on this DLL. Use this flag when you want to load a DLL just to retrieve messages or resources from it.
This means that this option will not work.
- The question quite rightly notes that there is no way to execute the
x64library code from a
- It is also quite true that the easiest way is to start or use another
64bit process by loading the library into it. This will require interprocess communication , i.e. transfer of data from one process to another.
- The issue of data transfer is a separate issue and strongly depends on the context of the task, which is not described here. In the simplest case, data can be passed to the destination stream from the command line, and an
intbe returned as the return value of
DllMain, or the result can be written to a file. In more complex cases, you can use named pipes , message queues , you can even start a process on another computer and exchange over the network.
If you need advice on interprocess communication, then I recommend asking a question with specific requirements.