From 43b0bdc2bfbb8ce198a0f031e394bcc35cde12f3 Mon Sep 17 00:00:00 2001 From: Sam Hartman Date: Wed, 28 Sep 2011 20:55:18 +0000 Subject: Moved LoadFuncs/UnloadFuncs() to windows/lib Signed-off-by: Alexey Melnikov git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25257 dc483132-0cff-0310-8789-dd5450dbe970 --- src/windows/lib/Makefile.in | 7 ++-- src/windows/lib/loadfuncs.c | 88 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/windows/lib/loadfuncs.c (limited to 'src/windows/lib') diff --git a/src/windows/lib/Makefile.in b/src/windows/lib/Makefile.in index c911de701..119db8902 100644 --- a/src/windows/lib/Makefile.in +++ b/src/windows/lib/Makefile.in @@ -2,9 +2,12 @@ BUILDTOP = ..\.. lib-windows: $(OUTPRE)libwin.lib -SRCS= vardlg.c gic.c registry.c +SRCS= vardlg.c gic.c registry.c loadfuncs.c + + +OBJS= $(OUTPRE)vardlg.obj $(OUTPRE)gic.obj $(OUTPRE)registry.obj \ + $(OUTPRE)loadfuncs.obj -OBJS= $(OUTPRE)vardlg.obj $(OUTPRE)gic.obj $(OUTPRE)registry.obj $(OUTPRE)libwin.lib: $(OBJS) lib /nologo /out:$*.lib $(OBJS) diff --git a/src/windows/lib/loadfuncs.c b/src/windows/lib/loadfuncs.c new file mode 100644 index 000000000..44ec54dfa --- /dev/null +++ b/src/windows/lib/loadfuncs.c @@ -0,0 +1,88 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include "loadfuncs.h" + +// +// UnloadFuncs: +// +// This function will reset all the function pointers of a function loaded +// by LaodFuncs and will free the DLL instance provided. +// + +void +UnloadFuncs( + FUNC_INFO fi[], + HINSTANCE h + ) +{ + int n; + if (fi) + for (n = 0; fi[n].func_ptr_var; n++) + *(fi[n].func_ptr_var) = NULL; + if (h) FreeLibrary(h); +} + + +// +// LoadFuncs: +// +// This function try to load the functions for a DLL. It returns 0 on failure +// and non-zero on success. The parameters are descibed below. +// + +int +LoadFuncs( + const char* dll_name, + FUNC_INFO fi[], + HINSTANCE* ph, // [out, optional] - DLL handle + int* pindex, // [out, optional] - index of last func loaded (-1 if none) + int cleanup, // cleanup function pointers and unload on error + int go_on, // continue loading even if some functions cannot be loaded + int silent // do not pop-up a system dialog if DLL cannot be loaded + + ) +{ + HINSTANCE h; + int i, n, last_i; + int error = 0; + UINT em; + + if (ph) *ph = 0; + if (pindex) *pindex = -1; + + for (n = 0; fi[n].func_ptr_var; n++) + *(fi[n].func_ptr_var) = NULL; + + if (silent) + em = SetErrorMode(SEM_FAILCRITICALERRORS); + h = LoadLibrary(dll_name); + if (silent) + SetErrorMode(em); + + if (!h) + return 0; + + last_i = -1; + for (i = 0; (go_on || !error) && (i < n); i++) + { + void* p = (void*)GetProcAddress(h, fi[i].func_name); + if (!p) + error = 1; + else + { + last_i = i; + *(fi[i].func_ptr_var) = p; + } + } + if (pindex) *pindex = last_i; + if (error && cleanup && !go_on) { + for (i = 0; i < n; i++) { + *(fi[i].func_ptr_var) = NULL; + } + FreeLibrary(h); + return 0; + } + if (ph) *ph = h; + if (error) return 0; + return 1; +} -- cgit