Load/edit/update functions from dll.

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?


Best regards
Volang

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?
I really don't understand what you're thinking. If you have a function like
1
2
3
int foo(int x){
    return x * 2;
}
you can already run it. You don't need to do anything crazy like add it to an existing library to run it.

Are you generating new code dynamically?
Last edited on
Are you generating new code dynamically?


Yes, after I run my exe, and the program is "live". I want to be able to insert new functions to it without restart/recompile it.

If I had a function that loads the dll file on command it could work right?
I see, so you want to run, for example, GCC to build a DLL and load that DLL into the process.

How about this?
1. If the DLL is loaded, unload it.
2. Run GCC. GCC will overwrite the DLL if it already exists.
3. Load the DLL.
1. If the DLL is loaded, unload it.


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.
Topic archived. No new replies allowed.