Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Created May 10, 2020 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickNaso/8df948f4932af206f6537d5ba539f70b to your computer and use it in GitHub Desktop.
Save NickNaso/8df948f4932af206f6537d5ba539f70b to your computer and use it in GitHub Desktop.
C function to get a symbol from the current process
#ifndef _GET_SYMBOL_FROM_CURRENT_PROCESS_H
#define _GET_SYMBOL_FROM_CURRENT_PROCESS_H
#include <assert.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
inline
void* get_symbol_from_current_process(const char* name) {
#ifdef _WIN32
HMODULE handle = GetModuleHandle(NULL);
assert(handle != NULL);
return (void*) GetProcAddress(handle, name);
#else
void* handle = dlopen(NULL, RTLD_LAZY);
assert(handle != NULL);
void* sym = dlsym(handle, name);
dlclose(handle);
dlerror(); // Clear any possible errors.
return sym;
#endif
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment