This is an automated email from the ASF dual-hosted git repository.

jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git


The following commit(s) were added to refs/heads/develop by this push:
     new 4d7309f  GEODE-5898: Fixes product directory searching.
4d7309f is described below

commit 4d7309f55b24c8e5e41131dadf6fdf644666e5bf
Author: Jacob Barrett <[email protected]>
AuthorDate: Fri Oct 19 16:32:28 2018 +0000

    GEODE-5898: Fixes product directory searching.
---
 cppcache/src/CppCacheLibrary.cpp | 70 ++++++++--------------------------------
 cppcache/src/dllmain.cpp         |  5 +++
 2 files changed, 18 insertions(+), 57 deletions(-)

diff --git a/cppcache/src/CppCacheLibrary.cpp b/cppcache/src/CppCacheLibrary.cpp
index a6c9621..e3765a6 100644
--- a/cppcache/src/CppCacheLibrary.cpp
+++ b/cppcache/src/CppCacheLibrary.cpp
@@ -63,17 +63,6 @@ void CppCacheLibrary::closeLib(void) {
   // using geode.
 }
 
-// Returns pathname of product's lib directory, adds 'addon' to it if 'addon' 
is
-// not null.
-std::string CppCacheLibrary::getProductLibDir(const char* addon) {
-  std::string proddir = CppCacheLibrary::getProductDir();
-  proddir += "/lib/";
-  if (addon != nullptr) {
-    proddir += addon;
-  }
-  return proddir;
-}
-
 // return the directory where the library/DLL resides
 std::string CppCacheLibrary::getProductLibDir() {
   // otherwise... get the DLL path, and work backwards from it.
@@ -82,71 +71,38 @@ std::string CppCacheLibrary::getProductLibDir() {
   DllMainGetPath(buffer, PATH_MAX);
 
   std::string path(buffer);
-  std::string::size_type pos = std::string::npos;
+
 #ifdef WIN32
-  std::string cppName = PRODUCT_LIB_NAME;
-  cppName += ".dll";
-  pos = path.find(cppName);
-  if (std::string::npos == pos) {
-    std::string dotNetName = PRODUCT_DLL_NAME;
-    dotNetName += ".dll";
-    pos = path.find(dotNetName);
-  }
-#else
-  std::string cppName = "lib";
-  cppName += PRODUCT_LIB_NAME;
-  pos = path.find(cppName);
+  std::replace(path.begin(), path.end(), '\\', '/');
 #endif
-  if (0 < pos) {
-    return path.substr(0, --pos);
+
+  const auto pos = path.rfind('/');
+  if (std::string::npos != pos) {
+    return path.substr(0, pos);
   }
+
   return std::string();
 }
 
 std::string CppCacheLibrary::getProductDir() {
   // If the environment variable is set, use it.
-  std::string geodeNativeEnvironment = Utils::getEnv("GEODE_NATIVE_HOME");
+  auto geodeNativeEnvironment = Utils::getEnv("GEODE_NATIVE_HOME");
   if (geodeNativeEnvironment.length() > 0) {
     return geodeNativeEnvironment;
   }
 
   // otherwise... get the DLL path, and work backwards from it.
-  std::string productLibraryDirectoryName = getProductLibDir();
-  if (productLibraryDirectoryName.size() == 0) {
+  auto productLibraryDirectoryName = getProductLibDir();
+  if (productLibraryDirectoryName.empty()) {
     fprintf(stderr,
             "Cannot determine location of product directory.\n"
             "Please set GEODE_NATIVE_HOME environment variable.\n");
     fflush(stderr);
     throw apache::geode::client::IllegalStateException(
-        "Product installation directory "
-        "not found. Please set GEODE_NATIVE_HOME environment variable.");
-  }
-  // replace all '\' with '/' to make everything easier..
-  size_t len = productLibraryDirectoryName.length() + 1;
-  char* slashtmp = new char[len];
-  ACE_OS::strncpy(slashtmp, productLibraryDirectoryName.c_str(), len);
-  for (size_t i = 0; i < productLibraryDirectoryName.length(); i++) {
-    if (slashtmp[i] == '\\') {
-      slashtmp[i] = '/';
-    }
-  }
-  productLibraryDirectoryName = slashtmp;
-  delete[] slashtmp;
-  slashtmp = nullptr;
-
-  // check if it is "hidden/lib/debug" and work back from build area.
-  size_t hiddenidx = productLibraryDirectoryName.find("hidden");
-  if (hiddenidx != std::string::npos) {
-    // make sure hidden was a whole word...
-    hiddenidx--;
-    if (productLibraryDirectoryName[hiddenidx] == '/' ||
-        productLibraryDirectoryName[hiddenidx] == '\\') {
-      // odds are high hiddenidx terminates osbuild.dir.
-      std::string hiddenroute =
-          productLibraryDirectoryName.substr(0, hiddenidx) + "/product";
-      return hiddenroute;
-    }
+        "Product installation directory not found. Please set "
+        "GEODE_NATIVE_HOME environment variable.");
   }
+
   // check if bin on windows, and go back one...
   GF_D_ASSERT(productLibraryDirectoryName.length() > 4);
 #ifdef WIN32
diff --git a/cppcache/src/dllmain.cpp b/cppcache/src/dllmain.cpp
index 323a9f1..0ad9d12 100644
--- a/cppcache/src/dllmain.cpp
+++ b/cppcache/src/dllmain.cpp
@@ -41,6 +41,8 @@ static bool initgflibDone = initgflib();
 #ifdef _WIN32
 #include <windows.h>
 
+EXTERN_C IMAGE_DOS_HEADER __ImageBase;
+
 BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
                       LPVOID lpReserved) {
   switch (ul_reason_for_call) {
@@ -74,6 +76,9 @@ APACHE_GEODE_EXPORT void DllMainGetPath(char *result, int 
maxLen) {
   if (module == 0) {
     module = GetModuleHandle(dotNetLibName.c_str());
   }
+  if (module == 0) {
+    module = (HMODULE)&__ImageBase;
+  }
   if (module != 0) {
     GetModuleFileName(module, result, maxLen);
   } else {

Reply via email to