changeset 13771:80b30e186b73

[Win32] Use Toolhelp32 APi to find octinterp module path. * sysdep.cc (w32_set_octave_home): Use Toolhelp32 API to find the octinterp module path.
author Michael Goffioul <michael.goffioul@gmail.com>
date Fri, 28 Oct 2011 23:53:08 +0100
parents b0bb7bd9e0c8
children ebefc477607b
files src/sysdep.cc
diffstat 1 files changed, 29 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/sysdep.cc	Fri Oct 28 09:27:00 2011 -0700
+++ b/src/sysdep.cc	Fri Oct 28 23:53:08 2011 +0100
@@ -101,31 +101,43 @@
 #endif
 
 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
+
+#define WIN32_LEAN_AND_MEAN
+#include <tlhelp32.h>
+
 static void
 w32_set_octave_home (void)
 {
-  int n = 1024;
+  std::string bin_dir;
 
-  std::string bin_dir (n, '\0');
+  HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE |
+				       TH32CS_SNAPMODULE32, 0);
 
-  while (true)
+  if (h != INVALID_HANDLE_VALUE)
     {
-      HMODULE hMod = GetModuleHandle ("octinterp");
-      if (! hMod)
-        hMod = GetModuleHandle ("liboctinterp-0");
+      MODULEENTRY32 mod_info;
+
+      ZeroMemory (&mod_info, sizeof (mod_info));
+      mod_info.dwSize = sizeof (mod_info);
 
-      int status = GetModuleFileName (hMod, &bin_dir[0], n);
+      if (Module32First (h, &mod_info))
+	{
+	  do
+	    {
+	      std::string mod_name (mod_info.szModule);
 
-      if (status < n)
-        {
-          bin_dir.resize (status);
-          break;
-        }
-      else
-        {
-          n *= 2;
-          bin_dir.resize (n);
-        }
+	      if (mod_name.find ("octinterp") != std::string::npos)
+		{
+		  bin_dir = mod_info.szExePath;
+		  if (bin_dir[bin_dir.length () - 1] != '\\')
+		    bin_dir.append (1, '\\');
+		  break;
+		}
+	    }
+	  while (Module32Next (h, &mod_info));
+	}
+
+      CloseHandle (h);
     }
 
   if (! bin_dir.empty ())