vc14 seems to have troubles with delayload hook. Load psqlodbc35w(30a) in psqlsetup...
authorHiroshi Inoue <h-inoue@dream.email.ne.jp>
Fri, 11 Nov 2016 08:37:38 +0000 (17:37 +0900)
committerHiroshi Inoue <h-inoue@dream.email.ne.jp>
Mon, 21 Nov 2016 08:37:38 +0000 (17:37 +0900)
loadlib.c
loadlib.h
psqlsetup.c

index c597f813a9cc1de7d2ac45791241ece52e83626b..50739f2590c0a4255a44c3689d557a437673970a 100644 (file)
--- a/loadlib.c
+++ b/loadlib.c
@@ -156,20 +156,6 @@ DliErrorHook(unsigned  dliNotify,
    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.
  */
index a76b65bc7788fe34e8dd82f16a51d3a2fc0b5ee2..f78ae33ce93c1f6761f060cf3b3aa063cf5782fe 100644 (file)
--- a/loadlib.h
+++ b/loadlib.h
@@ -32,7 +32,6 @@ void  CALL_ReleaseTransactionObject(void *);
 #endif /* _HANDLE_ENLIST_IN_DTC_ */
 /* void    UnloadDelayLoadedDLLs(BOOL); */
 void   CleanupDelayLoadedDLLs(void);
-void   EnableDelayLoadHook(void);
 
 #ifdef __cplusplus
 }
index a12be04e586c2c9f9eabcfe449f34aecaea0cc02..d2bd038348b05cb9e21b51cdf5dd5dc8937fc144 100644 (file)
@@ -7,8 +7,6 @@ HINSTANCE s_hModule;        /* Saved module handle. */
 
 #ifdef PG_BIN
 
-#include "loadlib.h"
-#include <DelayImp.h>
 #include <stdio.h>
 #include <string.h>
 #include <sql.h>
@@ -30,7 +28,8 @@ SQLDummyOrdinal(void)
 
 #endif /* PG_BIN */
 
-static HINSTANCE s_hLModule;
+static HINSTANCE s_hLModule = NULL;
+static HINSTANCE s_hLModule2 = NULL;
 /* This is where the Driver Manager attaches to this Driver */
 
 extern GLOBAL_VALUES globals;
@@ -59,9 +58,19 @@ static void finalize_global_cs(void)
 #endif /* _DEBUG */
 }
 
+#ifdef UNICODE_SUPPORT
+CSTR   psqlodbcdll = "psqlodbc35w.dll";
+#else
+CSTR   psqlodbcdll = "psqlodbc30a.dll";
+
+#endif
+
 BOOL       WINAPI
 DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
 {
+   char dllPath[MAX_PATH] = "";
+   char *sptr;
+
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
@@ -71,10 +80,12 @@ DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
                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 in the folder %s couldn't be loaded\n", PG_BIN);
+#endif /* PG_BIN */
+           if (NULL == s_hLModule)
            {
-               char dllPath[MAX_PATH] = "", message[MAX_PATH] = "";
+               char message[MAX_PATH] = "";
 
-               mylog("libpq in the folder %s couldn't be loaded\n", PG_BIN);
                SQLGetPrivateProfileString(DBMS_NAME, "Driver", "", dllPath, sizeof(dllPath), ODBCINST_INI);
                if (dllPath[0])
                {
@@ -88,21 +99,39 @@ DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
                        snprintf(message, sizeof(message), "libpq in neither %s nor %s%s could be loaded", PG_BIN, drive, dir);
                    }
                }
+#ifdef PG_BIN
                else
                    snprintf(message, sizeof(message),  "libpq in the folder %s couldn't be loaded", PG_BIN);
+#endif /* PG_BIN */
                if (message[0])
                    MessageBox(NULL, message, "psqlsetup", MB_OK);
            }
-           EnableDelayLoadHook();
-#endif
+           if (GetModuleFileName(s_hModule, dllPath, sizeof(dllPath)) <= 0)
+           {
+               MessageBox(NULL, "GetModuleFileName error", "psqlsetup", MB_OK);
+               return TRUE;
+           }
+           if (sptr = strrchr(dllPath, '\\'), NULL == sptr)
+           {
+               MessageBox(NULL, "strrchr error", "psqlsetup", MB_OK);
+               return FALSE;
+           }
+           strcpy_s(sptr + 1, MAX_PATH - (size_t)(sptr - dllPath), psqlodbcdll);
+           if (s_hLModule2 = LoadLibraryEx(dllPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH), s_hLModule2 == NULL)
+           {
+               MessageBox(NULL, dllPath, "psqlodbc load error", MB_OK);
+               return TRUE;
+           }
            break;
 
        case DLL_THREAD_ATTACH:
            break;
 
        case DLL_PROCESS_DETACH:
-           CleanupDelayLoadedDLLs();
-           FreeLibrary(s_hLModule);
+           if (NULL != s_hLModule2)
+               FreeLibrary(s_hLModule2);
+           if (NULL != s_hLModule)
+               FreeLibrary(s_hLModule);
            finalize_global_cs();
            return TRUE;