# HG changeset patch # User John W. Eaton # Date 1382988740 14400 # Node ID 136a9e27256ed54fe554e8953be03fe5205c8288 # Parent d029ef208e4a0c9e1ab862310856c3213f40d62d automatically locate wrapper binary on Windows systems (bug #40381) * main.in.cc (w32_get_octave_home): New function. (get_octave_home): Call it if OCTAVE_HOME is not found in the environment. diff -r d029ef208e4a -r 136a9e27256e src/main.in.cc --- a/src/main.in.cc Mon Oct 28 19:03:29 2013 +0100 +++ b/src/main.in.cc Mon Oct 28 15:32:20 2013 -0400 @@ -42,6 +42,66 @@ // From gnulib, so OK for Windows too. #include +#if defined (__WIN32__) && ! defined (_POSIX_VERSION) + +#define WIN32_LEAN_AND_MEAN +#include + +static std::string +w32_get_octave_home (void) +{ + std::string retval; + + std::string bin_dir; + + HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE +#ifdef TH32CS_SNAPMODULE32 + | TH32CS_SNAPMODULE32 +#endif + , 0); + + if (h != INVALID_HANDLE_VALUE) + { + MODULEENTRY32 mod_info; + + ZeroMemory (&mod_info, sizeof (mod_info)); + mod_info.dwSize = sizeof (mod_info); + + if (Module32First (h, &mod_info)) + { + do + { + std::string mod_name (mod_info.szModule); + + if (mod_name.find ("octave") != 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 ()) + { + size_t pos = bin_dir.rfind ("\\bin\\"); + + if (pos != std::string::npos) + retval = bin_dir.substr (0, pos); + } + + return retval; +} + +#endif + #if ! defined (__WIN32__) && ! defined (__CYGWIN__) #include @@ -262,6 +322,11 @@ { std::string oh = octave_getenv ("OCTAVE_HOME"); +#if defined (__WIN32__) && ! defined (_POSIX_VERSION) + if (oh.empty ()) + oh = w32_get_octave_home (); +#endif + return oh.empty () ? std::string (OCTAVE_PREFIX) : oh; }