[icinga-checkins] icinga.org: icinga2/feature/wsa-12128: Update ScriptUtils:: MsiGetComponentPathShim to be only called on demand

git at icinga.org git at icinga.org
Sat Aug 6 11:30:24 CEST 2016


Module: icinga2
Branch: feature/wsa-12128
Commit: 7651a62d2f3f7d76ee30ce32ecda935e55702205
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=7651a62d2f3f7d76ee30ce32ecda935e55702205

Author: Rune Darrud <theflyingcorpse at gmail.com>
Date:   Sat Aug  6 10:48:52 2016 +0200

Update ScriptUtils::MsiGetComponentPathShim to be only called on demand

refs #12128

---

 lib/base/scriptutils.cpp |   45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp
index 94a0f01..28f80f4 100644
--- a/lib/base/scriptutils.cpp
+++ b/lib/base/scriptutils.cpp
@@ -31,9 +31,6 @@
 #include <boost/regex.hpp>
 #include <algorithm>
 #include <set>
-#ifdef _WIN32
-#include <msi.h>
-#endif /* _WIN32 */
 
 using namespace icinga;
 
@@ -300,14 +297,40 @@ void ScriptUtils::Assert(const Value& arg)
 String ScriptUtils::MsiGetComponentPathShim(const String& component)
 {
 #ifdef _WIN32
-	TCHAR productCode[39];
-	if (MsiGetProductCode(component.CStr(), productCode) != ERROR_SUCCESS)
-		return "";
-	TCHAR path[2048];
-	DWORD szPath = sizeof(path);
-	path[0] = '\0';
-	MsiGetComponentPath(productCode, component.CStr(), path, &szPath);
-	return path;
+	 typedef long(__stdcall *pMsiGetProductCode)(
+		TCHAR szProduct[39],
+		TCHAR lpProductBuf[39]);
+
+	 typedef long(__stdcall *pMsiGetComponentPath)(
+		TCHAR* szProductCode,
+		TCHAR* szComponent,
+		TCHAR* lpPathBuf,
+		DWORD *path);
+
+	/* If msi.dll is present, load it. */
+	if (HINSTANCE hInst = LoadLibrary("msi.dll")){
+		pMsiGetProductCode MsiGetProductCode = (pMsiGetProductCode)GetProcAddress(hInst, "MsiGetProductCodeA");
+		pMsiGetComponentPath MsiGetComponentPath = (pMsiGetComponentPath)GetProcAddress(hInst, "MsiGetComponentPathA");
+
+		if (MsiGetProductCode && MsiGetComponentPath) {
+			TCHAR productCode[39];
+			TCHAR * componentStr = strdup(component.CStr());
+
+			/* Is the Msi present? */
+			if (MsiGetProductCode(componentStr, productCode) != ERROR_SUCCESS) {
+				FreeLibrary(hInst);
+				return "";
+			}
+			TCHAR path[2048];
+			DWORD szPath = sizeof(path);
+			path[0] = '\0';
+			MsiGetComponentPath(productCode, componentStr, path, &szPath);
+			FreeLibrary(hInst);
+			return path;
+		}
+		FreeLibrary(hInst);
+	}
+	return "";
 #else /* _WIN32 */
 	return String();
 #endif /* _WIN32 */



More information about the icinga-checkins mailing list