xref: /aoo42x/main/sal/systools/win32/uwinapi/macros.h (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #define _UWINAPI_
29*cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _INC_MALLOC
32*cdf0e10cSrcweir #	include <malloc.h>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER >= 1400)
36*cdf0e10cSrcweir #pragma warning(disable:4740)
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #ifndef _INC_TCHAR
40*cdf0e10cSrcweir #	ifdef UNICODE
41*cdf0e10cSrcweir #		define _UNICODE
42*cdf0e10cSrcweir #	endif
43*cdf0e10cSrcweir #	include <TCHAR.H>
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // Globally disable "warning C4100: unreferenced formal parameter" caused by
47*cdf0e10cSrcweir // IMPLEMENT_THUNK:
48*cdf0e10cSrcweir #ifdef _MSC_VER
49*cdf0e10cSrcweir #pragma warning(disable:4100)
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir /* Version macros */
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #define MAKE_VER_WIN32( major, minor, build, isWindows ) \
55*cdf0e10cSrcweir ((DWORD)MAKELONG( MAKEWORD( major, minor ), (build) | ( isWindows ? 0x8000 : 0 ) ))
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #define MAKE_VER_WIN32_NT( major, minor, build ) \
58*cdf0e10cSrcweir 	MAKE_VER_WIN32( major, minor, build, FALSE )
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define MAKE_VER_WIN32_WINDOWS( major, minor, build ) \
61*cdf0e10cSrcweir 	MAKE_VER_WIN32( major, minor, build, TRUE )
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir #define VER_WIN32_WINDOWS_95	MAKE_VER_WIN32_WINDOWS( 4, 0, 0 )
64*cdf0e10cSrcweir #define VER_WIN32_WINDOWS_98	MAKE_VER_WIN32_WINDOWS( 4, 10, 0 )
65*cdf0e10cSrcweir #define VER_WIN32_WINDOWS_ME	MAKE_VER_WIN32_WINDOWS( 4, 90, 0 )
66*cdf0e10cSrcweir #define VER_WIN32_NT_NT4		MAKE_VER_WIN32_NT( 4, 0, 0 )
67*cdf0e10cSrcweir #define VER_WIN32_NT_2000		MAKE_VER_WIN32_NT( 5, 0, 0 )
68*cdf0e10cSrcweir #define VER_WIN32_NT_XP			MAKE_VER_WIN32_NT( 5, 1, 0 )
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir #ifdef __cplusplus
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir #define _AUTO_WSTR2STR( lpStrA, lpStrW ) \
74*cdf0e10cSrcweir LPSTR	lpStrA; \
75*cdf0e10cSrcweir if ( lpStrW ) \
76*cdf0e10cSrcweir { \
77*cdf0e10cSrcweir 	int	cNeeded = WideCharToMultiByte( CP_ACP, 0, lpStrW, -1, NULL, 0, NULL, NULL ); \
78*cdf0e10cSrcweir 	lpStrA = (LPSTR)_alloca( cNeeded * sizeof(CHAR) ); \
79*cdf0e10cSrcweir 	WideCharToMultiByte( CP_ACP, 0, lpStrW, -1, lpStrA, cNeeded, NULL, NULL ); \
80*cdf0e10cSrcweir } \
81*cdf0e10cSrcweir else \
82*cdf0e10cSrcweir 	lpStrA = NULL;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir #define AUTO_WSTR2STR( lpStr ) \
86*cdf0e10cSrcweir 	_AUTO_WSTR2STR( lpStr##A, lpStr##W )
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir #define AUTO_STR( lpStr, cchBuffer ) \
89*cdf0e10cSrcweir LPSTR	lpStr##A = lpStr##W ? (LPSTR)_alloca( (cchBuffer) * sizeof(CHAR) ) : NULL;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir #endif	/* __cplusplus */
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir #define STRBUF2WSTR( lpStr, cchSrcBuffer, cchDestBuffer ) \
95*cdf0e10cSrcweir 	MultiByteToWideChar( CP_ACP, 0, lpStr##A, cchSrcBuffer, lpStr##W, (int) cchDestBuffer )
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir #define STR2WSTR( lpStr, cchBuffer ) \
98*cdf0e10cSrcweir 	STRBUF2WSTR( lpStr, -1, cchBuffer )
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir #define WSTR2STR( lpStr, cchBuffer ) \
101*cdf0e10cSrcweir 	WideCharToMultiByte( CP_ACP, 0, lpStr##W, -1, lpStr##A, cchBuffer, NULL, NULL )
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir EXTERN_C void WINAPI ResolveThunk_WINDOWS( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
104*cdf0e10cSrcweir EXTERN_C void WINAPI ResolveThunk_TRYLOAD( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
105*cdf0e10cSrcweir EXTERN_C void WINAPI ResolveThunk_ALLWAYS( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir #ifdef __MINGW32__
111*cdf0e10cSrcweir #define IMPLEMENT_THUNK( module, resolve, rettype, calltype, func, params ) \
112*cdf0e10cSrcweir static void func##_Thunk(); \
113*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
114*cdf0e10cSrcweir EXTERN_C rettype calltype func params \
115*cdf0e10cSrcweir { \
116*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
117*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
118*cdf0e10cSrcweir } \
119*cdf0e10cSrcweir EXTERN_C rettype calltype func##_##resolve params; \
120*cdf0e10cSrcweir static rettype calltype func##_##Failure params; \
121*cdf0e10cSrcweir static void func##_Thunk() \
122*cdf0e10cSrcweir { \
123*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func, (FARPROC)func##_##resolve, (FARPROC)func##_##Failure ); \
124*cdf0e10cSrcweir 	asm("	movl	%ebp, %esp"); \
125*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
126*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
127*cdf0e10cSrcweir } \
128*cdf0e10cSrcweir static rettype calltype func##_##Failure params \
129*cdf0e10cSrcweir { \
130*cdf0e10cSrcweir 	SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); \
131*cdf0e10cSrcweir 	return (rettype)0; \
132*cdf0e10cSrcweir } \
133*cdf0e10cSrcweir EXTERN_C rettype calltype func##_##resolve params
134*cdf0e10cSrcweir #else
135*cdf0e10cSrcweir #define IMPLEMENT_THUNK( module, resolve, rettype, calltype, func, params ) \
136*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr; \
137*cdf0e10cSrcweir EXTERN_C rettype calltype func##_##resolve params; \
138*cdf0e10cSrcweir static rettype calltype func##_##Failure params; \
139*cdf0e10cSrcweir static _declspec ( naked ) void func##_Thunk() \
140*cdf0e10cSrcweir { \
141*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func, (FARPROC)func##_##resolve, (FARPROC)func##_##Failure ); \
142*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
143*cdf0e10cSrcweir } \
144*cdf0e10cSrcweir EXTERN_C _declspec( naked ) rettype calltype func params \
145*cdf0e10cSrcweir { \
146*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
147*cdf0e10cSrcweir } \
148*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
149*cdf0e10cSrcweir static rettype calltype func##_##Failure params \
150*cdf0e10cSrcweir { \
151*cdf0e10cSrcweir 	SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); \
152*cdf0e10cSrcweir 	return (rettype)0; \
153*cdf0e10cSrcweir } \
154*cdf0e10cSrcweir EXTERN_C rettype calltype func##_##resolve params
155*cdf0e10cSrcweir #endif
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir #ifdef __MINGW32__
160*cdf0e10cSrcweir #define DEFINE_CUSTOM_THUNK( module, resolve, rettype, calltype, func, params ) \
161*cdf0e10cSrcweir static void func##_Thunk(); \
162*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
163*cdf0e10cSrcweir static void func##_Thunk() \
164*cdf0e10cSrcweir { \
165*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func ); \
166*cdf0e10cSrcweir 	asm("	movl	%ebp, %esp"); \
167*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
168*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
169*cdf0e10cSrcweir } \
170*cdf0e10cSrcweir EXTERN_C rettype calltype func params \
171*cdf0e10cSrcweir { \
172*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
173*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir #else
176*cdf0e10cSrcweir #define DEFINE_CUSTOM_THUNK( module, resolve, rettype, calltype, func, params ) \
177*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr; \
178*cdf0e10cSrcweir static _declspec ( naked ) void func##_Thunk() \
179*cdf0e10cSrcweir { \
180*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func ); \
181*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
182*cdf0e10cSrcweir } \
183*cdf0e10cSrcweir EXTERN_C _declspec( naked ) rettype calltype func params \
184*cdf0e10cSrcweir { \
185*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
186*cdf0e10cSrcweir } \
187*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk;
188*cdf0e10cSrcweir #endif
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir #ifdef __MINGW32__
192*cdf0e10cSrcweir #define DEFINE_DEFAULT_THUNK( module, resolve, rettype, calltype, func, params ) \
193*cdf0e10cSrcweir static void func##_Thunk(); \
194*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
195*cdf0e10cSrcweir static rettype calltype func##_##Failure params; \
196*cdf0e10cSrcweir static _declspec ( naked ) void func##_Thunk() \
197*cdf0e10cSrcweir { \
198*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func, NULL, (FARPROC)func##_##Failure ); \
199*cdf0e10cSrcweir 	asm("	movl	%ebp, %esp"); \
200*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
201*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
202*cdf0e10cSrcweir } \
203*cdf0e10cSrcweir EXTERN_C _declspec( naked ) rettype calltype func params \
204*cdf0e10cSrcweir { \
205*cdf0e10cSrcweir 	asm("	popl	%ebp"); \
206*cdf0e10cSrcweir 	asm("	jmp	*(%0)"::"m"(module##_##func##_Ptr)); \
207*cdf0e10cSrcweir } \
208*cdf0e10cSrcweir static rettype calltype func##_##Failure params \
209*cdf0e10cSrcweir { \
210*cdf0e10cSrcweir 	SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); \
211*cdf0e10cSrcweir 	return (rettype)0; \
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir #else
214*cdf0e10cSrcweir #define DEFINE_DEFAULT_THUNK( module, resolve, rettype, calltype, func, params ) \
215*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr; \
216*cdf0e10cSrcweir static rettype calltype func##_##Failure params; \
217*cdf0e10cSrcweir static _declspec ( naked ) void func##_Thunk() \
218*cdf0e10cSrcweir { \
219*cdf0e10cSrcweir 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func, NULL, (FARPROC)func##_##Failure ); \
220*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
221*cdf0e10cSrcweir } \
222*cdf0e10cSrcweir EXTERN_C _declspec( naked ) rettype calltype func params \
223*cdf0e10cSrcweir { \
224*cdf0e10cSrcweir 	_asm	jmp	[module##_##func##_Ptr] \
225*cdf0e10cSrcweir } \
226*cdf0e10cSrcweir EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
227*cdf0e10cSrcweir static rettype calltype func##_##Failure params \
228*cdf0e10cSrcweir { \
229*cdf0e10cSrcweir 	SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); \
230*cdf0e10cSrcweir 	return (rettype)0; \
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir #endif
233