Last active
November 20, 2020 13:59
-
-
Save pachuco/7a0f5b14f96b291167ba83ca041f3a8e to your computer and use it in GitHub Desktop.
Easilly create function pointers out of existing function definitions
This file contains 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
#pragma once | |
#include <windows.h> | |
#define DEFFUN_OBJ(_HMOD, _ERR, _FNAME) __typeof__(_FNAME) *pf##_FNAME = NULL | |
#define DEFFUN_EXT(_HMOD, _ERR, _FNAME) extern __typeof__(_FNAME) *pf##_FNAME | |
#define DYNLOAD(_HMOD, _ERR, _FNAME) _ERR = _ERR ? _ERR : !(pf##_FNAME = (void*)GetProcAddress(_HMOD, #_FNAME)) | |
//usage scenario | |
//You have a header full of function definitions that would result in linking to dll | |
//But you want to use them as dynamically loaded functions instead. | |
//usage example | |
/* | |
#define INPOUT32_FOREACH(_ACT, _HMOD, _ERR) \ | |
_ACT(_HMOD, _ERR, Inp32); \ | |
_ACT(_HMOD, _ERR, Out32); \ | |
_ACT(_HMOD, _ERR, DlPortReadPortUchar); \ | |
_ACT(_HMOD, _ERR, DlPortWritePortUchar); | |
INPOUT32_FOREACH(DEFFUN_OBJ, _, _); | |
BOOL init() { | |
HANDLE dllHandle = LoadLibraryA("inpout32.dll"); | |
if (dllHandle) { | |
BOOL isFail = FALSE; | |
INPOUT32_FOREACH(DYNLOAD, dllHandle, isFail); | |
return !isFail; | |
} | |
return FALSE; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment