What is the best way to load a 64-bit dynamic library from a 32-bit one ? The LoadLibrary () function in this case does not help, since the bit depth is different.

I saw many similar questions, where they write that it is necessary to create a separate process in which to load the dll- file and interact with it by means of IPC (Inter-Process Communication) , but nowhere are there examples. Is this the only way and is it really the best? In the answer, please give an example of the program code in C ++ .

PS:

There is such an article, but there the case is exactly the opposite: Accessing 32-bit DLLs from 64-bit code and without examples.

In this article, they write that you can do without COM and IPC using the LoadLibraryEx () function: Lesson 2. Support of 32-bit applications in 64-bit Windows environment .

It is impossible to load a 32-bit DLL from a 64-bit process and execute its code. It is not possible due to the design of 64-bit systems. It is impossible fundamentally. And no tricks and undocumented means will help you. It is not necessary to speak of the kernel structures. Actually, it means a 32-bit process must be made 32-bit "on the fly". "Why can't you thunk between 32-bit and 64-bit Windows?" It is a process that can be carried out through the COM technology. You may read "Accessing 32-bit DLLs from 64-bit code".

But it is a 32-bit DLL into a 64-bit process. Load_LIBRARY_AS_DATAFILE when calling LoadLibraryEx .

However, in the description of the function from here: The LoadLibraryEx function says that:

LOAD_LIBRARY_AS_DATAFILE. If this value is used, the system converts 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 to prepare for the execution of the displayed file. Therefore, you cannot call functions like GetModuleHandle or GetProcAddress for this DLL. Use this flag when you want to load a DLL only to extract messages or resources from it.

So, this option is not suitable.

  • I mean, recompile as 64-bit? - gil9red
  • @ gil9red, you do not need to recompile. You need to load something like LoadLibrary () from a 32-bit dll. And the library you want to download 64-bit. - neo
  • 3
    @neo this example allows you to use resources, but not as a function. Yes, and surrounded by 64 bits, not 32. Read there carefully. - Evgeny Borisov
  • @ Yevgeny Borisov, did you carefully read what I wrote? Re-read again. - neo
  • As far as I understand, this is the only way. How can a 64-bit DLL be able to work in a 32-bit process? How can you pass, for example, a pointer? - VladD

1 answer 1

  • In the question it is absolutely rightly noted that there is no way to execute x64 library code from a 32 bit process.
  • It is also absolutely true that the easiest way is to start or use another 64 bit process by loading the library into it. This will require interprocess communication , i.e. data transfer from one process to another.
  • The question of data transfer is a separate issue and strongly depends on the context of the task, the description of which is not here. In the simplest case, the data in the destination stream can be transferred from the command line, and you can return the int as the return value of DllMain , or write the result to the file. In more complex cases, you can use named pipes , message queues , you can even run the process on another computer and share it over the network.

If you need advice on interprocess communication, then I recommend asking a question with specific requirements.