From f40e5e352e60ab4d70aecdd283c46f3a25b78958 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 5 Jan 2012 22:22:19 +0000 Subject: [PATCH] Check registry for location of Cantera data files --- Cantera/src/base/misc.cpp | 55 +++++++++++++++++++++++++-------------- site_scons/wxsgen.py | 19 ++++++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/Cantera/src/base/misc.cpp b/Cantera/src/base/misc.cpp index ae43253ab..a498df529 100644 --- a/Cantera/src/base/misc.cpp +++ b/Cantera/src/base/misc.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#pragma comment(lib, "advapi32") #endif using namespace std; @@ -764,6 +766,11 @@ namespace Cantera { * @param msg c++ string to be written to the screen * @ingroup textlogs */ +#ifdef WIN32 + long int readStringRegistryKey(const std::string& keyName, const std::string& valueName, + std::string& value, const std::string& defaultValue); +#endif + void writelog(const std::string& msg) { pMessenger->writelog(msg); } @@ -1148,6 +1155,26 @@ protected: } } +#ifdef WIN32 + long int Application::readStringRegistryKey(const std::string& keyName, const std::string& valueName, + std::string& value, const std::string& defaultValue) { + + HKEY key; + long open_error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_READ, &key); + value = defaultValue; + CHAR buffer[1024]; + DWORD bufferSize = sizeof(buffer); + ULONG error; + error = RegQueryValueEx(key, valueName.c_str(), 0, NULL, (LPBYTE) buffer, &bufferSize); + if (ERROR_SUCCESS == error) { + value = buffer; + } + RegCloseKey(key); + return error; + } +#endif + + void setTmpDir(std::string tmp) { app()->setTmpDir(tmp); } void Application::setTmpDir(std::string tmp) { APP_LOCK(); @@ -1343,27 +1370,15 @@ protected: #ifdef WIN32 - // - // Under Windows, the Cantera setup utility puts data files in - // a directory 'Cantera\data' below the one the environment - // variable COMMONPROGRAMFILES points to. (This is usually - // C:\Program Files\Common Files.) If this environment - // variable is defined, then this directory is assumed to - // exist and is added to the search path. - // - const char* comfiles = getenv("COMMONPROGRAMFILES"); - if (comfiles != 0) { - string cfiles = string(comfiles); + // Under Windows, the Cantera setup utility records the installation + // directory in the registry. Data files are stored in the 'data' and + // 'templates' subdirectories of the main installation directory. - // remove quotes if necessary - if (cfiles[0] == '\'') - cfiles = cfiles.substr(1,1000); - if (cfiles[cfiles.size()-1] == '\'') cfiles[cfiles.size()-1] = '\n'; - - string datadir = string(comfiles) + "/Cantera/data"; - string tmpldir = string(comfiles) + "/Cantera/templates"; - dirs.push_back(datadir); - dirs.push_back(tmpldir); + std::string installDir; + readStringRegistryKey("SOFTWARE\\Cantera\\Cantera 2.0", "InstallDir", installDir, ""); + if (installDir != "") { + dirs.push_back(installDir + "data"); + dirs.push_back(installDir + "templates"); } #endif diff --git a/site_scons/wxsgen.py b/site_scons/wxsgen.py index 8d361fd8b..571fc1c5a 100644 --- a/site_scons/wxsgen.py +++ b/site_scons/wxsgen.py @@ -129,6 +129,14 @@ class WxsGenerator(object): templates_dir = self.addDirectoryContents('templates', instdir, extras) tutorials_dir = self.addDirectoryContents('tutorials', instdir, extras) + # Registry entries + reg_key = self.addRegistryKey(core, product, Id='CanteraRegRoot', Root='HKLM', + Key='Software\\Cantera\\Cantera 2.0', + Action='createAndRemoveOnUninstall') + et.SubElement(reg_key, 'RegistryValue', dict(Type='string', + Name='InstallDir', + Value='[INSTALLDIR]')) + # Wix UI et.SubElement(product, 'UIRef', dict(Id='WixUI_FeatureTree')) et.SubElement(product, 'UIRef', dict(Id='WixUI_ErrorProgressText')) @@ -144,6 +152,17 @@ class WxsGenerator(object): tree = et.ElementTree(wix) tree.write(outFile) + def addRegistryKey(self, feature, parent, Id, Root, Key, Action): + guid = str(uuid.uuid5(self.CANTERA_UUID, Id)) + + fields = {'Win64': 'yes'} if self.x64 else {} + dr = et.SubElement(parent, "DirectoryRef", dict(Id="TARGETDIR")) + c = et.SubElement(dr, "Component", + dict(Id=Id, Guid=guid, **fields)) + r = et.SubElement(c, "RegistryKey", dict(Id=Id, Root=Root, Key=Key, Action=Action)) + et.SubElement(feature, 'ComponentRef', dict(Id=Id)) + return r + def indent(elem, level=0): """ in-place prettyprint formatter (from lxml) """