Skip to main content
Version: Dev (main)

Dynamic Loading

Source: qar-streaming-c/examples/dynamic_loading.c. Conceptual background: Load the installed runtime.

Dynamic Loading

What you build​

A program that finds qar-streaming-c.dll, the QAROS runtime library, loads it, prints which file it loaded and which runtime that is, then initializes the library and tears it down again. Every other QAROS program in C starts with these lines.

What you will learn:

  • how qar_library_load(NULL) finds the runtime, and when to pass a path instead
  • why loading is all or nothing
  • what to print so a user can tell which runtime your program runs against
  • the order of init, destroy and unload

Before you start​

  • Windows, with QAROS installed. No Hub needs to run: this program only loads the library.
  • This repository built with CMake: cmake --preset x64-windows, then cmake --build --preset x64-windows-debug. The programs land in build/x64-windows/qar-streaming-c/examples/Debug/.
  • A runtime at least as new as the header. The loader needs every function qar_streaming.h declares, so the installed QAROS must be at least as new as this repository's header.

Run it​

With QAROS installed, run it without arguments. The default lookup finds the installed runtime:

 .\build\x64-windows\qar-streaming-c\examples\Debug\dynamic_loading.exe

It prints this, with your runtime's version in place of <version>:

 Loading the QAROS runtime (default lookup)
 Loaded C:\Program Files\Quaternar\QAROS\bin\qar-streaming-c.dll (the installed QAROS)
 Runtime <version> (release, C API v0)
 Library initialized.
 qar_library_destroy: SUCCESS (code=0)
 Library unloaded. Dynamic loading tutorial complete.

The Loaded line names the file and, in brackets, the lookup step that found it. The Runtime line shows the flavor, release or demo. Started by a QAROS development hub, or with QAR_INSTANCE set to an instance's name, the same run loads that instance's runtime and prints (QAR_INSTANCE).

To load one library file and nothing else, name it:

 .\build\x64-windows\qar-streaming-c\examples\Debug\dynamic_loading.exe --qaros-library=C:\path\to\qar-streaming-c.dll

Exit codes:

CodeMeaning
0loaded, initialized and torn down
1an argument other than --qaros-library=PATH, or qar_library_init failed
2no runtime loaded

Walkthrough​

1. Decide where the runtime comes from​

A distributed application never names a file. It runs against the QAROS installed on the machine, so the Hub and the application use the same runtime. A path is for one specific file, such as a development build of QAROS.

The program takes the path from --qaros-library=PATH and refuses any other argument:

  const char* library_path = example_library_path(argc, argv);
  for(int i = 1; i < argc; ++i)
  {
  if(strncmp(argv[i], "--qaros-library=", strlen("--qaros-library=")) != 0)
  {
  print_usage(argv[0]);
  return 1;
  }
  }

The helper it calls returns NULL when the flag is absent:

 /** \brief The value of `--qaros-library=PATH` among the arguments, else NULL.
  *
  * NULL is what a shipped application passes to qar_library_load: the default
  * lookup finds the runtime of the QAROS installed on this machine. A path is
  * for one specific library file, such as a development build of QAROS. */
 static const char*
 example_library_path(int argc, char** argv)
 {
  static const char flag[] = "--qaros-library=";
  for(int i = 1; i < argc; ++i)
  {
  if(strncmp(argv[i], flag, sizeof(flag) - 1) == 0)
  {
  return argv[i] + sizeof(flag) - 1;
  }
  }
  return NULL;
 }

2. Load the library​

  if(library_path != NULL)
  {
  printf("Loading qar-streaming-c from: %s\n", library_path);
  }
  else
  {
  printf("Loading the QAROS runtime (default lookup)\n");
  }
  /* NULL runs the default lookup. On failure it has already printed why. */
  if(!qar_library_load(library_path))
  {
  fprintf(
  stderr,
  "No QAROS runtime loaded. Install QAROS, fix or unset QAR_INSTANCE, or pass "
  "--qaros-library=PATH.\n"
  );
  return 2;
  }

qar_library_load opens the library and resolves every function the header declares. If one is missing, it prints the missing name, unloads the file and returns false. You never hold a half-bound API that fails at its first call. A second call while the library is loaded returns true and loads nothing.

With a path, it tries that one file and nothing else. With NULL, it runs the default lookup that every QAROS binding follows. The first match wins:

StepWhere it looksPrinted as
0a qar-streaming-c.dll already loaded in this process
1the folder of the executable (or of the DLL that holds this code)beside the executable
2when QAR_INSTANCE names a QAROS instance, its runtime folder: default is the installed QAROS, any other name the RuntimeFolder recorded under HKCU\SOFTWARE\Quaternar\QAROS\Instances\<name>QAR_INSTANCE
3the installed QAROS: the bin folder of the InstallFolder the installer records in the registry (HKLM)the installed QAROS
4each folder on PATH, in orderPATH

Two steps end the lookup when they fail. When steps 0 and 1 find nothing and QAR_INSTANCE is set, the lookup ends at step 2: an unknown instance or a runtime missing there is an error, and the installed QAROS and PATH are not tried, even when they hold a good one. An installed QAROS whose runtime fails to load is an error too. The current directory is never searched. Each file loads from its absolute path, and its dependencies come from its own folder and System32, never from your executable's folder.

Do not ship qar-streaming-c.dll with your application. A copy beside the executable wins over the installation (step 1), and the Hub refuses a local app whose runtime is another version or flavor than its own.

