I try to loop over a directory and load functions from several dll files. Every dll file only exports a function with the same name as the dll. The problem is, it loads only the first file.
DIR *d;
struct dirent *dir;
d = opendir(".");
if(d) {
while((dir = readdir(d)) != NULL) {
// split filename into 'name', 'ext' and 'fullname'
// '.', '..' and all unliked files are sorted out correctly
HINSTANCE ext_dll = LoadLibrary(fullname);
if(NULL != ext_dll) {
MYPROC ext_func = (MYPROC) GetProcAddress(ext_dll, name);
}
ext_dll = NULL;
}
}
However, when i just write it twice it works.
HINSTANCE ext_dll = LoadLibrary("ext1.dll");
if(NULL != ext_dll) {
MYPROC ext_func = (MYPROC) GetProcAddress(ext_dll, "ext1");
}
ext_dll = LoadLibrary("ext2.dll");
if(NULL != ext_dll) {
MYPROC ext_func = (MYPROC) GetProcAddress(ext_dll, "ext2");
}
Is there something wrong with my code or is LoadLibrary not meant to be used that way?
Thanks
OP's linked code (though not the posted one) includes the following.
d = opendir(".");
setvoidfunc = (MYSETVOIDPROC) GetProcAddress(dll, "setVoidCallBack");
if (d) {
while ((dir = readdir(d)) != NULL) {
char *tok = strtok(dir->d_name, ".");
if(NULL == tok)
continue;
The strtok
call (potentially) modifies the dir->d_name
buffer, which violates the Open Group requirement for readdir
.
The application shall not modify the structure to which the return value of readdir() points, nor any storage areas pointed to by pointers within the structure.
Because of the violation, everything that happens after the first time dir->d_name
gets modified is undefined by the standard and implementation dependent.