I want to export a function, from a .dll file in to my C++ program. I read that I have to load the entire file/library in to memory, then I can retreive the address of a specific function from it.
I want to keep the function that i've selected, and later maybe add new functions to the dll file and load them in as well. What approaches can be preferred here?
A library is only meant to add specific functionality to a program. You can't modify the functions in a library, nor add functions to it.
Or rather, it's not that it's completely impossible, it's just that in almost all circumstances it wouldn't make sense. If you want to put functions in a library just create your own library.
I need a way to insert new functions that can be used while the program is running.
Maybe, after exporting the dll and retreived the address of the function I want, I can use that address to make another copy of the function and save it to a different location. Then I can recompile a new dll with the new functions added, and load that version, after I removed the current one from memory?
Once I have loaded the library into memory, I need to use a winapi function to get the address of the function that I want to get from the library.
So if I unload the library, the address to the function that i've got will not be valid anymore, right? I have to wait for the new load, then get a new address and meanwhile that function wont be valid.
So do I make a copy of the function (when I get the address from the first load) and save it to a separate place, different from rest? Or is there a better way to handle this?
So if I unload the library, the address to the function that i've got will not be valid anymore, right? I have to wait for the new load, then get a new address and meanwhile that function wont be valid.
Correct.
So do I make a copy of the function (when I get the address from the first load) and save it to a separate place, different from rest? Or is there a better way to handle this?
If you have multiple threads, just use a mutex and never allow the function to be called while the library is being recreated. Do you really need to ensure a minimum response time at all times?
If you have just one thread then you don't need to do anything, since you won't be able to do anything while reloading.