Add dl wrapper

This commit is contained in:
Zhi Guan
2022-11-02 12:21:02 +08:00
parent 86a5f4a257
commit 562ba9bb74
5 changed files with 68 additions and 56 deletions

View File

@@ -11,12 +11,10 @@
#ifndef SKFUTIL_SKF_INT_H
#define SKFUTIL_SKF_INT_H
#include <gmssl/dylib.h>
#include "../sgd.h"
#include "skf.h"
#ifdef WIN32
#include <windows.h>
#endif
typedef ULONG (DEVAPI *SKF_WaitForDevEvent_FuncPtr)(
@@ -476,11 +474,8 @@ typedef ULONG (DEVAPI *SKF_CloseHandle_FuncPtr)(
typedef struct skf_method_st {
char *name;
#ifdef WIN32
HMODULE dso;
#else
void *dso;
#endif
dylib_handle_t dso;
SKF_WaitForDevEvent_FuncPtr WaitForDevEvent;
SKF_CancelWaitForDevEvent_FuncPtr CancelWaitForDevEvent;
SKF_EnumDev_FuncPtr EnumDev;

View File

@@ -11,24 +11,15 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <gmssl/dylib.h>
#include "skf.h"
#include "skf_ext.h"
#include "skf_int.h"
#define SKFerr(e,r)
#ifdef WIN32
#define SKF_METHOD_BIND_FUNCTION_EX(func,name) \
skf->func = (SKF_##func##_FuncPtr)GetProcAddress(skf->dso, "SKF_"#name)
#else
#define SKF_METHOD_BIND_FUNCTION_EX(func,name) \
skf->func = (SKF_##func##_FuncPtr)dlsym(skf->dso, "SKF_"#name)
#endif
skf->func = (SKF_##func##_FuncPtr)dylib_get_function(skf->dso, "SKF_"#name)
#define SKF_METHOD_BIND_FUNCTION(func) \
SKF_METHOD_BIND_FUNCTION_EX(func,func)
@@ -43,17 +34,10 @@ SKF_METHOD *SKF_METHOD_load_library(const char *so_path)
SKFerr(SKF_F_SKF_METHOD_LOAD_LIBRARY, ERR_R_MALLOC_FAILURE);
goto end;
}
#ifdef WIN32
if ((skf->dso = LoadLibraryA(so_path)) == NULL) {
goto end;
}
#else
if (!(skf->dso = dlopen(so_path, 0/*RTLD_LAZY*/))) {//FIXME:dlopen not in windows
if (!(skf->dso = dylib_load_library(so_path))) {
SKFerr(SKF_F_SKF_METHOD_LOAD_LIBRARY, SKF_R_DSO_LOAD_FAILURE);
goto end;
}
#endif
SKF_METHOD_BIND_FUNCTION(WaitForDevEvent);
SKF_METHOD_BIND_FUNCTION(CancelWaitForDevEvent);