WIX 在自定义操作中编辑二进制文件

标签 wix wix3.5 wix3.6 wix-extension wixlib

我正在尝试执行以下操作:

  1. 在安装过程中通过自定义操作打开和编辑 sql 文件
  2. 保存编辑的更改并在安装过程中执行。

在我的product.wxs中,我有以下内容:

<Binary Id="SqlScriptSQLAuthentication"  SourceFile="$(sys.SOURCEFILEDIR)\MyDb.sql" />

        <Component Id='SqlComponent.SQLAuthentication' Guid='665D641C-3570-4b96-9CA5-2B4C12594A35' KeyPath='yes'>
            <Condition><![CDATA[USEINTEGRATEDSECURITY<>1]]></Condition>
            <sql:SqlDatabase Id='SqlDatabase.SQLAuthentication' Database='[DATABASE_NAME]' User='SQLUser' Server='[DATABASE_SERVER]' CreateOnInstall='yes' DropOnUninstall='yes' ContinueOnError='no' />
            <sql:SqlScript Id='SqlScriptSQLAuthentication' BinaryKey='SqlScriptSQLAuthentication' SqlDb='SqlDatabase.SQLAuthentication' ExecuteOnInstall='yes' />
        </Component>

在安装过程中,我想编辑“MyDb.sql”,将更改写入其中并将其保存回来,以便wix可以在安装过程中运行它。

最好的方法是什么? 谢谢

编辑:

MyDb.sql 文件:

CREATE TABLE Test12345 (Value1 CHAR(50), Value2 INTEGER)

在我的自定义操作中,我有以下内容:

        View v = session.Database.OpenView("SELECT `Data` FROM `Binary` WHERE `Name` = '{0}'", binaryKeyName);

        v.Execute();

        var IsReadOnly = session.Database.IsReadOnly;

        Record r = v.Fetch();

        StreamReader reader = new StreamReader(r.GetStream("Data"));
        string text = reader.ReadToEnd();
        text = text.Replace(@"Test12345", "TTTest");

        byte[] byteArray = Encoding.ASCII.GetBytes(text);
        MemoryStream stream = new MemoryStream(byteArray);

        r.SetStream("Data", stream);

//到目前为止,它可以工作,我从 .sql 文件中读取了我的 sql 文本

    session.Database.ExecuteStringQuery("UPDATE `Binary` SET `Data` = '{0}' WHERE `Name` = '{1}')", text, binaryKeyName);

    v.Close();
    session.Database.Commit();

当我尝试更新时(不确定我是否正确),它失败了。

最佳答案

您可以使用此自定义操作来提取二进制数据。然后您必须进行更改并重新保存它。不知道如何将其重新保存回二进制文件,因为我以前没有这样做过 - 我已经用它来将临时数据流式传输到许可协议(protocol)中。这应该会给你一个好的开始。

HRESULT ExtractBinary(__in LPCWSTR wzBinaryId,
                      __out BYTE** pbData,
                      __out DWORD* pcbData)
{
    HRESULT hr = S_OK;
    LPWSTR pwzSql = NULL;
    PMSIHANDLE hView;
    PMSIHANDLE hRec;

    // make sure we're not horked from the get-go
    hr = WcaTableExists(L"Binary");
    if (S_OK != hr)
    {
        if (SUCCEEDED(hr))
        {
            hr = E_UNEXPECTED;
        }
        ExitOnFailure(hr, "There is no Binary table.");
    }

    ExitOnNull(wzBinaryId, hr, E_INVALIDARG, "Binary ID cannot be null");
    ExitOnNull(*wzBinaryId, hr, E_INVALIDARG, "Binary ID cannot be empty string");

    hr = StrAllocFormatted(&pwzSql, L"SELECT `Data` FROM `Binary` WHERE `Name`=\'%s\'", wzBinaryId);
    ExitOnFailure(hr, "Failed to allocate Binary table query.");

    hr = WcaOpenExecuteView(pwzSql, &hView);
    ExitOnFailure(hr, "Failed to open view on Binary table");

    hr = WcaFetchSingleRecord(hView, &hRec);
    ExitOnFailure(hr, "Failed to retrieve request from Binary table");

    hr = WcaGetRecordStream(hRec, 1, pbData, pcbData);
    ExitOnFailure(hr, "Failed to read Binary.Data.");

    LExit:
    ReleaseStr(pwzSql);

    return hr;
}

关于WIX 在自定义操作中编辑二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14518786/

相关文章:

Wix:设置安装程序的版本

windows - 如何防止 WiX 在打补丁时删除注册表项

wix - InstallFinalize 后使用提升的权限运行自定义操作

未为静默安装设置 Wix 目录 ID 符号值

wix - 你怎么能_创建_一个Wix中的文本文件?

msbuild - 如何修复 WiX 3.5.1811.0 的 HEAT5301 错误

c# - 我怎么知道exe的安装路径?

WiX:在 CustomAction 期间动态更改状态文本

WIX 和 NAnt : Unable to build WIX source code

wix - 删除许可证对话框