Created
May 10, 2020 15:06
-
-
Save NickNaso/8df948f4932af206f6537d5ba539f70b to your computer and use it in GitHub Desktop.
C function to get a symbol from the current process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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