From: misomosi Date: Tue, 7 May 2024 05:48:38 +0000 (-0400) Subject: Load functions lazily X-Git-Url: http://git.misomosispi.com/?a=commitdiff_plain;h=242bde1483bdd1021d5b673f19f705bf87012800;p=pldh.git Load functions lazily --- diff --git a/examples/multipld.c b/examples/multipld.c new file mode 100644 index 0000000..d0850a7 --- /dev/null +++ b/examples/multipld.c @@ -0,0 +1,19 @@ +#include "../pld.h" + +#define PLDH k32 +PLD("KERNEL32.DLL"); +static DWORD MyWriteFile(void) +{ + DWORD dummy; + PFN(h, WriteFile); + return h(GetStdHandle(STD_OUTPUT_HANDLE), "Hello World!\n", 13, &dummy, NULL); +} +#undef PLDH +#define PLDH u32 +PLD("USER32.DLL"); + +int main(void) { + MyWriteFile(); + PFN(MessageBoxA)(NULL, "Hello!", "Hello!", MB_OK); + return 0; +} diff --git a/examples/writefile.c b/examples/writefile.c index 5f6e4b3..6134773 100644 --- a/examples/writefile.c +++ b/examples/writefile.c @@ -2,7 +2,8 @@ PLD("KERNEL32.DLL") int main(void) { + PFN(h, WriteFile); DWORD dummy; - PFN(WriteFile)(GetStdHandle(STD_OUTPUT_HANDLE), "Hello World!\n", 13, &dummy, NULL); + h(GetStdHandle(STD_OUTPUT_HANDLE), "Hello World!\n", 13, &dummy, NULL); return 0; } diff --git a/examples/writefile.cpp b/examples/writefile.cpp new file mode 100644 index 0000000..2b92926 --- /dev/null +++ b/examples/writefile.cpp @@ -0,0 +1,8 @@ +#include "../pld.h" + +PLD("KERNEL32.DLL") +int main(void) { + DWORD dummy; + PFNFIND(WriteFile)(GetStdHandle(STD_OUTPUT_HANDLE), "Hello World!\n", 13, &dummy, NULL); + return 0; +} diff --git a/pld.h b/pld.h index 7068653..d0d1db9 100644 --- a/pld.h +++ b/pld.h @@ -199,7 +199,6 @@ static PLD_LibHandle PLD_Private_dlopen_wrapper(const char *name) return handle; } - #define PLD(name) \ static PLD_LibHandle PLDH(void) { \ static PLD_LibHandle handle = NULL; \ @@ -209,7 +208,13 @@ static PLD_LibHandle PLD_Private_dlopen_wrapper(const char *name) // For importing by ordinal on Windows or just using // another name in a library for whatever reason -#define PFNALT(name, alt) ((PLDTYPEOF(&name))PLD_Private_dlsym_wrapper(PLDH(), alt)) -#define PFN(name) PFNALT(name, #name) +#define PFNALTFIND(name, alt) ((PLDTYPEOF(&name))PLD_Private_dlsym_wrapper(PLDH(), alt)) +#define PFNFIND(name) PFNALTFIND(name, #name) + +// Lazily load functions +#define PFNALT(h, name, alt) \ + static PLDTYPEOF(&name) h = NULL; \ + if (h == NULL) h = PFNALTFIND(name, alt) +#define PFN(h, name) PFNALT(h, name, #name) #endif // COOL_PLD_H