From 76c1cc869f1eaa08be0fcca186cf478076c6b6e6 Mon Sep 17 00:00:00 2001 From: ptahmose Date: Fri, 18 Oct 2024 06:35:26 +0200 Subject: [PATCH] Update dynamic library loading and artifact paths Modified build.yml to update artifact paths and remove Octave-MEX upload. Enhanced octavelibczi.c with platform-specific code for dynamic library loading on Windows and non-Windows systems. Updated Initialize function to include error handling for library loading. --- .github/workflows/build.yml | 2 +- OctaveMex/octavelibczi.c | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 45deaaa..31f7481 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -124,7 +124,7 @@ jobs: mkdir -p artifacts name="Octave-MEXlibCZI-windows-x64-$(git describe --always)" mkdir -p artifacts/${name} - cp $(cygpath "${GITHUB_WORKSPACE}")/OctaveMex/libmexlibczi.dll artifacts/${name}/ + cp $(cygpath "${GITHUB_WORKSPACE}")/libmexlibczi/libmexlibczi.dll artifacts/${name}/ cp $(cygpath "${GITHUB_WORKSPACE}")/OctaveMex/octavelibczi.mex artifacts/${name}/ echo "artifactName=${name}" >> "$GITHUB_ENV" echo "artifactPath=artifacts/${name}" >> "$GITHUB_ENV" diff --git a/OctaveMex/octavelibczi.c b/OctaveMex/octavelibczi.c index 16f82e1..55295a8 100644 --- a/OctaveMex/octavelibczi.c +++ b/OctaveMex/octavelibczi.c @@ -2,7 +2,14 @@ #include "mex.h" #include "../AppModel/include/app_api.h" #include +#ifdef _WIN32 #include +#else +#include +#include +#include +#include +#endif static bool octaveMexIsNanOrInfDouble(double value) { @@ -300,7 +307,7 @@ static void Initialize() // * we try to load the library from the same folder where this mex file is located // * therefore, we first get the handle of the module of this mex file, use this handle to get the path of the mex file // * and then, we replace the file name with the name of the dynamic library - +#ifdef _WIN32 HMODULE hModuleOfMexFile; BOOL B = GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)&Initialize, &hModuleOfMexFile); if (!B) @@ -337,6 +344,36 @@ static void Initialize() pfn_OnInitialize = (void(*)())GetProcAddress(hModule, "OnInitialize"); pfn_OnShutdown = (void(*)())GetProcAddress(hModule, "OnShutdown"); pfn_mexFunction = (void(*)(int, Parameter[], int, const Parameter[], struct IAppExtensionFunctions*))GetProcAddress(hModule, "mexFunction"); +#else + Dl_info dl_info; + if (dladdr((void*)&Initialize, &dl_info) == 0) + { + mexErrMsgIdAndTxt("MATLAB:mexlibCZI:dladdrFailed", "Failed to get the handle of the module."); + } + + char* path = (char*)malloc(PATH_MAX + strlen(DllName) + 1); + strncpy(path, dl_info.dli_fname, PATH_MAX); + char* last_slash = strrchr(path, '/'); + if (last_slash != NULL) + { + strcpy(last_slash + 1, DllName); + } + else + { + strcpy(path, DllName); + } + + void* hModule = dlopen(path, RTLD_LAZY); + free(path); + if (hModule == NULL) + { + mexErrMsgIdAndTxt("MATLAB:mexlibCZI:dlopenFailed", "Failed to load the library."); + } + + pfn_OnInitialize = (void(*)())dlsym(hModule, "OnInitialize"); + pfn_OnShutdown = (void(*)())dlsym(hModule, "OnShutdown"); + pfn_mexFunction = (void(*)(int, Parameter[], int, const Parameter[], struct IAppExtensionFunctions*))dlsym(hModule, "mexFunction"); +#endif if (pfn_OnInitialize == NULL || pfn_OnShutdown == NULL || pfn_mexFunction == NULL) { FreeLibrary(hModule);