2
* Windows backend common header for libusbx 1.0
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
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.
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.
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
29
// Windows API default is uppercase - ugh!
40
#define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0)
41
#define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0)
42
#define safe_min(a, b) min((size_t)(a), (size_t)(b))
43
#define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \
44
((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0)
45
#define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1)
46
#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1))
47
#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1)
48
#define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
49
#define safe_stricmp(str1, str2) _stricmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
50
#define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2), count)
51
#define safe_strlen(str) ((str==NULL)?0:strlen(str))
52
#define safe_sprintf(dst, count, ...) do {_snprintf(dst, count, __VA_ARGS__); (dst)[(count)-1] = 0; } while(0)
53
#define safe_stprintf _sntprintf
54
#define safe_tcslen(str) ((str==NULL)?0:_tcslen(str))
55
#define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0)
56
#define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL)
58
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
61
#define ERR_BUFFER_SIZE 256
62
#define TIMER_REQUEST_RETRY_MS 100
63
#define MAX_TIMER_SEMAPHORES 128
67
* API macros - from libusb-win32 1.x
69
#define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \
70
typedef ret (api * __dll_##name##_t)args; \
71
static __dll_##name##_t prefixname = NULL
74
#define DLL_STRINGIFY(dll) #dll
75
#define DLL_GET_MODULE_HANDLE(dll) GetModuleHandleA(DLL_STRINGIFY(dll))
76
#define DLL_LOAD_LIBRARY(dll) LoadLibraryA(DLL_STRINGIFY(dll))
78
#define DLL_STRINGIFY(dll) L#dll
79
#define DLL_GET_MODULE_HANDLE(dll) GetModuleHandle(DLL_STRINGIFY(dll))
80
#define DLL_LOAD_LIBRARY(dll) LoadLibrary(DLL_STRINGIFY(dll))
83
#define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
85
HMODULE h = DLL_GET_MODULE_HANDLE(dll); \
87
h = DLL_LOAD_LIBRARY(dll); \
89
if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; } \
92
prefixname = (__dll_##name##_t)GetProcAddress(h, \
93
DLL_STRINGIFY(name)); \
94
if (prefixname) break; \
95
prefixname = (__dll_##name##_t)GetProcAddress(h, \
96
DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \
97
if (prefixname) break; \
98
prefixname = (__dll_##name##_t)GetProcAddress(h, \
99
DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \
100
if (prefixname) break; \
102
return LIBUSB_ERROR_NOT_FOUND; \
105
#define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args)
106
#define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, name, name, ret_on_failure)
107
#define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) DLL_DECLARE_PREFIXNAME(api, ret, prefix##name, name, args)
108
#define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, prefix##name, name, ret_on_failure)