forked from postgresql-interfaces/psqlodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadlib.c
286 lines (268 loc) · 7.18 KB
/
loadlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*------
* Module: loadlib.c
*
* Description: This module contains routines related to
* delay load import libraries.
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifndef WIN32
#include <errno.h>
#endif /* WIN32 */
#include "loadlib.h"
#include "pgenlist.h"
#include "misc.h"
#ifdef WIN32
#ifdef _MSC_VER
#pragma comment(lib, "Delayimp")
#ifdef _HANDLE_ENLIST_IN_DTC_
#ifdef UNICODE_SUPPORT
#pragma comment(lib, "pgenlist")
#else
#pragma comment(lib, "pgenlista")
#endif /* UNICODE_SUPPORT */
#endif /* _HANDLE_ENLIST_IN_DTC_ */
// The followings works under VC++6.0 but doesn't work under VC++7.0.
// Please add the equivalent linker options using command line etc.
#if (_MSC_VER == 1200) && defined(DYNAMIC_LOAD) // VC6.0
#ifdef UNICODE_SUPPORT
#pragma comment(linker, "/Delayload:pgenlist.dll")
#else
#pragma comment(linker, "/Delayload:pgenlista.dll")
#endif /* UNICODE_SUPPORT */
#pragma comment(linker, "/Delay:UNLOAD")
#endif /* _MSC_VER */
#endif /* _MSC_VER */
#if defined(DYNAMIC_LOAD)
#define WIN_DYN_LOAD
#ifdef UNICODE_SUPPORT
CSTR pgenlist = "pgenlist";
CSTR pgenlistdll = "pgenlist.dll";
CSTR psqlodbc = "psqlodbc35w";
CSTR psqlodbcdll = "psqlodbc35w.dll";
#else
CSTR pgenlist = "pgenlista";
CSTR pgenlistdll = "pgenlista.dll";
CSTR psqlodbc = "psqlodbc30a";
CSTR psqlodbcdll = "psqlodbc30a.dll";
#endif /* UNICODE_SUPPORT */
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#define _MSC_DELAY_LOAD_IMPORT
#endif /* MSC_VER */
#endif /* DYNAMIC_LOAD */
#endif /* WIN32 */
#if defined(_MSC_DELAY_LOAD_IMPORT)
/*
* Error hook function for delay load import.
* Try to load a DLL based on psqlodbc path.
*/
#if (_MSC_VER >= 1900) /* vc14 or later */
#define TRY_DLI_HOOK \
__try {
#define RELEASE_NOTIFY_HOOK
#elif (_MSC_VER < 1300) /* vc6 */
extern PfnDliHook __pfnDliFailureHook;
extern PfnDliHook __pfnDliNotifyHook;
#define TRY_DLI_HOOK \
__try { \
__pfnDliFailureHook = DliErrorHook; \
__pfnDliNotifyHook = DliErrorHook;
#define RELEASE_NOTIFY_HOOK \
__pfnDliNotifyHook = NULL;
#else /* vc7 ~ 12 */
extern PfnDliHook __pfnDliFailureHook2;
extern PfnDliHook __pfnDliNotifyHook2;
#define TRY_DLI_HOOK \
__try { \
__pfnDliFailureHook2 = DliErrorHook; \
__pfnDliNotifyHook2 = DliErrorHook;
#define RELEASE_NOTIFY_HOOK \
__pfnDliNotifyHook2 = NULL;
#endif /* _MSC_VER */
#else
#define TRY_DLI_HOOK \
__try {
#define RELEASE_NOTIFY_HOOK
#endif /* _MSC_DELAY_LOAD_IMPORT */
#if defined(_MSC_DELAY_LOAD_IMPORT)
static BOOL loaded_pgenlist = FALSE;
static HMODULE enlist_module = NULL;
static BOOL loaded_psqlodbc = FALSE;
/*
* Load a DLL based on psqlodbc path.
*/
HMODULE MODULE_load_from_psqlodbc_path(const char *module_name)
{
extern HINSTANCE s_hModule;
HMODULE hmodule = NULL;
char szFileName[MAX_PATH];
if (GetModuleFileName(s_hModule, szFileName, sizeof(szFileName)) > 0)
{
char drive[_MAX_DRIVE], dir[_MAX_DIR], sysdir[MAX_PATH];
_splitpath(szFileName, drive, dir, NULL, NULL);
GetSystemDirectory(sysdir, MAX_PATH);
SPRINTF_FIXED(szFileName, "%s%s%s.dll", drive, dir, module_name);
if (_strnicmp(szFileName, sysdir, strlen(sysdir)) != 0)
{
hmodule = LoadLibraryEx(szFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
MYLOG(0, "psqlodbc path based %s loaded module=%p\n", module_name, hmodule);
}
}
return hmodule;
}
static FARPROC WINAPI
DliErrorHook(unsigned dliNotify,
PDelayLoadInfo pdli)
{
HMODULE hmodule = NULL;
const char* call_module = NULL;
MYLOG(0, "Dli%sHook %s Notify=%d\n", (dliFailLoadLib == dliNotify || dliFailGetProc == dliNotify) ? "Error" : "Notify", NULL != pdli->szDll ? pdli->szDll : pdli->dlp.szProcName, dliNotify);
switch (dliNotify)
{
case dliNotePreLoadLibrary:
case dliFailLoadLib:
RELEASE_NOTIFY_HOOK
if (_strnicmp(pdli->szDll, pgenlist, strlen(pgenlist)) == 0)
call_module = pgenlist;
else if (_strnicmp(pdli->szDll, psqlodbc, strlen(psqlodbc)) == 0)
call_module = psqlodbc;
if (call_module)
{
if (hmodule = MODULE_load_from_psqlodbc_path(call_module), NULL == hmodule)
hmodule = LoadLibrary(call_module);
if (NULL != hmodule)
{
if (pgenlist == call_module)
loaded_pgenlist = TRUE;
else if (psqlodbc == call_module)
loaded_psqlodbc = TRUE;
}
}
break;
}
return (FARPROC) hmodule;
}
void AlreadyLoadedPsqlodbc(void)
{
loaded_psqlodbc = TRUE;
}
/*
* unload delay loaded libraries.
*/
typedef BOOL (WINAPI *UnloadFunc)(LPCSTR);
void CleanupDelayLoadedDLLs(void)
{
BOOL success;
#if (_MSC_VER < 1300) /* VC6 DELAYLOAD IMPORT */
UnloadFunc func = __FUnloadDelayLoadedDLL;
#else
UnloadFunc func = __FUnloadDelayLoadedDLL2;
#endif
/* The dll names are case sensitive for the unload helper */
if (loaded_pgenlist)
{
if (enlist_module != NULL)
{
MYLOG(0, "Freeing Library %s\n", pgenlistdll);
FreeLibrary(enlist_module);
}
MYLOG(0, "%s unloading\n", pgenlistdll);
success = (*func)(pgenlistdll);
MYLOG(0, "%s unloaded success=%d\n", pgenlistdll, success);
loaded_pgenlist = FALSE;
}
if (loaded_psqlodbc)
{
MYLOG(0, "%s unloading\n", psqlodbcdll);
success = (*func)(psqlodbcdll);
MYLOG(0, "%s unloaded success=%d\n", psqlodbcdll, success);
loaded_psqlodbc = FALSE;
}
return;
}
#else
void CleanupDelayLoadedDLLs(void)
{
return;
}
#endif /* _MSC_DELAY_LOAD_IMPORT */
#ifdef _HANDLE_ENLIST_IN_DTC_
RETCODE CALL_EnlistInDtc(ConnectionClass *conn, void *pTra, int method)
{
RETCODE ret;
BOOL loaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
if (!loaded_pgenlist)
{
TRY_DLI_HOOK
ret = EnlistInDtc(conn, pTra, method);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
if (enlist_module = MODULE_load_from_psqlodbc_path(pgenlist), NULL == enlist_module)
loaded = FALSE;
else
ret = EnlistInDtc(conn, pTra, method);
}
if (loaded)
loaded_pgenlist = TRUE;
RELEASE_NOTIFY_HOOK
}
else
ret = EnlistInDtc(conn, pTra, method);
#else
ret = EnlistInDtc(conn, pTra, method);
loaded_pgenlist = TRUE;
#endif /* _MSC_DELAY_LOAD_IMPORT */
return ret;
}
RETCODE CALL_DtcOnDisconnect(ConnectionClass *conn)
{
if (loaded_pgenlist)
return DtcOnDisconnect(conn);
return FALSE;
}
RETCODE CALL_IsolateDtcConn(ConnectionClass *conn, BOOL continueConnection)
{
if (loaded_pgenlist)
return IsolateDtcConn(conn, continueConnection);
return FALSE;
}
void *CALL_GetTransactionObject(HRESULT *hres)
{
void *ret = NULL;
BOOL loaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
if (!loaded_pgenlist)
{
TRY_DLI_HOOK
ret = GetTransactionObject(hres);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
if (enlist_module = MODULE_load_from_psqlodbc_path(pgenlist), NULL == enlist_module)
loaded = FALSE;
else
ret = GetTransactionObject(hres);
}
if (loaded)
loaded_pgenlist = TRUE;
RELEASE_NOTIFY_HOOK
}
else
ret = GetTransactionObject(hres);
#else
ret = GetTransactionObject(hres);
loaded_pgenlist = TRUE;
#endif /* _MSC_DELAY_LOAD_IMPORT */
return ret;
}
void CALL_ReleaseTransactionObject(void *pObj)
{
if (loaded_pgenlist)
ReleaseTransactionObject(pObj);
return;
}
#endif /* _HANDLE_ENLIST_IN_DTC_ */