From 5548119d8d68ea2d1a2cac6a563f67769ba7820f Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 28 Apr 2019 16:26:00 -0400 Subject: [PATCH] Big oops --- windows/winhresult.cpp | 85 +++++++++++++++++++++++++++++++++++++++--- windows/winhresult.hpp | 85 +++--------------------------------------- 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/windows/winhresult.cpp b/windows/winhresult.cpp index a723f04c..cf8998d6 100644 --- a/windows/winhresult.cpp +++ b/windows/winhresult.cpp @@ -1,8 +1,81 @@ // 28 april 2019 +#include "winapi.hpp" +#include "winhresult.hpp" -extern HRESULT WINAPI uiprivHrRegisterClassW(const WNDCLASSW *wc); -extern HRESULT WINAPI uiprivHrCreateWindowExW(DWORD exStyle, LPCWSTR className, LPCWSTR windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE hInstance, LPVOID lpParam, HWND *hwnd); -extern HRESULT WINAPI uiprivHrGetMessageW(LPMSG msg, HWND hwnd, UINT filterMin, UINT filterMax, BOOL *ret); -extern HRESULT WINAPI uiprivHrPostMessageW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -extern HRESULT WINAPI uiprivHrLoadIconW(HINSTANCE hInstance, LPCWSTR name, HICON *hIcon); -extern HRESULT WINAPI uiprivHrLoadCursorW(HINSTANCE hInstance, LPCWSTR name, HCURSOR *hCursor); +// This file wraps standard Windows API functions that don't use HRESULTs to return HRESULTs. +// It also calls SetLastError(0) before each such call. + +static inline HRESULT lastErrorCodeToHRESULT(DWORD lastError) +{ + if (lastError == 0) + return E_FAIL; + return HRESULT_FROM_WIN32(lastError); +} + +static inline HRESULT lastErrorToHRESULT(void) +{ + return lastErrorCodeToHRESULT(GetLastError()); +} + +HRESULT WINAPI uiprivHrRegisterClassW(const WNDCLASSW *wc) +{ + ATOM a; + + SetLastError(0); + a = RegisterClassW(wc); + if (a == 0) + return lastErrorToHRESULT(); + return S_OK; +} + +HRESULT WINAPI uiprivHrCreateWindowExW(DWORD exStyle, LPCWSTR className, LPCWSTR windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE hInstance, LPVOID lpParam, HWND *hwnd) +{ + SetLastError(0); + *hwnd = CreateWindowExW(exStyle, + className, windowName, + style, + x, y, width, height, + parent, menu, hInstance, lpParam); + if (*hwnd == NULL) + return lastErrorToHRESULT(); + return S_OK; +} + +// TODO turn ret into S_OK/S_FALSE? +HRESULT WINAPI uiprivHrGetMessageW(LPMSG msg, HWND hwnd, UINT filterMin, UINT filterMax, BOOL *ret) +{ + SetLastError(0); + *ret = GetMessageW(msg, hwnd, filterMin, filterMax); + if (*ret < 0) + return lastErrorToHRESULT(); + return S_OK; +} + +HRESULT WINAPI uiprivHrPostMessageW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + BOOL ret; + + SetLastError(0); + ret = PostMessageW(hwnd, uMsg, wParam, lParam); + if (ret == 0) + return lastErrorToHRESULT(); + return S_OK; +} + +HRESULT WINAPI uiprivHrLoadIconW(HINSTANCE hInstance, LPCWSTR name, HICON *hIcon) +{ + SetLastError(0); + *hIcon = LoadIconW(hInstance, name); + if (*hIcon == NULL) + return lastErrorToHRESULT(); + return S_OK; +} + +HRESULT WINAPI uiprivHrLoadCursorW(HINSTANCE hInstance, LPCWSTR name, HCURSOR *hCursor) +{ + SetLastError(0); + *hCursor = LoadCursorW(hInstance, name); + if (*hCursor == NULL) + return lastErrorToHRESULT(); + return S_OK; +} diff --git a/windows/winhresult.hpp b/windows/winhresult.hpp index 8f7b8704..a723f04c 100644 --- a/windows/winhresult.hpp +++ b/windows/winhresult.hpp @@ -1,81 +1,8 @@ // 28 april 2019 -#include "winapi.h" -#include "winhresult.h" -// This file wraps standard Windows API functions that don't use HRESULTs to return HRESULTs. -// It also calls SetLastError(0) before each such call. - -static inline HRESULT lastErrorCodeToHRESULT(DWORD lastError) -{ - if (lastError == 0) - return E_FAIL; - return HRESULT_FROM_WIN32(lastError); -} - -static inline HRESULT lastErrorToHRESULT(void) -{ - return lastErrorCodeToHRESULT(GetLastError()); -} - -HRESULT WINAPI uiprivHrRegisterClassW(const WNDCLASSW *wc) -{ - ATOM a; - - SetLastError(0); - a = RegisterClassW(wc); - if (a == 0) - return lastErrorToHRESULT(); - return S_OK; -} - -HRESULT WINAPI uiprivHrCreateWindowExW(DWORD exStyle, LPCWSTR className, LPCWSTR windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE hInstance, LPVOID lpParam, HWND *hwnd) -{ - SetLastError(0); - *hwnd = CreateWindowExW(exStyle, - className, windowName, - style, - x, y, width, height, - parent, menu, hInstance, lpParam); - if (*hwnd == NULL) - return lastErrorToHRESULT(); - return S_OK; -} - -// TODO turn ret into S_OK/S_FALSE? -HRESULT WINAPI uiprivHrGetMessageW(LPMSG msg, HWND hwnd, UINT filterMin, UINT filterMax, BOOL *ret) -{ - SetLastError(0); - *ret = GetMessageW(msg, hwnd, filterMin, filterMax); - if (*ret < 0) - return lastErrorToHRESULT(); - return S_OK; -} - -HRESULT WINAPI uiprivHrPostMessageW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - BOOL ret; - - SetLastError(0); - ret = PostMessageW(hwnd, uMsg, wParam, lParam); - if (ret == 0) - return lastErrorToHRESULT(); - return S_OK; -} - -HRESULT WINAPI uiprivHrLoadIconW(HINSTANCE hInstance, LPCWSTR name, HICON *hIcon) -{ - SetLastError(0); - *hIcon = LoadIconW(hInstance, name); - if (*hIcon == NULL) - return lastErrorToHRESULT(); - return S_OK; -} - -HRESULT WINAPI uiprivHrLoadCursorW(HINSTANCE hInstance, LPCWSTR name, HCURSOR *hCursor) -{ - SetLastError(0); - *hCursor = LoadCursorW(hInstance, name); - if (*hCursor == NULL) - return lastErrorToHRESULT(); - return S_OK; -} +extern HRESULT WINAPI uiprivHrRegisterClassW(const WNDCLASSW *wc); +extern HRESULT WINAPI uiprivHrCreateWindowExW(DWORD exStyle, LPCWSTR className, LPCWSTR windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE hInstance, LPVOID lpParam, HWND *hwnd); +extern HRESULT WINAPI uiprivHrGetMessageW(LPMSG msg, HWND hwnd, UINT filterMin, UINT filterMax, BOOL *ret); +extern HRESULT WINAPI uiprivHrPostMessageW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); +extern HRESULT WINAPI uiprivHrLoadIconW(HINSTANCE hInstance, LPCWSTR name, HICON *hIcon); +extern HRESULT WINAPI uiprivHrLoadCursorW(HINSTANCE hInstance, LPCWSTR name, HCURSOR *hCursor);