Zu den Fehlernummern, die mit der Methode GetLastError()
vom Windows-System ermittelt werden, kann mit der folgenden Methode der
zugehörige Text ermittelt werden.
void FormatError(const DWORD errorCode, CString& message) const { LPVOID buffer = NULL; if (0L < ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorCode, 0, (LPTSTR) &buffer, 0, NULL)) { message = (LPCSTR) buffer; ::LocalFree(buffer); } else { message.Format("Error %d", errorCode); } }
Dynamisches Laden von Bibliotheken zur Laufzeit eines Programms.
Deklaration der Basisklasse:
class LibraryLoader { private: HINSTANCE m_library; protected: LibraryLoader(); virtual ~LibraryLoader(); bool Load(const char* path); void Free(); FARPROC GetFunction(const char* functionName) const; virtual bool GetFunctions() = 0; public: bool IsLoaded() const {return (NULL != m_library); } virtual bool Load() = 0; };
Implementierung der Basisklasse:
LibraryLoader::LibraryLoader() : m_library(NULL) { } LibraryLoader::~LibraryLoader() { Free(); } bool LibraryLoader::Load(const char* path) { if (! IsLoaded()) { m_library = ::LoadLibrary(path); if (NULL != m_library) { return GetFunctions(); } } return false; } void LibraryLoader::Free() { if (IsLoaded()) { if (::FreeLibrary(m_library)) { m_library = NULL; } } } FARPROC LibraryLoader::GetFunction(const char* functionName) const { if (IsLoaded()) { return ::GetProcAddress(m_library, functionName); } throw "Failed to get function!"; }
Implementierung am Beispiel einer Klasse, die ein paar Funktionen der Windows-DLL "user32.dll" aufruft.
Deklaration der Beispielklasse:
class ExampleLoader : public LibraryLoader { private: static const char* LIBRARY_PATH; typedef int (WINAPI *LPFN_MESSAGE_BOX)(HWND window, LPCTSTR text, LPCTSTR title, UINT type); typedef BOOL (WINAPI *LPFN_IS_WINDOW_VISIBLE)(HWND window); typedef BOOL (WINAPI *LPFN_GET_WINDOW_RECT)(HWND window, LPRECT rect); LPFN_MESSAGE_BOX m_functionMessageBox; LPFN_IS_WINDOW_VISIBLE m_functionIsWindowVisible; LPFN_GET_WINDOW_RECT m_functionGetWindowRect; protected: virtual bool GetFunctions(); public: ExampleLoader(); virtual ~ExampleLoader(); virtual bool Load(); int MessageWindow(const CString& title, const CString& text, unsigned int type) const; bool IsVisible(HWND window) const; bool GetRect(HWND window, long& left, long& top, long& right, long& bottom) const; };
Implementierung der Beispielklasse:
const char* ExampleLoader::LIBRARY_PATH = "user32.dll"; ExampleLoader::ExampleLoader() : m_functionMessageBox(NULL) , m_functionIsWindowVisible(NULL) , m_functionGetWindowRect(NULL) { } ExampleLoader::~ExampleLoader() { } bool ExampleLoader::Load() { return LibraryLoader::Load(ExampleLoader::LIBRARY_PATH); } bool ExampleLoader::GetFunctions() { if (IsLoaded()) { m_functionMessageBox = (LPFN_MESSAGE_BOX) LibraryLoader::GetFunction("MessageBoxA"); m_functionIsWindowVisible = (LPFN_IS_WINDOW_VISIBLE) LibraryLoader::GetFunction("IsWindowVisible"); m_functionGetWindowRect = (LPFN_GET_WINDOW_RECT) LibraryLoader::GetFunction("GetWindowRect"); if ( (NULL != m_functionMessageBox) && (NULL != m_functionIsWindowVisible) && (NULL != m_functionGetWindowRect) ) { return true; } } return false; } int ExampleLoader::MessageWindow(const CString& title, const CString& text, unsigned int type) const { if (NULL != m_functionMessageBox) { return m_functionMessageBox(NULL, text, title, type); } return 0L; } bool ExampleLoader::IsVisible(HWND window) const { if (NULL != m_functionIsWindowVisible) { BOOL isVisible = m_functionIsWindowVisible(window); return (TRUE == isVisible) ? true : false; } return false; } bool ExampleLoader::GetRect(HWND window, long& left, long& top, long& right, long& bottom) const { if (NULL != m_functionGetWindowRect) { RECT rectangle; if (m_functionGetWindowRect(window, &rectangle)) { left = rectangle.left; top = rectangle.top; right = rectangle.right; bottom = rectangle.bottom; return true; } } return false; }
Verwendung der Beispielklasse:
#include "LibraryLoader.h" #include "ExampleLoader.h" void UseExampleLoader() { ExampleLoader loader; HWND hwnd = NULL; long left = 0L; long top = 0L; long right = 0L; long bottom = 0L; ... loader.Load(); loader.MessageWindow("Titel", "Text", MB_ICONINFORMATION); loader.IsVisible(hwnd); loader.GetRect(hwnd, left, top, right, bottom); }