Blame libusb/os/windows_common.h

Packit Service b0a153
/*
Packit Service b0a153
 * Windows backend common header for libusb 1.0
Packit Service b0a153
 *
Packit Service b0a153
 * This file brings together header code common between
Packit Service b0a153
 * the desktop Windows and Windows CE backends.
Packit Service b0a153
 * Copyright © 2012-2013 RealVNC Ltd.
Packit Service b0a153
 * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
Packit Service b0a153
 * With contributions from Michael Plante, Orin Eman et al.
Packit Service b0a153
 * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
Packit Service b0a153
 * Major code testing contribution by Xiaofan Chen
Packit Service b0a153
 *
Packit Service b0a153
 * This library is free software; you can redistribute it and/or
Packit Service b0a153
 * modify it under the terms of the GNU Lesser General Public
Packit Service b0a153
 * License as published by the Free Software Foundation; either
Packit Service b0a153
 * version 2.1 of the License, or (at your option) any later version.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is distributed in the hope that it will be useful,
Packit Service b0a153
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b0a153
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b0a153
 * Lesser General Public License for more details.
Packit Service b0a153
 *
Packit Service b0a153
 * You should have received a copy of the GNU Lesser General Public
Packit Service b0a153
 * License along with this library; if not, write to the Free Software
Packit Service b0a153
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
#pragma once
Packit Service b0a153
Packit Service b0a153
// Windows API default is uppercase - ugh!
Packit Service b0a153
#if !defined(bool)
Packit Service b0a153
#define bool BOOL
Packit Service b0a153
#endif
Packit Service b0a153
#if !defined(true)
Packit Service b0a153
#define true TRUE
Packit Service b0a153
#endif
Packit Service b0a153
#if !defined(false)
Packit Service b0a153
#define false FALSE
Packit Service b0a153
#endif
Packit Service b0a153
Packit Service b0a153
#define EPOCH_TIME	UINT64_C(116444736000000000)	// 1970.01.01 00:00:000 in MS Filetime
Packit Service b0a153
Packit Service b0a153
#if defined(__CYGWIN__ )
Packit Service b0a153
#define _stricmp strcasecmp
Packit Service b0a153
#define _strdup strdup
Packit Service b0a153
// _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread
Packit Service b0a153
#define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, (LPDWORD)f)
Packit Service b0a153
#endif
Packit Service b0a153
Packit Service b0a153
#define safe_free(p) do {if (p != NULL) {free((void *)p); p = NULL;}} while (0)
Packit Service b0a153
Packit Service b0a153
#ifndef ARRAYSIZE
Packit Service b0a153
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
Packit Service b0a153
#endif
Packit Service b0a153
Packit Service b0a153
#define ERR_BUFFER_SIZE	256
Packit Service b0a153
Packit Service b0a153
/*
Packit Service b0a153
 * API macros - leveraged from libusb-win32 1.x
Packit Service b0a153
 */
Packit Service b0a153
#ifndef _WIN32_WCE
Packit Service b0a153
#define DLL_STRINGIFY(s) #s
Packit Service b0a153
#define DLL_LOAD_LIBRARY(name) LoadLibraryA(DLL_STRINGIFY(name))
Packit Service b0a153
#else
Packit Service b0a153
#define DLL_STRINGIFY(s) L#s
Packit Service b0a153
#define DLL_LOAD_LIBRARY(name) LoadLibrary(DLL_STRINGIFY(name))
Packit Service b0a153
#endif
Packit Service b0a153
Packit Service b0a153
/*
Packit Service b0a153
 * Macros for handling DLL themselves
Packit Service b0a153
 */
Packit Service b0a153
#define DLL_HANDLE_NAME(name) __dll_##name##_handle
Packit Service b0a153
Packit Service b0a153
#define DLL_DECLARE_HANDLE(name)				\
Packit Service b0a153
	static HMODULE DLL_HANDLE_NAME(name) = NULL
Packit Service b0a153
Packit Service b0a153
#define DLL_GET_HANDLE(name)					\
Packit Service b0a153
	do {							\
Packit Service b0a153
		DLL_HANDLE_NAME(name) = DLL_LOAD_LIBRARY(name);	\
Packit Service b0a153
		if (!DLL_HANDLE_NAME(name))			\
Packit Service b0a153
			return FALSE;				\
Packit Service b0a153
	} while (0)
Packit Service b0a153
Packit Service b0a153
#define DLL_FREE_HANDLE(name)					\
Packit Service b0a153
	do {							\
Packit Service b0a153
		if (DLL_HANDLE_NAME(name)) {			\
Packit Service b0a153
			FreeLibrary(DLL_HANDLE_NAME(name));	\
Packit Service b0a153
			DLL_HANDLE_NAME(name) = NULL;		\
Packit Service b0a153
		}						\
Packit Service b0a153
	} while (0)
Packit Service b0a153
Packit Service b0a153
Packit Service b0a153
/*
Packit Service b0a153
 * Macros for handling functions within a DLL
Packit Service b0a153
 */
Packit Service b0a153
#define DLL_FUNC_NAME(name) __dll_##name##_func_t
Packit Service b0a153
Packit Service b0a153
#define DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefixname, name, args)	\
Packit Service b0a153
	typedef ret (api * DLL_FUNC_NAME(name))args;			\
Packit Service b0a153
	static DLL_FUNC_NAME(name) prefixname = NULL
Packit Service b0a153
Packit Service b0a153
#define DLL_DECLARE_FUNC(api, ret, name, args)				\
Packit Service b0a153
	DLL_DECLARE_FUNC_PREFIXNAME(api, ret, name, name, args)
Packit Service b0a153
#define DLL_DECLARE_FUNC_PREFIXED(api, ret, prefix, name, args)		\
Packit Service b0a153
	DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefix##name, name, args)
Packit Service b0a153
Packit Service b0a153
#define DLL_LOAD_FUNC_PREFIXNAME(dll, prefixname, name, ret_on_failure)	\
Packit Service b0a153
	do {								\
Packit Service b0a153
		HMODULE h = DLL_HANDLE_NAME(dll);			\
Packit Service b0a153
		prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h,	\
Packit Service b0a153
				DLL_STRINGIFY(name));			\
Packit Service b0a153
		if (prefixname)						\
Packit Service b0a153
			break;						\
Packit Service b0a153
		prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h,	\
Packit Service b0a153
				DLL_STRINGIFY(name) DLL_STRINGIFY(A));	\
Packit Service b0a153
		if (prefixname)						\
Packit Service b0a153
			break;						\
Packit Service b0a153
		prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h,	\
Packit Service b0a153
				DLL_STRINGIFY(name) DLL_STRINGIFY(W));	\
Packit Service b0a153
		if (prefixname)						\
Packit Service b0a153
			break;						\
Packit Service b0a153
		if (ret_on_failure)					\
Packit Service b0a153
			return FALSE;					\
Packit Service b0a153
	} while (0)
Packit Service b0a153
Packit Service b0a153
#define DLL_LOAD_FUNC(dll, name, ret_on_failure)			\
Packit Service b0a153
	DLL_LOAD_FUNC_PREFIXNAME(dll, name, name, ret_on_failure)
Packit Service b0a153
#define DLL_LOAD_FUNC_PREFIXED(dll, prefix, name, ret_on_failure)	\
Packit Service b0a153
	DLL_LOAD_FUNC_PREFIXNAME(dll, prefix##name, name, ret_on_failure)