我需要知道Installer.OnCommitted回调中的应用程序的ProductCode。似乎没有明显的方法来确定这一点。
答案 0 :(得分:2)
您可以使用CustomActionData属性中的/ productCode = [ProductCode]来避免对产品代码进行硬编码。
答案 1 :(得分:1)
我最终使用Visual Studio中的CustomActionData属性将产品代码作为命令行参数传递给我的Installer类(例如/ productcode = {31E1145F-B833-47c6-8C80-A55F306B8A6C}。 然后,我可以使用Context.Parameters StringDictionary
从Installer类中的任何回调中访问它string productCode = (string)Context.Parameters["productcode"];
答案 2 :(得分:0)
MSI函数MsiGetProperty可用于获取ProductCode属性的名称。我不知道在这种情况下是否可行,因为我从未创建过.NET安装程序。
答案 3 :(得分:0)
@Chris Tybur 的建议似乎有效
这是我的 C# 代码:
public static string GetProductCode(string fileName)
{
IntPtr hInstall = IntPtr.Zero;
try
{
uint num = MsiOpenPackage(fileName, ref hInstall);
if ((ulong)num != 0)
{
throw new Exception("Cannot open database: " + num);
}
int pcchValueBuf = 255;
StringBuilder szValueBuf = new StringBuilder(pcchValueBuf);
num = MsiGetProperty(hInstall, "ProductCode", szValueBuf, ref pcchValueBuf);
if ((ulong)num != 0)
{
throw new Exception("Failed to Get Property ProductCode: " + num);
}
return szValueBuf.ToString();
}
finally
{
if (hInstall != IntPtr.Zero)
{
MsiCloseHandle(hInstall);
}
}
}
[DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiGetPropertyW", ExactSpelling = true, SetLastError = true)]
private static extern uint MsiGetProperty(IntPtr hInstall, string szName, [Out] StringBuilder szValueBuf, ref int pchValueBuf);
[DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiOpenPackageW", ExactSpelling = true, SetLastError = true)]
private static extern uint MsiOpenPackage(string szDatabasePath, ref IntPtr hProduct);
[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
private static extern int MsiCloseHandle(IntPtr hAny);
FWIW:这个 MSDN 站点上有一个可能引起关注的小信息:https://docs.microsoft.com/en-us/windows/win32/msi/obtaining-context-information-for-deferred-execution-custom-actions
Function Description
MsiGetProperty Supports a limited set of properties when used with deferred execution custom actions:
the CustomActionData property, ProductCode property, and UserSID property.Commit custom
actions cannot use the MsiGetProperty function to obtain the ProductCode property.
Commit custom actions can use the CustomActionData property to obtain the product code.
注意对 cannot use the MsiGetProperty function to obtain the ProductCode property
的调用。所以YMMV。
查看 How can I find the product GUID of an installed MSI setup?,您可以使用 COM API 收集此信息(当前版本显示 VBScript),这也可能值得检查。