#include <windows.h>
#include <msi.h>
#include <msiquery.h>
#include <tchar.h>

//!	GUIDの長さ
#define	GUID_LEN				(38+1)

/*!
	DllMain
*/
BOOL APIENTRY DllMain(HANDLE /*hModule*/, DWORD  /*ul_reason_for_call*/,  LPVOID /*lpReserved*/)
{
  return TRUE;
}


/*!
	他のプロダクトを検索
	<Property Id="FIND_OTHER_PRODUCT_FIND_UPGRADE_CODE">{FIND_GUID}</Property>
*/
UINT __stdcall FindOtherProductByUpgradeCode(MSIHANDLE hInstall)
{
	//	プロパティを取得
	TCHAR	findUpgradeCode[GUID_LEN] = {0};
	DWORD	findUpgradeCodeLen = GUID_LEN;
	if(::MsiGetProperty(hInstall, _T("FIND_OTHER_PRODUCT_FIND_UPGRADE_CODE"), findUpgradeCode, &findUpgradeCodeLen) != ERROR_SUCCESS)
		return 1;

	//	検索を実行
	TCHAR oldProductCode[GUID_LEN] = {0};
	UINT ret = ::MsiEnumRelatedProducts(findUpgradeCode, 0, 0, oldProductCode);
	if(ret != ERROR_SUCCESS)
	{
		if(ret == ERROR_NO_MORE_ITEMS)
		{
			::MsiSetProperty(hInstall, _T("FIND_OTHER_PRODUCT_RESULT"), _T(""));
			return 0;
		}
		else
			return 1;
	}

	//	プロパティを設定
	::MsiSetProperty(hInstall, _T("FIND_OTHER_PRODUCT_RESULT_PRODUCT_CIDE"), oldProductCode);
	::MsiSetProperty(hInstall, _T("FIND_OTHER_PRODUCT_RESULT"), _T("TRUE"));

	return 0;
}