There is a pointer to a function located in the DLL library. Is it possible to save this function in an object of type std::function<...> if the template parameters are correctly specified, then disable the library via FreeLibrary (I work in MSVC ) and work with the function via std::function ? Do I just have to push the dereferenced pointer to this function there?

  • You should rather have a LIB static library to include in the program body. Then you don’t need FreeLibrary et al. - Daniel Protopopov

3 answers 3

Ie, in simple terms, what you want to do (or rather, what you will do) - you save the pointer to the function, discard the function code from memory, leaving the pointer unclear where, and call it ...
What do you think you will do?

std::function does not preserve the std::function body in any way. Well, the function pointer will be invalidated by calling FreeLibrary .

Sketch the appropriate code is very simple, you can see for yourself ...

indll.cpp

 #include <iostream> #include <iomanip> using namespace std; extern "C" void __declspec(dllexport) inDll() { cout << "From DLL\n"; } 

Compile as cl /LD indll.cpp

test.cpp

 #include <iostream> #include <iomanip> #include <functional> #include <windows.h> using namespace std; int main(int argc, const char * argv[]) { HMODULE dll; if (0 != (dll = LoadLibrary("inDll.dll"))) { cout << "Load success\n"; FARPROC ptr = GetProcAddress(dll,"inDll"); function<void(void)> f = ptr; f(); FreeLibrary(dll); } } 

Compile as cl test.cpp

We start test.exe , we are convinced that everything works. Swap lines

  f(); FreeLibrary(dll); 

compile and make sure that you can’t do this :)

    Neither the C language nor the C ++ language have the notion of "save function". Any manipulations with std::function or something like that will only work with the pointer to the original function.

    With a "dereferenced function pointer" in C ++, you can only do a very limited number of things: for example, call a function or convert it back to a pointer. "Shove" the result of dereference can not be anywhere.

      std::function is just a wrapper over some entity that can be called . If the code of this entity becomes unavailable (for example, after executing FreeLibrary ), then attempts to invoke it will result in undefined behavior. Most likely you will simply get some runtime error from the operating system.

      You should not consider std::function as some kind of magic container that stores the code, and if you pass it somewhere, it can be executed. Otherwise, one could develop a thought and assume that, say, the packaging and transmission of an object std::function over the network to another device would allow the ability to execute the code “stored” in this object. This is impossible, if only for the reason that at the other end of the network there can be a device of a completely different architecture and the code (roughly speaking, a sequence of commands) should be completely different. To ensure this, other technologies are used, for example, CORBA .