3. Print what you loaded​

The C API does not report which step found the runtime. Since the lookup stops at the first step that has one, and steps 2 and 3 stop it even when they fail, the helper reads the step back from what is set on this machine, in the lookup's own order:

 /** \brief Print which runtime file was loaded, which lookup step found it, and
  * the runtime's version. Call it after qar_library_load succeeded.
  *
  * The default lookup stops at the first step that has a runtime, and steps 2
  * and 3 stop it even when they fail. So the step can be read back from what
  * is set on this machine, in the lookup's own order. */
 static void
 example_print_loaded_runtime(const char* library_path)
 {
 #ifdef _WIN32
  wchar_t loaded_file[MAX_PATH] = { 0 };
  wchar_t executable_file[MAX_PATH] = { 0 };
  const DWORD loaded_length =
  GetModuleFileNameW(g_qar_dynamic_library_handle, loaded_file, MAX_PATH);
  const DWORD executable_length = GetModuleFileNameW(NULL, executable_file, MAX_PATH);
  wchar_t* loaded_separator = wcsrchr(loaded_file, L'\\');
  wchar_t* executable_separator = wcsrchr(executable_file, L'\\');
  const bool beside_executable =
  loaded_length > 0 && loaded_length < MAX_PATH && executable_length > 0
  && executable_length < MAX_PATH && loaded_separator != NULL && executable_separator != NULL
  && (size_t)(loaded_separator - loaded_file)
  == (size_t)(executable_separator - executable_file)
  && _wcsnicmp(loaded_file, executable_file, (size_t)(loaded_separator - loaded_file)) == 0;
 
  char folder[1024] = { 0 };
  wchar_t instance[QAR_INSTANCE_NAME_MAX_LENGTH + 2] = { 0 };
  const char* lookup_step = NULL;
  if(library_path != NULL)
  {
  lookup_step = "the file you named";
  }
  else if(beside_executable)
  {
  lookup_step = "beside the executable";
  }
  else if(qar_requested_instance_w(instance, QAR_INSTANCE_NAME_MAX_LENGTH + 2))
  {
  lookup_step = "QAR_INSTANCE";
  }
  else if(qar_installed_bin_folder(folder, sizeof(folder)))
  {
  lookup_step = "the installed QAROS";
  }
  else
  {
  lookup_step = "PATH";
  }
  printf("Loaded %ls (%s)\n", loaded_file, lookup_step);
 #else
  printf("Loaded %s\n", library_path != NULL ? library_path : "qar-streaming-c");
 #endif
 
  QarRuntimeInfo runtime_info;
  memset(&runtime_info, 0, sizeof(runtime_info));
  QarResult info_result = qar_runtime_info(&runtime_info);
  if(qar_result_is_success(info_result))
  {
  printf(
  "Runtime %s (%s, C API %s)\n",
  runtime_info.version,
  runtime_info.flavor,
  runtime_info.c_api
  );
  }
  else
  {
  log_result("qar_runtime_info", info_result);
  }
 }

GetModuleFileNameW on g_qar_dynamic_library_handle, the handle QAR_IMPLEMENT_DYNAMIC_LOADING() defines, gives the file. qar_runtime_info gives the runtime's version, its flavor and the C API generation; it works before qar_library_init. qar_requested_instance_w and qar_installed_bin_folder read the same environment variable and registry value the lookup reads.

Print this in your own application too. A copy of the runtime beside the executable wins over the installation, and this line is how you notice. A bug report with the file and the version saves a round of questions.

4. Initialize the library​

  library_init.enable_console_logging = true;
  library_init.log_folder_path = NULL; /* no log file: the console is enough here */
 
  QarResult init_result = qar_library_init(&library_init);
  if(qar_result_is_error(init_result))
  {
  log_result("qar_library_init", init_result);
  return 1;
  }
  printf("Library initialized.\n");

qar_library_init sets up the process-wide state, once per process, before any runtime is created. Console logging prints the library's log lines to the console; log_folder_path names a folder for a log file, qar_streaming.log. When it fails, unload the library before you stop.

5. Tear down​

  /* Destroy first: it needs the functions that unloading removes. */
  log_result("qar_library_destroy", qar_library_destroy());
  printf("Library unloaded. Dynamic loading tutorial complete.\n");

Ownership and teardown in C​

Troubleshooting​

What you seeCauseWhat to do
qar-streaming-c.dll is not beside this module, QAR_INSTANCE is not set, QAROS is not installed ..., exit 2no runtime in any step of the lookupinstall QAROS
QAR_INSTANCE is '...', but no runtime folder is recorded for that instance ... or ... but no loadable qar-streaming-c.dll is in its runtime folder ..., exit 2the variable names an instance without a loadable runtime; the lookup does not go onstart that instance's hub, or fix or unset the variable
Failed to load symbol '...' for module '...'., exit 2the runtime is older than the header: it lacks a function the header declaresinstall a QAROS at least as new as this repository's header
No QAROS runtime loaded. alone, after Loading qar-streaming-c from:the file you named does not exist or does not loadname the qar-streaming-c.dll inside the runtime's folder, where its dependencies are
Loaded ... (beside the executable) when you expected the installationa copy of the runtime sits beside your executable and winsremove it; do not ship the runtime with your application

The Troubleshooting page covers what comes after loading.

Next​


Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.