1 #pragma once
2 // *************************************************************
3 //
4 //  Licensed to the Apache Software Foundation (ASF) under one
5 //  or more contributor license agreements.  See the NOTICE file
6 //  distributed with this work for additional information
7 //  regarding copyright ownership.  The ASF licenses this file
8 //  to you under the Apache License, Version 2.0 (the
9 //  "License"); you may not use this file except in compliance
10 //  with the License.  You may obtain a copy of the License at
11 //
12 //    http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //  Unless required by applicable law or agreed to in writing,
15 //  software distributed under the License is distributed on an
16 //  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 //  KIND, either express or implied.  See the License for the
18 //  specific language governing permissions and limitations
19 //  under the License.
20 //
21 // *************************************************************
22 
23 #ifdef _UWINAPI_
24 #define _KERNEL32_
25 #define _USER32_
26 #define _SHELL32_
27 #endif
28 
29 #include <windows.h>
30 #include <malloc.h>
31 
32 #ifndef _UWINAPI_
33 EXTERN_C WINBASEAPI DWORD UWINAPI_dwFakedVersion;
34 #endif
35 
36 EXTERN_C WINBASEAPI DWORD SetVersion( DWORD dwVersion );
37 
38 /* Version macros */
39 
40 #define MAKE_VER_WIN32( major, minor, build, isWindows ) \
41 ((DWORD)MAKELONG( MAKEWORD( major, minor ), (build) | ( isWindows ? 0x8000 : 0 ) ))
42 
43 #define MAKE_VER_WIN32_NT( major, minor, build ) \
44 	MAKE_VER_WIN32( major, minor, build, FALSE )
45 
46 #define MAKE_VER_WIN32_WINDOWS( major, minor, build ) \
47 	MAKE_VER_WIN32( major, minor, build, TRUE )
48 
49 #define VER_WIN32_WINDOWS_95	MAKE_VER_WIN32_WINDOWS( 4, 0, 0 )
50 #define VER_WIN32_WINDOWS_98	MAKE_VER_WIN32_WINDOWS( 4, 10, 0 )
51 #define VER_WIN32_WINDOWS_ME	MAKE_VER_WIN32_WINDOWS( 4, 90, 0 )
52 #define VER_WIN32_NT_NT4		MAKE_VER_WIN32_NT( 4, 0, 0 )
53 #define VER_WIN32_NT_2000		MAKE_VER_WIN32_NT( 5, 0, 0 )
54 #define VER_WIN32_NT_XP			MAKE_VER_WIN32_NT( 5, 1, 0 )
55 
56 
57 EXTERN_C WINBASEAPI LPSTR WINAPI lstrchrA( LPCSTR lpString, CHAR c );
58 EXTERN_C WINBASEAPI LPWSTR WINAPI lstrchrW( LPCWSTR lpString, WCHAR c );
59 EXTERN_C WINBASEAPI LPSTR WINAPI lstrrchrA( LPCSTR lpString, CHAR c );
60 EXTERN_C WINBASEAPI LPWSTR WINAPI lstrrchrW( LPCWSTR lpString, WCHAR c );
61 
62 #ifdef UNICODE
63 #define lstrrchr	lstrrchrW
64 #define lstrchr		lstrchrW
65 #else
66 #define lstrrchr	lstrrchrA
67 #define lstrchr		lstrchrA
68 #endif
69 
70 // macro that calculates the count of elements of a static array
71 
72 #define bufsizeof(buf)	(sizeof(buf) / sizeof((buf)[0]))
73 
74 
75 #define IsValidHandle(Handle)	((DWORD)(Handle) + 1 > 1)
76 
77 #ifdef __cplusplus
78 
79 #define _AUTO_WSTR2STR( lpStrA, lpStrW ) \
80 LPSTR	lpStrA; \
81 if ( lpStrW ) \
82 { \
83 	int	cNeeded = WideCharToMultiByte( CP_ACP, 0, lpStrW, -1, NULL, 0, NULL, NULL ); \
84 	lpStrA = (LPSTR)_alloca( cNeeded * sizeof(CHAR) ); \
85 	WideCharToMultiByte( CP_ACP, 0, lpStrW, -1, lpStrA, cNeeded, NULL, NULL ); \
86 } \
87 else \
88 	lpStrA = NULL;
89 
90 
91 #define AUTO_WSTR2STR( lpStr ) \
92 	_AUTO_WSTR2STR( lpStr##A, lpStr##W )
93 
94 #define AUTO_STR( lpStr, cchBuffer ) \
95 LPSTR	lpStr##A = lpStr##W ? (LPSTR)_alloca( (cchBuffer) * sizeof(CHAR) ) : NULL;
96 
97 #endif	// __cplusplus
98 
99 #define STRBUF2WSTR( lpStr, cchSrcBuffer, cchDestBuffer ) \
100 	MultiByteToWideChar( CP_ACP, 0, lpStr##A, cchSrcBuffer, lpStr##W, cchDestBuffer )
101 
102 #define STR2WSTR( lpStr, cchBuffer ) \
103 	STRBUF2WSTR( lpStr, -1, cchBuffer )
104 
105 #define WSTR2STR( lpStr, cchBuffer ) \
106 	WideCharToMultiByte( CP_ACP, 0, lpStr##W, -1, lpStr##A, cchBuffer, NULL, NULL )
107 
108 EXTERN_C void WINAPI ResolveThunk_WINDOWS( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
109 EXTERN_C void WINAPI ResolveThunk_TRYLOAD( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
110 EXTERN_C void WINAPI ResolveThunk_ALLWAYS( FARPROC *lppfn, LPCSTR lpLibFileName, LPCSTR lpFuncName, FARPROC lpfnEmulate, FARPROC lpfnFailure );
111 
112 
113 
114 
115 #define IMPLEMENT_THUNK( module, resolve, rettype, calltype, func, params ) \
116 EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr; \
117 EXTERN_C rettype calltype func##_##resolve params; \
118 static rettype calltype func##_##Failure params; \
119 static _declspec ( naked ) func##_Thunk() \
120 { \
121 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func, (FARPROC)func##_##resolve, (FARPROC)func##_##Failure ); \
122 	_asm	jmp	[module##_##func##_Ptr] \
123 } \
124 EXTERN_C _declspec( naked ) rettype calltype func params \
125 { \
126 	_asm	jmp	[module##_##func##_Ptr] \
127 } \
128 EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk; \
129 static rettype calltype func##_##Failure params \
130 { \
131 	SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); \
132 	return (rettype)0; \
133 } \
134 EXTERN_C rettype calltype func##_##resolve params
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 #define DEFINE_CUSTOM_THUNK( module, resolve, rettype, calltype, func, params ) \
147 EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr; \
148 static _declspec ( naked ) func##_Thunk() \
149 { \
150 	ResolveThunk_##resolve( &module##_##func##_Ptr, #module ".dll", #func ); \
151 	_asm	jmp	[module##_##func##_Ptr] \
152 } \
153 EXTERN_C _declspec( naked ) rettype calltype func params \
154 { \
155 	_asm	jmp	[module##_##func##_Ptr] \
156 } \
157 EXTERN_C _declspec( dllexport ) FARPROC module##_##func##_Ptr = (FARPROC)func##_Thunk;
158