I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting:
void *gptr = dlsym(
well, if you know what the argument list is, it's extremely simple to just c-cast it. As stated above, it has undefined behavior, but I've been using this in my own events handler for a pet project and it seems to work just fine on msvc.
I can cast the same void* to _beginthread_proc_type to start a thread with _beginthread, and that also doesn't seem to cause any issues (though I don't really know what the consequences of sending arguments to a function that doesn't require any, or not sending arguments to functions that do require arguments will do, that does seem to at least call the function/start the thread in my limited testing)
void somefunction(){
std::cout <<"hi"<<std::endl;
}
void* function = (void*)&somefunction;
((void(__cdecl*)(void))(function)) ();
_beginthread((_beginthread_proc_type)function, 0, NULL);
I know the community has developed a hatred for macros, but I use a macro for that function call in my events handler.
#define call_voidstar_function(fc) ((void(__cdecl*)(void))(fc)) ()
I found this (a bit ugly) solution. gcc with maximum warning level does not complain. This example calls dlsym() (that returns a void*) and returns the result in a function pointer.
typedef void (*FUNPTR)();
FUNPTR fun_dlsym(void* handle, const char* name) {
union {
void* ptr;
FUNPTR fptr;
} u;
u.ptr = dlsym(handle, name);
return u.fptr;
}