C++ Dynamic Library Loading

Loading a Windows DLL
We start by creating our Dynamic Link Library in a separate project. The file lib.cpp contains our library function:
 * 1) include "windows.h"
 * 2) include

extern "C" _declspec(dllexport) std::string my_func { return std::string("SKINKE"); } We declare the type of each library function in lib.h:
 * 1) pragma once
 * 2) include

typedef std::string(* My_func_ptr); In our main program, we include lib.h and use LoadLibrary to load the DLL and GetProcAddress to get our function:
 * 1) include
 * 2) include
 * 3) include 
 * 4) include "lib.h" // our library

int main { HINSTANCE my_lib = LoadLibraryA("lib.dll"); // note that the file 'lib.dll' will be locked until FreeLibrary is called My_func_ptr my_func = reinterpret_cast(GetProcAddress(my_lib, "my_func")); std::cout << "Output " << my_func << std::endl; FreeLibrary(my_lib); // the file 'lib.dll' is now unlocked return 0; } Note that LoadLibrary increments a global reference counter on lib.dll, and FreeLibrary decrements it. The system can unload lib.dll when the reference count is 0.