Windows OS is full of DLLs (Dynamic Link Library). Each DLL file has some APIs, which can be used whenever needed (avoid re-inventing the wheel). For example, the system DLL User32.dll provides a very basic MessageBox API (The ANSI version is MessageBoxA and the Unicode Version is MessageBoxW).
The API declaration is as follows:
1 2 3 4 5 6 | int WINAPI MessageBox( _In_opt_ HWND hWnd, _In_opt_ LPCTSTR lpText, _In_opt_ LPCTSTR lpCaption, _In_ UINT uType ); |
int WINAPI MessageBox( _In_opt_ HWND hWnd, _In_opt_ LPCTSTR lpText, _In_opt_ LPCTSTR lpCaption, _In_ UINT uType );
If you are using g++ for windows (e.g. Mingw compilers), the following shows you a quick way to interact with the Windows API.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <windows.h> int main() { int msgboxID = MessageBoxA( NULL, "Hello, World!", "https://HelloACM.com", 0 ); return msgboxID; } |
#include <windows.h> int main() { int msgboxID = MessageBoxA( NULL, "Hello, World!", "https://HelloACM.com", 0 ); return msgboxID; }
The MessageBoxA is declared in header file windows.h. And this kind of method calling is called static binding. It means that the application requires the DLL at the startup or it will fail showing that “User32.DLL” is not found (unlikely because it is system library).
The other way of loading the library whenever needed and call the method after library loaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <windows.h> #include <iostream> typedef int (*Msg)(HWND, LPCTSTR , LPCTSTR , UINT); int main() { HINSTANCE hDLL = LoadLibrary("User32.dll"); if (hDLL == NULL) { std::cout << "Failed to load the library.\n"; } else { std::cout << "Library loaded.\n"; Msg MsgBox = (Msg)GetProcAddress(hDLL, "MessageBoxA"); MsgBox(0, "https://HelloACM.com", "Hello, World!", 0); } FreeLibrary(hDLL); return 0; } |
#include <windows.h> #include <iostream> typedef int (*Msg)(HWND, LPCTSTR , LPCTSTR , UINT); int main() { HINSTANCE hDLL = LoadLibrary("User32.dll"); if (hDLL == NULL) { std::cout << "Failed to load the library.\n"; } else { std::cout << "Library loaded.\n"; Msg MsgBox = (Msg)GetProcAddress(hDLL, "MessageBoxA"); MsgBox(0, "https://HelloACM.com", "Hello, World!", 0); } FreeLibrary(hDLL); return 0; }
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: SQL Coding Exercise - Rank Scores
Next Post: How to Check If Object Is Empty in Javascript?
it’s great example!