Adding to what ONEEYEMAN wrote:
If you want to call functions from a DLL that may not be available when your program starts, you need to use run-time dynamic linking. If you would link such functions "normally" (load-time dynamic linking using an imported library) and the DLL could not be found, your program will not start.
You would use run-time linking not because a programmer did not want to install an SDK, you do that when the DLL (or a function in DLL may be available only since a certain version of the DLL) may not be present at the user computer and you still want the application to start - run-time linking is more work than the usual load-time linking, header files are not used as the functions cannot be just simply called and are useful only for obtaining the information about function properties.
The following example demonstrates how to call a function from DLL loaded at run-time, As you can see, it's quite a hassle even with a single simple function.
Code: Select all
#include <wx/wx.h>
#include <wx/dynlib.h>
class MyApp : public wxApp
{
public:
bool OnInit()
{
// The function we want to call is declared as
// int ShellAbout(_In_opt_ HWND hWnd, _In_ LPCTSTR szApp, _In_opt_ LPCTSTR szOtherStuff, _In_opt_ HICON hIcon);
// first create the function typedef
// The function actually exists in ANSI (ending with A) and Unicode versions (ending with W)
// we want the Unicode version
typedef int (WINAPI *ShellAboutW_t) (HWND, LPCTSTR, LPCTSTR, HICON);
// declare the function variable
ShellAboutW_t pfnShellAboutW = NULL;
wxDynamicLibrary dllShell32; // ShellAbout is in shell32.dll
if ( !dllShell32.Load(wxS("shell32.dll"), wxDL_VERBATIM | wxDL_QUIET) )
{
wxMessageBox("Could not load shell32.dll.");
return false;
}
wxDL_INIT_FUNC(pfn, ShellAboutW, dllShell32);
if ( !pfnShellAboutW )
{
wxMessageBox("Could not load ShellAboutW().");
return false;
}
wxString title("wxWidgets application");
wxString hello("Hello from wxWidgets!");
int result = pfnShellAboutW(NULL, title.wc_str(), hello.wc_str(), NULL);
return false;
}
};
wxIMPLEMENT_APP(MyApp);