nRF Command Line Tools v10.11.1

Loading the DLL at run-time

You can manually load the nrfjprog DLL when the application is running.

This method provides more flexibility than build-time loading by allowing you to load a limited set of DLL functions. In addition, it allows more complex mechanisms for finding the library file. Manual loading also prevents the DLL from “polluting” the global name space with function names.

The following platform-specific code snippets describe how to load and call one function of the nrfjprog DLL. Remember that error checking should be done in each step of the code, but for simplicity this is not illustrated in the following code snippets.

  1. Include the necessary header files:
    • On Windows:
      #include "nrfjprogdll.h"
      #include <windows.h>
    • On Linux or macOS:
      #include "nrfjprogdll.h"
      #include <dlfcn.h>
  2. Load the DLL:
    • On Windows:
      HMODULE dll = LoadLibrary("nrfjprog.dll");
    • On Linux:
      void * dll = dlopen("libnrfjprogdll.so", RTLD_LAZY);
    • On macOS:
      void * dll = dlopen("libnrfjprogdll.dylib", RTLD_LAZY);
  3. Declare a function pointer type to store the address of the DLL function:
    typedef nrfjprogdll_err_t (*Dll_NRFJPROG_is_halted_t)(bool * is_device_halted);
  4. Define a function pointer and load into it the DLL function address:
    • On Windows:
      Dll_NRFJPROG_is_halted_t NRFJPROG_is_halted = 
          (Dll_NRFJPROG_is_halted_t)GetProcAddress(dll, "NRFJPROG_is_halted");
    • On Linux or macOS:
      Dll_NRFJPROG_is_halted_t NRFJPROG_is_halted = 
          (Dll_NRFJPROG_is_halted_t)dlsym(dll, "NRFJPROG_is_halted");
  5. Call the function, for example:
    bool halted;
    NRFJPROG_is_halted(&halted);
  6. Free the DLL:
    • On Windows:
      FreeLibrary(dll);
    • On Linux or macOS:
      dlclose(dll);