[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
Mon Aug 8 15:55:36 CEST 2016


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

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 |   34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp
index 94a0f01..b4172bd 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,13 +297,40 @@ void ScriptUtils::Assert(const Value& arg)
 String ScriptUtils::MsiGetComponentPathShim(const String& component)
 {
 #ifdef _WIN32
+	typedef long(WINAPI *pMsiGetProductCode)(
+		TCHAR szProduct[39],
+		TCHAR lpProductBuf[39]);
+
+	typedef long(WINAPI *pMsiGetComponentPath)(
+		TCHAR* szProductCode,
+		TCHAR* szComponent,
+		TCHAR* lpPathBuf,
+		DWORD *path);
+
+	HINSTANCE hInst = LoadLibrary("msi.dll");
+	if (!hInst){
+		return "";
+	}
+	pMsiGetProductCode MsiGetProductCode = (pMsiGetProductCode)GetProcAddress(hInst, "MsiGetProductCodeA");
+	pMsiGetComponentPath MsiGetComponentPath = (pMsiGetComponentPath)GetProcAddress(hInst, "MsiGetComponentPathA");
+
+	if (!MsiGetProductCode && !MsiGetComponentPath) {
+		FreeLibrary(hInst);
+		return "";
+	}
 	TCHAR productCode[39];
-	if (MsiGetProductCode(component.CStr(), productCode) != ERROR_SUCCESS)
+	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, component.CStr(), path, &szPath);
+	MsiGetComponentPath(productCode, componentStr, path, &szPath);
+	FreeLibrary(hInst);
 	return path;
 #else /* _WIN32 */
 	return String();



More information about the icinga-checkins mailing list