Код: Выделить всё
void * do_function_with_so ( char * so_name, char * fun_name, void ** args, int n) {
void * rusult = NULL;
void * dl_handle;
dl_handle = dlopen(so_name, RTLD_LAZY);
if (!dl_handle) {
// ... error
return rusult;
}
void * _f_ ();
char * error;
_f_ = dlsym(dl_handle, fun_name);
error = dlerror();
if (error != NULL) {
// ... mh
return rusult;
}
switch (n) {
case 0: rusult = (* _f_)(); break;
case 1: rusult = (* _f_)(args[0]); break;
case 2: rusult = (* _f_)(args[0], args[1]); break;
// ...
}
dlclose( dl_handle );
return rusult;
}
или то же самое для dll:
Код: Выделить всё
void * do_function_with_dll (char * dll_name, char * fun_name, void ** args, int n) {
void * rusult = NULL;
THANDLE dll = GetModuleHandle(dll_name);
if (dll == NULL) {
HINSTANCE _dll_ = LoadLibrary(dll_name);
if (_dll_ == NULL) {
// ... error-horror!!!
return rusult;
}
dll = _dll_;
}
PROC fun_handle = GetProcAddress(dll, fun_name);
if (fun_handle == NULL) {
// ... poor-yoric
return rusult;
}
switch (n) {
case 0: rusult = (*fun_handle)(); break;
case 1: rusult = (*fun_handle)(args[0]); break;
case 2: rusult = (*fun_handle)(args[0], args[1]); break;
// ...
}
FreeLibrary(dll);
return rusult;
}
Для системных вызовов уже бредово как-то, но всё равно:
Код: Выделить всё
extern int syscall(int, ...);
int do_syscall ( int sys_call, void ** args, int n) {
switch (n) {
case 0: return syscall(sys_call);
case 1: return syscall(sys_call, args[0]);
case 2: return syscall(sys_call, args[0], args[1]);
// ...
}
// error "n"
return -1;
}