Cross-Platform GUI Programming with wxWidgets

If you need to run functions in dynamic libraries, you can use the wxDynamicLibrary class. Pass the name of the dynamic library to the constructor or Load, and pass wxDL_VERBATIM if you don't want wxWidgets to append an appropriate extension, such as .dll on Windows or .so on Linux. If the library was loaded successfully, you can load functions by name using GetSymbol. Here's an example that loads and initializes the common controls library on Windows:

#include "wx/dynlib.h" INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(icex); icex.dwICC = ICC_DATE_CLASSES; // Load comctl32.dll wxDynamicLibrary dllComCtl32(wxT("comctl32.dll"), wxDL_VERBATIM); // Define the ICCEx_t type typedef BOOL (WINAPI *ICCEx_t)(INITCOMMONCONTROLSEX *); // Get the InitCommonControlsEx symbol ICCEx_t pfnInitCommonControlsEx = (ICCEx_t) dllComCtrl32.GetSymbol(wxT("InitCommonControlsEx")); // Call the function to initialize the common controls library if ( pfnInitCommonControlsEx ) { (*pfnInitCommonControlsEx)(&icex); }

You could also write the GetSymbol line more succinctly using the wxDYNLIB_FUNCTION macro:

wxDYNLIB_FUNCTION( ICCEx_t, InitCommonControlsEx, dllComCtl32 );

wxDYNLIB_FUNCTION allows you to specify the type only once, as the first parameter, and creates a variable of this type named after the function but with a pfn prefix.

If the library was loaded successfully in the constructor or Load, the function Unload will be called automatically in the destructor to unload the library from memory. Call Detach if you want to keep a handle to the library after the wxDynamicLibrary object has been destroyed.

    Категории