在使用MSI互操作API时,我遇到了一些不寻常的行为,导致我的应用程序崩溃。这是足够简单的‘处理’的问题,但我想知道更多的‘为什么’这是发生。
第一次对MSIEnumRelatedProducts的调用返回值为0,并正确地将字符串缓冲区设置为产品代码。我的理解是,只有当给定的升级解码程序(作为方法的parm传递给该方法)当前安装了“相关的家庭产品”时才会发生这种情况,否则它将返回259个ERROR_NO_MORE_ITEMS。
但是,当我随后使用相同的产品代码调用MSIGetProductInfo时,我得到返回值1605,“此操作仅对当前已安装的产品有效”。
有没有人知道在什么情况下会发生这种情况?它在1台机器上是100%可重复的,但我还没有设法在另一台机器上获得复制步骤。
我们所有的产品都是用Wix属性"AllUsers=1“构建的,所以应该为所有用户安装产品,而不仅仅是一个。
任何想法/建议都很感激。
谢谢本
更新:--我注意到,在运行带有日志的问题msi包时,将显示如下行:
MSI (s) (88:68) 12:15:50:235: FindRelatedProducts:无法读取产品{840 C.等.96}的信息。跳过..。
有人知道这可能意味着什么吗?
更新:代码示例.
do
{
result = _MSIApi.EnumRelatedProducts(upgradeCode.ToString("B"), 0,
productIndex, productCode);
if (result == MSIApi.ERROR_BAD_CONFIGURATION ||
result == MSIApi.ERROR_INVALID_PARAMETER ||
result == MSIApi.ERROR_NOT_ENOUGH_MEMORY)
{
throw new MSIInteropException("Failed to check for related products",
new Win32Exception((Int32)result));
}
if(!String.IsNullOrEmpty(productCode.ToString()))
{
Int32 size = 255;
StringBuilder buffer = new StringBuilder(size);
Int32 result = (Int32)_MSIApi.GetProductInfo(productCode,
MSIApi.INSTALLPROPERTY_VERSIONSTRING,
buffer,
ref size);
if (result != MSIApi.ERROR_SUCCESS)
{
throw new MSIInteropException("Failed to get installed version",
new Win32Exception(result));
}
version = new Version(buffer.ToString());
}
productCode = new StringBuilder(39);
productIndex++;
}
while (result == MSIApi.ERROR_SUCCESS);
发布于 2010-10-25 12:19:49
我假设您尝试使用MsiGetProductInfo获取文档中描述的其他属性。例如,您可以以没有任何问题的方式获得"PackageCode"
属性(INSTALLPROPERTY_PACKAGECODE
)的值,但不能获得"UpgradeCode"
属性相对于MsiGetProductInfo的值并接收错误1605 (ERROR_UNKNOWN_PRODUCT
)。
更新了:好的,现在我明白你的问题了。如何在互联网上找到MsiGetProductInfo中的一个bug,所以它并不总是有效的。有时它会返回1605 (ERROR_UNKNOWN_PRODUCT
)或1608 (ERROR_UNKNOWN_PROPERTY
)。在这种情况下,唯一的解决办法是手动获取version属性。我可以使用MicrosoftOfficeOutlook2010MUI (UpgradeCode =“{00140000-001A-0000-0000000FF1CE}”)复制您在我的计算机上描述的问题,并编写了一个从注册表获得产品版本的解决方案。在这个示例中,我只从HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products
获得信息。如果您对不仅为所有用户安装的产品感兴趣,您必须修改程序。这是代码
using System;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace EnumInstalledMsiProducts {
internal static class NativeMethods {
internal const int MaxGuidChars = 38;
internal const int NoError = 0;
internal const int ErrorNoMoreItems = 259;
internal const int ErrorUnknownProduct = 1605;
internal const int ErrorUnknownProperty = 1608;
internal const int ErrorMoreData = 234;
[DllImport ("msi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int MsiEnumRelatedProducts (string lpUpgradeCode, int dwReserved,
int iProductIndex, //The zero-based index into the registered products.
StringBuilder lpProductBuf); // A buffer to receive the product code GUID.
// This buffer must be 39 characters long.
// The first 38 characters are for the GUID, and the last character is for
// the terminating null character.
[DllImport ("msi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern Int32 MsiGetProductInfo (string product, string property,
StringBuilder valueBuf, ref Int32 cchValueBuf);
}
class Program {
static int GetProperty(string productCode, string propertyName, StringBuilder sbBuffer) {
int len = sbBuffer.Capacity;
sbBuffer.Length = 0;
int status = NativeMethods.MsiGetProductInfo (productCode,
propertyName,
sbBuffer, ref len);
if (status == NativeMethods.ErrorMoreData) {
len++;
sbBuffer.EnsureCapacity (len);
status = NativeMethods.MsiGetProductInfo (productCode, propertyName, sbBuffer, ref len);
}
if ((status == NativeMethods.ErrorUnknownProduct ||
status == NativeMethods.ErrorUnknownProperty)
&& (String.Compare (propertyName, "ProductVersion", StringComparison.Ordinal) == 0 ||
String.Compare (propertyName, "ProductName", StringComparison.Ordinal) == 0)) {
// try to get vesrion manually
StringBuilder sbKeyName = new StringBuilder ();
sbKeyName.Append ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\");
Guid guid = new Guid (productCode);
byte[] buidAsBytes = guid.ToByteArray ();
foreach (byte b in buidAsBytes) {
int by = ((b & 0xf) << 4) + ((b & 0xf0) >> 4); // swap hex digits in the byte
sbKeyName.AppendFormat ("{0:X2}", by);
}
sbKeyName.Append ("\\InstallProperties");
RegistryKey key = Registry.LocalMachine.OpenSubKey (sbKeyName.ToString ());
if (key != null) {
string valueName = "DisplayName";
if (String.Compare (propertyName, "ProductVersion", StringComparison.Ordinal) == 0)
valueName = "DisplayVersion";
string val = key.GetValue (valueName) as string;
if (!String.IsNullOrEmpty (val)) {
sbBuffer.Length = 0;
sbBuffer.Append (val);
status = NativeMethods.NoError;
}
}
}
return status;
}
static void Main () {
string upgradeCode = "{00140000-001A-0000-0000-0000000FF1CE}";
StringBuilder sbProductCode = new StringBuilder (39);
StringBuilder sbProductName = new StringBuilder ();
StringBuilder sbProductVersion = new StringBuilder (1024);
for (int iProductIndex = 0; ; iProductIndex++) {
int iRes = NativeMethods.MsiEnumRelatedProducts (upgradeCode, 0, iProductIndex, sbProductCode);
if (iRes != NativeMethods.NoError) {
// NativeMethods.ErrorNoMoreItems=259
break;
}
string productCode = sbProductCode.ToString();
int status = GetProperty (productCode, "ProductVersion", sbProductVersion);
if (status != NativeMethods.NoError) {
Console.WriteLine ("Can't get 'ProductVersion' for {0}", productCode);
}
status = GetProperty (productCode, "ProductName", sbProductName);
if (status != NativeMethods.NoError) {
Console.WriteLine ("Can't get 'ProductName' for {0}", productCode);
}
Console.WriteLine ("ProductCode: {0}{3}ProductName:'{1}'{3}ProductVersion:'{2}'{3}",
productCode, sbProductName, sbProductVersion, Environment.NewLine);
}
}
}
}
它在我的计算机上产生正确的输出
ProductCode: {90140000-001A-0407-0000-0000000FF1CE}
ProductName:'Microsoft Office Outlook MUI (German) 2010'
ProductVersion:'14.0.4763.1000'
ProductCode: {90140000-001A-0419-0000-0000000FF1CE}
ProductName:'Microsoft Office Outlook MUI (Russian) 2010'
ProductVersion:'14.0.4763.1000'
而不是以前ProductVersion
中的错误。
发布于 2010-10-26 11:29:00
您应该查看的部署工具基础。它有一个非常成熟的MSI ( Microsoft.Deployment.WindowsInstaller ),这将使编写和测试这段代码更加容易。
我看到您已经有了WiX (希望是v3+ ),所以在C:\Program \WindowsInstallerXMLv3SDK文件夹中查找它。
https://stackoverflow.com/questions/4013425
复制相似问题