windows_common.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Windows backend common header for libusbx 1.0
  3. *
  4. * This file brings together header code common between
  5. * the desktop Windows and Windows CE backends.
  6. * Copyright © 2012-2013 RealVNC Ltd.
  7. * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
  8. * With contributions from Michael Plante, Orin Eman et al.
  9. * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
  10. * Major code testing contribution by Xiaofan Chen
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #pragma once
  27. // Windows API default is uppercase - ugh!
  28. #if !defined(bool)
  29. #define bool BOOL
  30. #endif
  31. #if !defined(true)
  32. #define true TRUE
  33. #endif
  34. #if !defined(false)
  35. #define false FALSE
  36. #endif
  37. #define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0)
  38. #define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0)
  39. #define safe_min(a, b) min((size_t)(a), (size_t)(b))
  40. #define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \
  41. ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0)
  42. #define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1)
  43. #define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1))
  44. #define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1)
  45. #define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
  46. #define safe_stricmp(str1, str2) _stricmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
  47. #define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2), count)
  48. #define safe_strlen(str) ((str==NULL)?0:strlen(str))
  49. #define safe_sprintf(dst, count, ...) do {_snprintf(dst, count, __VA_ARGS__); (dst)[(count)-1] = 0; } while(0)
  50. #define safe_stprintf _sntprintf
  51. #define safe_tcslen(str) ((str==NULL)?0:_tcslen(str))
  52. #define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0)
  53. #define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL)
  54. #ifndef ARRAYSIZE
  55. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  56. #endif
  57. #define ERR_BUFFER_SIZE 256
  58. #define TIMER_REQUEST_RETRY_MS 100
  59. #define MAX_TIMER_SEMAPHORES 128
  60. /*
  61. * API macros - from libusb-win32 1.x
  62. */
  63. #define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \
  64. typedef ret (api * __dll_##name##_t)args; \
  65. static __dll_##name##_t prefixname = NULL
  66. #ifndef _WIN32_WCE
  67. #define DLL_STRINGIFY(dll) #dll
  68. #define DLL_GET_MODULE_HANDLE(dll) GetModuleHandleA(DLL_STRINGIFY(dll))
  69. #define DLL_LOAD_LIBRARY(dll) LoadLibraryA(DLL_STRINGIFY(dll))
  70. #else
  71. #define DLL_STRINGIFY(dll) L#dll
  72. #define DLL_GET_MODULE_HANDLE(dll) GetModuleHandle(DLL_STRINGIFY(dll))
  73. #define DLL_LOAD_LIBRARY(dll) LoadLibrary(DLL_STRINGIFY(dll))
  74. #endif
  75. #define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
  76. do { \
  77. HMODULE h = DLL_GET_MODULE_HANDLE(dll); \
  78. if (!h) \
  79. h = DLL_LOAD_LIBRARY(dll); \
  80. if (!h) { \
  81. if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; } \
  82. else { break; } \
  83. } \
  84. prefixname = (__dll_##name##_t)GetProcAddress(h, \
  85. DLL_STRINGIFY(name)); \
  86. if (prefixname) break; \
  87. prefixname = (__dll_##name##_t)GetProcAddress(h, \
  88. DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \
  89. if (prefixname) break; \
  90. prefixname = (__dll_##name##_t)GetProcAddress(h, \
  91. DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \
  92. if (prefixname) break; \
  93. if(ret_on_failure) \
  94. return LIBUSB_ERROR_NOT_FOUND; \
  95. } while(0)
  96. #define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args)
  97. #define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, name, name, ret_on_failure)
  98. #define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) DLL_DECLARE_PREFIXNAME(api, ret, prefix##name, name, args)
  99. #define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, prefix##name, name, ret_on_failure)