From 52c8ec1461d9544ef56c0ac7f41df828700ae001 Mon Sep 17 00:00:00 2001 From: Hiroshi Inoue Date: Wed, 31 Aug 2016 16:27:48 +0900 Subject: [PATCH] Build the setup program psqlsetup used for the regression dsn. psqlsetup also playes a role to relay ODBC API calls to compiled psqlodbc35w(30a).dll. --- loadlib.c | 46 +++++- loadlib.h | 1 + psqlsetup.c | 100 ++++++++++++ psqlsetup.def | 70 +++++++++ psqlsetupa.def | 69 +++++++++ winbuild/platformbuild.vcxproj | 70 ++++++--- winbuild/psqlodbc.vcxproj | 4 +- winbuild/psqlsetup.vcxproj | 274 +++++++++++++++++++++++++++++++++ 8 files changed, 609 insertions(+), 25 deletions(-) create mode 100644 psqlsetup.c create mode 100644 psqlsetup.def create mode 100644 psqlsetupa.def create mode 100644 winbuild/psqlsetup.vcxproj diff --git a/loadlib.c b/loadlib.c index 3f3a6b8..c597f81 100644 --- a/loadlib.c +++ b/loadlib.c @@ -46,10 +46,14 @@ #define WIN_DYN_LOAD #ifdef UNICODE_SUPPORT CSTR pgenlist = "pgenlist"; -CSTR pgenlistdll = "PGENLIST.dll"; +CSTR pgenlistdll = "pgenlist.dll"; +CSTR psqlodbc = "psqlodbc35w"; +CSTR psqlodbcdll = "psqlodbc35w.dll"; #else CSTR pgenlist = "pgenlista"; -CSTR pgenlistdll = "PGENLISTA.dll"; +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 @@ -80,6 +84,7 @@ CSTR pgenlistdll = "PGENLISTA.dll"; #if defined(_MSC_DELAY_LOAD_IMPORT) static BOOL loaded_pgenlist = FALSE; +static BOOL loaded_psqlodbc = FALSE; /* * Load a DLL based on psqlodbc path. */ @@ -122,6 +127,7 @@ DliErrorHook(unsigned dliNotify, PDelayLoadInfo pdli) { HMODULE hmodule = NULL; + const char* call_module = NULL; mylog("Dli%sHook %s Notify=%d\n", (dliFailLoadLib == dliNotify || dliFailGetProc == dliNotify) ? "Error" : "Notify", NULL != pdli->szDll ? pdli->szDll : pdli->dlp.szProcName, dliNotify); switch (dliNotify) @@ -130,15 +136,40 @@ DliErrorHook(unsigned dliNotify, 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(pgenlist), NULL == hmodule) - hmodule = LoadLibrary(pgenlist); + 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; } +#if (_MSC_VER < 1300) +void EnableDelayLoadHook() +{ + __pfnDliFailureHook = DliErrorHook; + __pfnDliNotifyHook = DliErrorHook; +} +#else +void EnableDelayLoadHook() +{ + __pfnDliFailureHook2 = DliErrorHook; + __pfnDliNotifyHook2 = DliErrorHook; +} +#endif /* _MSC_VER */ + /* * unload delay loaded libraries. */ @@ -156,8 +187,15 @@ void CleanupDelayLoadedDLLs(void) if (loaded_pgenlist) { success = (*func)(pgenlistdll); + loaded_pgenlist = FALSE; mylog("%s unload success=%d\n", pgenlistdll, success); } + if (loaded_psqlodbc) + { + success = (*func)(psqlodbcdll); + loaded_psqlodbc = FALSE; + mylog("%s unload success=%d\n", psqlodbcdll, success); + } return; } #else diff --git a/loadlib.h b/loadlib.h index f78ae33..a76b65b 100644 --- a/loadlib.h +++ b/loadlib.h @@ -32,6 +32,7 @@ void CALL_ReleaseTransactionObject(void *); #endif /* _HANDLE_ENLIST_IN_DTC_ */ /* void UnloadDelayLoadedDLLs(BOOL); */ void CleanupDelayLoadedDLLs(void); +void EnableDelayLoadHook(void); #ifdef __cplusplus } diff --git a/psqlsetup.c b/psqlsetup.c new file mode 100644 index 0000000..47c90ff --- /dev/null +++ b/psqlsetup.c @@ -0,0 +1,100 @@ +#include + +#include "psqlodbc.h" +#include "dlg_specific.h" + +HINSTANCE s_hModule; /* Saved module handle. */ + +#ifdef PG_BIN + +#include "loadlib.h" +#include +#include +#include +#include + +RETCODE SQL_API SQLDummyOrdinal(void); + +/* + * This function is used to cause the Driver Manager to + * call functions by number rather than name, which is faster. + * The ordinal value of this function must be 199 to have the + * Driver Manager do this. Also, the ordinal values of the + * functions must match the value of fFunction in SQLGetFunctions() + */ +RETCODE SQL_API +SQLDummyOrdinal(void) +{ + return SQL_SUCCESS; +} + +#endif /* PG_BIN */ + +static HINSTANCE s_hLModule; +/* This is where the Driver Manager attaches to this Driver */ + +extern GLOBAL_VALUES globals; + +int initialize_global_cs(void) +{ + static int init = 1; + + if (!init) + return 0; + init = 0; + InitializeLogging(); + memset(&globals, 0, sizeof(globals)); + + return 0; +} + +static void finalize_global_cs(void) +{ + finalize_globals(&globals); + FinalizeLogging(); +#ifdef _DEBUG +#ifdef _MEMORY_DEBUG_ + // _CrtDumpMemoryLeaks(); +#endif /* _MEMORY_DEBUG_ */ +#endif /* _DEBUG */ +} + +BOOL WINAPI +DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + s_hModule = hInst; /* Save for dialog boxes */ + + if (initialize_global_cs() == 0) + getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL); +#ifdef PG_BIN + if (s_hLModule = LoadLibraryEx(PG_BIN "\\libpq.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH), s_hLModule == NULL) + { + mylog("libpq folder %s couldn't be loaded\n", PG_BIN); + } + EnableDelayLoadHook(); +#endif + break; + + case DLL_THREAD_ATTACH: + break; + + case DLL_PROCESS_DETACH: + CleanupDelayLoadedDLLs(); + FreeLibrary(s_hLModule); + finalize_global_cs(); + return TRUE; + + case DLL_THREAD_DETACH: + break; + + default: + break; + } + + return TRUE; + + UNREFERENCED_PARAMETER(lpReserved); +} diff --git a/psqlsetup.def b/psqlsetup.def new file mode 100644 index 0000000..d15bdf2 --- /dev/null +++ b/psqlsetup.def @@ -0,0 +1,70 @@ +LIBRARY psqlsetup +EXPORTS + +DllMain @201 +ConfigDSN @202 +ConfigDriver @203 + +SQLBindCol @4 +SQLCancel @5 +SQLDisconnect @9 +SQLExecute @12 +SQLFetch @13 +SQLFreeStmt @16 +SQLNumResultCols @18 +SQLRowCount @20 +SQLGetData @43 +SQLGetFunctions @44 +SQLParamData @48 +SQLPutData @49 +SQLDescribeParam @58 +SQLExtendedFetch @59 +SQLMoreResults @61 +SQLNumParams @63 +SQLSetPos @68 +SQLBindParameter @72 + +SQLAllocHandle @80 +SQLBindParam @81 +SQLCloseCursor @82 +SQLCopyDesc @84 +SQLEndTran @85 +SQLFetchScroll @86 +SQLFreeHandle @87 +SQLGetEnvAttr @92 +SQLSetEnvAttr @98 +SQLBulkOperations @100 + +SQLDummyOrdinal @199 + +SQLColAttributeW @101 +SQLColumnPrivilegesW @102 +SQLColumnsW @103 +SQLConnectW @104 +SQLDescribeColW @106 +SQLExecDirectW @107 +SQLForeignKeysW @108 +SQLGetConnectAttrW @109 +SQLGetCursorNameW @110 +SQLGetInfoW @111 +SQLNativeSqlW @112 +SQLPrepareW @113 +SQLPrimaryKeysW @114 +SQLProcedureColumnsW @115 +SQLProceduresW @116 +SQLSetConnectAttrW @117 +SQLSetCursorNameW @118 +SQLSpecialColumnsW @119 +SQLStatisticsW @120 +SQLTablesW @121 +SQLTablePrivilegesW @122 +SQLDriverConnectW @123 +SQLGetDiagRecW @124 +SQLGetStmtAttrW @125 +SQLSetStmtAttrW @126 +SQLSetDescFieldW @127 +SQLGetTypeInfoW @128 +SQLGetDiagFieldW @129 +SQLGetDescFieldW @130 +SQLGetDescRecW @131 +SQLSetDescRecW @132 diff --git a/psqlsetupa.def b/psqlsetupa.def new file mode 100644 index 0000000..da1a778 --- /dev/null +++ b/psqlsetupa.def @@ -0,0 +1,69 @@ +LIBRARY psqlsetupa +EXPORTS + +DllMain @201 +ConfigDSN @202 +ConfigDriver @203 + +SQLBindCol @4 +SQLCancel @5 +SQLConnect @7 +SQLDescribeCol @8 +SQLDisconnect @9 +SQLExecDirect @11 +SQLExecute @12 +SQLFetch @13 +SQLFreeStmt @16 +SQLGetCursorName @17 +SQLNumResultCols @18 +SQLPrepare @19 +SQLRowCount @20 +SQLSetCursorName @21 +SQLColumns @40 +SQLDriverConnect @41 +SQLGetData @43 +SQLGetFunctions @44 +SQLGetInfo @45 +SQLGetTypeInfo @47 +SQLParamData @48 +SQLPutData @49 +SQLSpecialColumns @52 +SQLStatistics @53 +SQLTables @54 +SQLColumnPrivileges @56 +SQLDescribeParam @58 +SQLExtendedFetch @59 +SQLForeignKeys @60 +SQLMoreResults @61 +SQLNativeSql @62 +SQLNumParams @63 +SQLPrimaryKeys @65 +SQLProcedureColumns @66 +SQLProcedures @67 +SQLSetPos @68 +SQLTablePrivileges @70 +SQLBindParameter @72 + +SQLAllocHandle @80 +SQLBindParam @81 +SQLCloseCursor @82 +SQLColAttribute @83 +SQLCopyDesc @84 +SQLEndTran @85 +SQLFetchScroll @86 +SQLFreeHandle @87 +SQLGetDescField @88 +SQLGetDescRec @89 +SQLGetDiagField @90 +SQLGetDiagRec @91 +SQLGetEnvAttr @92 +SQLGetConnectAttr @93 +SQLGetStmtAttr @94 +SQLSetConnectAttr @95 +SQLSetDescField @96 +SQLSetDescRec @97 +SQLSetEnvAttr @98 +SQLSetStmtAttr @99 +SQLBulkOperations @100 + +SQLDummyOrdinal @199 diff --git a/winbuild/platformbuild.vcxproj b/winbuild/platformbuild.vcxproj index c2ad3c0..5ec4b43 100755 --- a/winbuild/platformbuild.vcxproj +++ b/winbuild/platformbuild.vcxproj @@ -18,61 +18,93 @@ + + + + ANSI_VERSION=yes + + - - - - - - - - + Properties="Configuration=$(Configuration);srcPath=$(srcPath)"/> + - - - - - - - - + + + + + $(ADD_DEFINES);DBMS_NAME="PostgreSQL ANSI(x64)" - $(ADD_DEFINES);UNICODE_SUPPORT + $(ADD_DEFINES);UNICODE_SUPPORT;UNICODE_SUPPORTXX $(RSC_DEFINES);UNICODE_SUPPORT - $(ADD_DEFINES);UNICODE_SUPPORT;DBMS_NAME="PostgreSQL Unicode(x64)" + $(ADD_DEFINES);UNICODE_SUPPORT;UNICODE_SUPPORTXX;DBMS_NAME="PostgreSQL Unicode(x64)" $(RSC_DEFINES);UNICODE_SUPPORT diff --git a/winbuild/psqlsetup.vcxproj b/winbuild/psqlsetup.vcxproj new file mode 100644 index 0000000..d334e03 --- /dev/null +++ b/winbuild/psqlsetup.vcxproj @@ -0,0 +1,274 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {DFD90C9A-E9BA-4CA3-812B-E820EE9A3F5B} + Win32Proj + psqlsetup + true + + + + DynamicLibrary + + true + v120_xp + MultiByte + + + DynamicLibrary + + true + v120_xp + MultiByte + + + DynamicLibrary + + false + v120_xp + true + MultiByte + + + DynamicLibrary + + false + v120_xp + true + MultiByte + + + + + + + + + + + + + + + + + + no + + + + + + + psqlsetupa + ANSI + psqlodbc30a.lib + psqlodbc30a.dll;pgenlista.dll + + + + + psqlsetup + Unicode + psqlodbc35w.lib + psqlodbc35w.dll;pgenlist.dll + UNICODE_SUPPORT + UNICODE_SUPPORT + + + + x86 + + + x64 + + + + false + + + + $(srcPath)$(TARGET_CPU)_$(TargetType)_$(Configuration)\psqlsetup\ + $(srcPath)$(TARGET_CPU)_$(TargetType)_$(Configuration)\ + + + + true + + + true + + + false + + + false + + + + + + $(ADD_DEFINES);PG_BIN="$(PG_BIN.Replace('\','\\'))" + + + + + DYNAMIC_LOAD;_HANDLE_ENLIST_IN_DTC_;$(ADD_DEFINES);_CRT_SECURE_NO_DEPRECATE;WIN32;_DEBUG;_WINDOWS;_USRDLL;PSQLSETUP_EXPORTS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + + + $(RSC_DEFINES);%(PreprocessorDefinitions) + + + MachineX86 + $(srcPath)$(targetName).def + true + $(OutDir);%(AdditionalLibraryDirectories) + + + $(DELAY_LOAD_DLLS);%(DelayLoadDLLs) + $(CALL_LIB);winmm.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + true + + + + + + + + + + + + + + + + + DYNAMIC_LOAD;_HANDLE_ENLIST_IN_DTC_;WIN_MULTITHREAD_SUPPORT;$(ADD_DEFINES);_CRT_SECURE_NO_DEPRECATE;WIN32;_DEBUG;_WINDOWS;_USRDLL;PSQLSETUP_EXPORTS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + + + $(RSC_DEFINES);%(PreprocessorDefinitions) + + + MachineX64 + $(srcPath)$(targetName).def + true + $(OutDir);%(AdditionalLibraryDirectories) + + + $(DELAY_LOAD_DLLS);%(DelayLoadDLLs) + $(CALL_LIB);winmm.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + true + + + + + + + + + + + + + + + + + DYNAMIC_LOAD;_HANDLE_ENLIST_IN_DTC_;WIN_MULTITHREAD_SUPPORT;$(ADD_DEFINES);_CRT_SECURE_NO_DEPRECATE;WIN32;NDEBUG;_WINDOWS;_USRDLL;PSQLSETUP_EXPORTS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + + + $(RSC_DEFINES);%(PreprocessorDefinitions) + + + MachineX86 + $(srcPath)$(targetName).def + true + $(OutDir);%(AdditionalLibraryDirectories) + + + $(DELAY_LOAD_DLLS);%(DelayLoadDLLs) + $(CALL_LIB);winmm.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + true + + + + + + + + + DYNAMIC_LOAD;_HANDLE_ENLIST_IN_DTC_;WIN_MULTITHREAD_SUPPORT;$(ADD_DEFINES);_CRT_SECURE_NO_DEPRECATE;WIN32;NDEBUG;_WINDOWS;_USRDLL;PSQLSETUP_EXPORTS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + + + $(RSC_DEFINES);%(PreprocessorDefinitions) + + + MachineX64 + $(srcPath)$(targetName).def + true + $(OutDir);%(AdditionalLibraryDirectories) + + + $(DELAY_LOAD_DLLS);%(DelayLoadDLLs) + $(CALL_LIB);winmm.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + true + + + + + + + + + + $(TargetName).exp;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + -- 2.39.5