0

我试图从 C++ 自定义操作中获取“UILevel”MSI 属性,以确定用户是否在“无 UI 模式”下运行,但运气不佳。我正在调用的函数是从我在我的 DLL 中导出的函数(可能是“延迟”或“第一序列”操作)中传递给 MSIHANDLE 的。我看到的是它MsiGetPropertyW总是返回ERROR_MORE_DATA并且该trueLength字段始终为0。这是我的代码:

bool runningInNoUIMode(MSIHANDLE hInstall)
{
    unsigned long nBufLen = 64UL;
    WCHAR *wszValue = new WCHAR[nBufLen];

    DWORD trueLength = 0UL;
    UINT result = ::MsiGetPropertyW(hInstall, L"UILevel", L"", &trueLength); // Get the size of the property value first to see if there is enough storage allocated.
    if (ERROR_MORE_DATA == result || nBufLen <= trueLength)
    {
        if (NULL != wszValue)
        {
            delete [] wszValue;
        }

        // Allocate more memory for the property adding one for the null terminator.
        nBufLen = trueLength + 1;
        wszValue = new WCHAR[nBufLen];
    }

    if (NULL == wszValue)
    {
        WcaLog(LOGMSG_STANDARD, "Unable to determine the user interface level the MSI is being run with because we were unable to allocate storage for accessing the 'UILevel' property.");
        return false;
    }

    memset(wszValue, L'\0', nBufLen * sizeof(WCHAR));
    result = ::MsiGetPropertyW(hInstall, L"UILevel", wszValue, &trueLength);
    if (ERROR_SUCCESS != result)
    {
        WcaLog(LOGMSG_STANDARD, "Unable to determine the user interface level the MSI is being run with, error code = '%lu'.", result);
        delete [] wszValue;
        return false;
    }

    if (0 == wcscmp(L"2", wszValue)) // INSTALLUILEVEL_NONE == 2
    {
        delete [] wszValue;
        return true;
    }

    delete [] wszValue;
    return false;
}

我相信我现在可以通过通过 WiX 传递“UILevel”属性并在 C++ 中以这种方式检查它来解决这个问题,但我很好奇这里的问题是什么。

我在带有 WiX 3.5.2519 的 Windows 7 上使用 Visual Studio/Visual C++ 2010。

感谢您提供的任何帮助!

4

2 回答 2

1

感谢@DanielGehriger,我们发现问题不在于代码,而在于自定义操作的调度。运行自定义操作时,UILevelMSI 属性根本不可用deferred(我发现代码对于计划为的自定义操作正常工作firstsequence)。通过使用 WiX 将自定义操作数据显式传递给它,我已经解决了这个限制:

<CustomAction Id="CustomAction.SetProperty" Property="CustomActionCall"
  Value="UILEVEL=[UILevel];" />

然后在 C++ 中使用WcaIsPropertySetand来检查这个WcaGetProperty。请注意,方括号之间的属性名称的字符大小写在这里很重要。

于 2012-10-26T21:53:38.833 回答
1

另一种更简单的方法是使用MsiEvaluateCondition 函数

BOOL bUI = MsiEvaluateCondition(L"UILevel<3");

在 C# 中使用 Microsoft.Deployment.WindowsIntaller (DTF) 它是:

var uiLevel = session["UILevel"];

在 C++ 中,MsiGetProperty 函数有一个示例:

UINT __stdcall MyCustomAction(MSIHANDLE hInstall)
{
    TCHAR* szValueBuf = NULL;
    DWORD cchValueBuf = 0;
    UINT uiStat =  MsiGetProperty(hInstall, TEXT("MyProperty"), TEXT(""), &cchValueBuf);
    //cchValueBuf now contains the size of the property's string, without null termination
    if (ERROR_MORE_DATA == uiStat)
    {
        ++cchValueBuf; // add 1 for null termination
        szValueBuf = new TCHAR[cchValueBuf];
        if (szValueBuf)
        {
            uiStat = MsiGetProperty(hInstall, TEXT("MyProperty"), szValueBuf, &cchValueBuf);
        }
    }
    if (ERROR_SUCCESS != uiStat)
    {
        if (szValueBuf != NULL) 
           delete[] szValueBuf;
        return ERROR_INSTALL_FAILURE;
    }

    // custom action uses MyProperty
    // ...

    delete[] szValueBuf;

    return ERROR_SUCCESS;
}
于 2012-10-26T19:56:48.033 回答