|
| 1 | +//**@@@*@@@**************************************************** |
| 2 | +// |
| 3 | +// Microsoft Windows |
| 4 | +// Copyright (C) Microsoft Corporation. All rights reserved. |
| 5 | +// |
| 6 | +//**@@@*@@@**************************************************** |
| 7 | + |
| 8 | +// |
| 9 | +// FileName: CommonMacros.h |
| 10 | +// |
| 11 | +// Abstract: Useful macros |
| 12 | +// |
| 13 | +// ---------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +#pragma once |
| 17 | +#include <windef.h> |
| 18 | +#include <windows.h> |
| 19 | + |
| 20 | + |
| 21 | +//------------------------------------------------------------------------- |
| 22 | +// Description: |
| 23 | +// |
| 24 | +// If the condition evaluates to TRUE, jump to the given label. |
| 25 | +// |
| 26 | +// Parameters: |
| 27 | +// |
| 28 | +// condition - [in] code that fits in if statement |
| 29 | +// label - [in] label to jump if condition is met |
| 30 | +// |
| 31 | +#define IF_TRUE_JUMP(condition, label) \ |
| 32 | + if (condition) \ |
| 33 | + { \ |
| 34 | + goto label; \ |
| 35 | + } |
| 36 | + |
| 37 | +//------------------------------------------------------------------------- |
| 38 | +// Description: |
| 39 | +// |
| 40 | +// If the condition evaluates to FALSE, jump to the given label. |
| 41 | +// |
| 42 | +// Parameters: |
| 43 | +// |
| 44 | +// condition - [in] code that fits in if statement |
| 45 | +// label - [in] label to jump if condition is met |
| 46 | +// |
| 47 | +#define IF_FALSE_JUMP(condition, label) \ |
| 48 | + if (!condition) \ |
| 49 | + { \ |
| 50 | + goto label; \ |
| 51 | + } |
| 52 | + |
| 53 | +//------------------------------------------------------------------------- |
| 54 | +// Description: |
| 55 | +// |
| 56 | +// If the hresult passed FAILED, jump to the given label. |
| 57 | +// |
| 58 | +// Parameters: |
| 59 | +// |
| 60 | +// _hresult - [in] Value to check |
| 61 | +// label - [in] label to jump if condition is met |
| 62 | +// |
| 63 | +#define IF_FAILED_JUMP(_hresult, label) \ |
| 64 | + if (FAILED(_hresult)) \ |
| 65 | + { \ |
| 66 | + goto label; \ |
| 67 | + } |
| 68 | + |
| 69 | +//------------------------------------------------------------------------- |
| 70 | +// Description: |
| 71 | +// |
| 72 | +// If the hresult passed SUCCEEDED, jump to the given label. |
| 73 | +// |
| 74 | +// Parameters: |
| 75 | +// |
| 76 | +// _hresult - [in] Value to check |
| 77 | +// label - [in] label to jump if condition is met |
| 78 | +// |
| 79 | +#define IF_SUCCEEDED_JUMP(_hresult, label) \ |
| 80 | + if (SUCCEEDED(_hresult)) \ |
| 81 | + { \ |
| 82 | + goto label; \ |
| 83 | + } |
| 84 | + |
| 85 | +//------------------------------------------------------------------------- |
| 86 | +// Description: |
| 87 | +// |
| 88 | +// If the condition evaluates to TRUE, perform the given statement |
| 89 | +// then jump to the given label. |
| 90 | +// |
| 91 | +// Parameters: |
| 92 | +// |
| 93 | +// condition - [in] Code that fits in if statement |
| 94 | +// action - [in] action to perform in body of if statement |
| 95 | +// label - [in] label to jump if condition is met |
| 96 | +// |
| 97 | +#define IF_TRUE_ACTION_JUMP(condition, action, label) \ |
| 98 | + if (condition) \ |
| 99 | + { \ |
| 100 | + action; \ |
| 101 | + goto label; \ |
| 102 | + } |
| 103 | + |
| 104 | +//------------------------------------------------------------------------- |
| 105 | +// Description: |
| 106 | +// |
| 107 | +// If the hresult FAILED, perform the given statement then jump to |
| 108 | +// the given label. |
| 109 | +// |
| 110 | +// Parameters: |
| 111 | +// |
| 112 | +// _hresult - [in] Value to check |
| 113 | +// action - [in] action to perform in body of if statement |
| 114 | +// label - [in] label to jump if condition is met |
| 115 | +// |
| 116 | +#define IF_FAILED_ACTION_JUMP(_hresult, action, label) \ |
| 117 | + if (FAILED(_hresult)) \ |
| 118 | + { \ |
| 119 | + action; \ |
| 120 | + goto label; \ |
| 121 | + } |
| 122 | + |
| 123 | +//------------------------------------------------------------------------- |
| 124 | +// Description: |
| 125 | +// |
| 126 | +// Closes a handle and assigns NULL. |
| 127 | +// |
| 128 | +// Parameters: |
| 129 | +// |
| 130 | +// h - [in] handle to close |
| 131 | +// |
| 132 | +#define SAFE_CLOSE_HANDLE(h) \ |
| 133 | + if (NULL != h) \ |
| 134 | + { \ |
| 135 | + CloseHandle(h); \ |
| 136 | + h = NULL; \ |
| 137 | + } |
| 138 | + |
| 139 | +//------------------------------------------------------------------------- |
| 140 | +// Description: |
| 141 | +// |
| 142 | +// Addref an interface pointer |
| 143 | +// |
| 144 | +// Parameters: |
| 145 | +// |
| 146 | +// p - [in] object to addref |
| 147 | +// |
| 148 | +#define SAFE_ADDREF(p) \ |
| 149 | + if (NULL != p) \ |
| 150 | + { \ |
| 151 | + (p)->AddRef();; \ |
| 152 | + } |
| 153 | + |
| 154 | +//------------------------------------------------------------------------- |
| 155 | +// Description: |
| 156 | +// |
| 157 | +// Releases an interface pointer and assigns NULL. |
| 158 | +// |
| 159 | +// Parameters: |
| 160 | +// |
| 161 | +// p - [in] object to release |
| 162 | +// |
| 163 | +#define SAFE_RELEASE(p) \ |
| 164 | + if (NULL != p) \ |
| 165 | + { \ |
| 166 | + (p)->Release(); \ |
| 167 | + (p) = NULL; \ |
| 168 | + } |
| 169 | + |
| 170 | +//------------------------------------------------------------------------- |
| 171 | +// Description: |
| 172 | +// |
| 173 | +// Deletes a pointer and assigns NULL. Do not check for NULL because |
| 174 | +// the default delete operator checks for it. |
| 175 | +// |
| 176 | +// Parameters: |
| 177 | +// |
| 178 | +// p - [in] object to delete |
| 179 | +// |
| 180 | +#define SAFE_DELETE(p) \ |
| 181 | + delete p; \ |
| 182 | + p = NULL; |
| 183 | + |
| 184 | +//------------------------------------------------------------------------- |
| 185 | +// Description: |
| 186 | +// |
| 187 | +// Deletes an array pointer and assigns NULL. Do not check for NULL because |
| 188 | +// the default delete operator checks for it. |
| 189 | +// |
| 190 | +// Parameters: |
| 191 | +// |
| 192 | +// p - [in] Array to delete |
| 193 | +// |
| 194 | +#define SAFE_DELETE_ARRAY(p) \ |
| 195 | + delete [] p; \ |
| 196 | + p = NULL; |
| 197 | + |
| 198 | +//------------------------------------------------------------------------- |
| 199 | +// Description: |
| 200 | +// |
| 201 | +// Frees a block of memory allocated by CoTaskMemAlloc and assigns NULL to |
| 202 | +// the pointer |
| 203 | +// |
| 204 | +// Parameters: |
| 205 | +// |
| 206 | +// p - [in] Pointer to memory to free |
| 207 | +// |
| 208 | +#define SAFE_COTASKMEMFREE(p) \ |
| 209 | + if (NULL != p) \ |
| 210 | + { \ |
| 211 | + CoTaskMemFree(p); \ |
| 212 | + (p) = NULL; \ |
| 213 | + } |
| 214 | + |
| 215 | +//------------------------------------------------------------------------- |
| 216 | +// Description: |
| 217 | +// |
| 218 | +// Frees a DLL loaded with LoadLibrary and assigns NULL to the handle |
| 219 | +// |
| 220 | +// Parameters: |
| 221 | +// |
| 222 | +// h - [in] Handle to DLL to free |
| 223 | +// |
| 224 | +#define SAFE_FREELIBRARY(h) \ |
| 225 | + if (NULL != h) \ |
| 226 | + { \ |
| 227 | + FreeLibrary(h); \ |
| 228 | + (h) = NULL; \ |
| 229 | + } |
| 230 | + |
| 231 | +//------------------------------------------------------------------------- |
| 232 | +// Description: |
| 233 | +// |
| 234 | +// Used to validate a read pointer |
| 235 | +// |
| 236 | +// Parameters: |
| 237 | +// |
| 238 | +// p - [in] read pointer. |
| 239 | +// s - [in] size of memory in bytes pointed to by p. |
| 240 | +// |
| 241 | +#define IS_VALID_READ_POINTER(p, s) ((NULL != p) || (0 == s)) |
| 242 | + |
| 243 | +//------------------------------------------------------------------------- |
| 244 | +// Description: |
| 245 | +// |
| 246 | +// Used to validate a write pointer |
| 247 | +// |
| 248 | +// Parameters: |
| 249 | +// |
| 250 | +// p - [in] write pointer. |
| 251 | +// s - [in] size of memory in bytes pointed to by p. |
| 252 | +// |
| 253 | +#define IS_VALID_WRITE_POINTER(p, s) ((NULL != p) || (0 == s)) |
| 254 | + |
| 255 | +//------------------------------------------------------------------------- |
| 256 | +// Description: |
| 257 | +// |
| 258 | +// Used to validate a read pointer of a particular type |
| 259 | +// |
| 260 | +// Parameters: |
| 261 | +// |
| 262 | +// p - [in] typed read pointer |
| 263 | +// |
| 264 | +#define IS_VALID_TYPED_READ_POINTER(p) IS_VALID_READ_POINTER((p), sizeof *(p)) |
| 265 | + |
| 266 | +//------------------------------------------------------------------------- |
| 267 | +// Description: |
| 268 | +// |
| 269 | +// Used to validate a write pointer of a particular type |
| 270 | +// |
| 271 | +// Parameters: |
| 272 | +// |
| 273 | +// p - [in] typed write pointer |
| 274 | +// |
| 275 | +#define IS_VALID_TYPED_WRITE_POINTER(p) IS_VALID_WRITE_POINTER((p), sizeof *(p)) |
| 276 | + |
| 277 | +// --------------------------------------------------------------------------- |
| 278 | +// Macros that wrap windows messages. Similar to those in windowsX.h and |
| 279 | +// commctrl.h |
| 280 | +// |
| 281 | +#if !defined Static_SetIcon |
| 282 | +#define Static_SetIcon(hwnd, hi) \ |
| 283 | + (BOOL)SNDMSG((hwnd), STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)(hi)) |
| 284 | +#endif |
| 285 | + |
| 286 | +#define TrackBar_SetTickFrequency(hwnd, f) \ |
| 287 | + (BOOL)SNDMSG((hwnd), TBM_SETTICFREQ, (WPARAM)(f), 0) |
| 288 | + |
| 289 | +#define TrackBar_SetBuddy(hwnd, f, hbud) \ |
| 290 | + (HWND)SNDMSG((hwnd), TBM_SETBUDDY, (WPARAM)(f), (LPARAM)hbud) |
| 291 | + |
| 292 | +#define TrackBar_GetPos(hwnd) \ |
| 293 | + (int)SNDMSG((hwnd), TBM_GETPOS, 0, 0) |
| 294 | + |
| 295 | +#define TrackBar_SetPos(hwnd, pos) \ |
| 296 | + SNDMSG((hwnd), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pos) |
| 297 | + |
| 298 | +#define TrackBar_SetRange(hwnd, min, max) \ |
| 299 | + SNDMSG((hwnd), TBM_SETRANGE , (WPARAM)TRUE, (LPARAM) MAKELONG(min, max)) |
| 300 | + |
| 301 | +#define TrackBar_SetThumbLength(hwnd, l) \ |
| 302 | + SNDMSG((hwnd), TBM_SETTHUMBLENGTH, (WPARAM)l, 0); |
| 303 | + |
| 304 | +#define TrackBar_SetPageSize(hwnd, n) \ |
| 305 | + SNDMSG((hwnd), TBM_SETPAGESIZE, 0, (LPARAM)n) |
| 306 | + |
| 307 | +#define Window_GetFont(hwnd) \ |
| 308 | + (HFONT)SNDMSG((hwnd), WM_GETFONT, 0, 0) |
| 309 | + |
| 310 | +#define Window_SetFont(hwnd, font) \ |
| 311 | + SNDMSG((hwnd), WM_SETFONT, (WPARAM)font, FALSE) |
| 312 | + |
| 313 | + |
| 314 | +// ---------------------------------------------------------------------- |
| 315 | +// A struct for holding a rect in easier terms than a RECT struct |
| 316 | +// |
| 317 | +struct SRECT |
| 318 | +{ |
| 319 | + int x, y, w, h; |
| 320 | + SRECT() |
| 321 | + { |
| 322 | + x = y = w = h = 0; |
| 323 | + } |
| 324 | + SRECT(int X, int Y, int W, int H) |
| 325 | + { |
| 326 | + x = X; y = Y; w = W; h = H; |
| 327 | + } |
| 328 | + SRECT(RECT* prc) |
| 329 | + { |
| 330 | + x = prc->left; |
| 331 | + y = prc->top; |
| 332 | + w = prc->right - prc->left; |
| 333 | + h = prc->bottom - prc->top; |
| 334 | + } |
| 335 | +}; |
| 336 | + |
| 337 | +#define HNS_PER_SECOND (10ull * 1000ull * 1000ull) |
0 commit comments