Skip to content

Commit

Permalink
Update dynamic library loading and artifact paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptahmose committed Oct 18, 2024
1 parent f4e1c00 commit 76c1cc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 38 additions & 1 deletion OctaveMex/octavelibczi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
#include "mex.h"
#include "../AppModel/include/app_api.h"
#include <string.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <dlfcn.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#endif

static bool octaveMexIsNanOrInfDouble(double value)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 76c1cc8

Please sign in to comment.