WiX: Extracting a binary string in a custom action yields a string like "good data"

I just ran into strange behavior when trying to extract a row from a binary table in MSI.

I have a file containing Hello world

, the data I receive is this ???Hello world

. (Literary question mark.)

Is this how it was supposed?
Will it always be exactly 3 characters at the beginning?


Sample code:
[CustomAction]
public static ActionResult CustomAction2(Session session)
{
    View v = session.Database.OpenView("SELECT `Name`,`Data` FROM `Binary`");
    v.Execute();

    Record r = v.Fetch();
    int datalen = r.GetDataSize("Data");
    System.IO.Stream strm = r.GetStream("Data");
    byte[] rawData = new byte[datalen];
    int res = strm.Read(rawData, 0, datalen);
    strm.Close();

    String s = System.Text.Encoding.ASCII.GetString(rawData);
    // s == "???Hello World"

    return ActionResult.Success;
}

      

+2


a source to share


2 answers


Surprisingly, if you created the file using Notepad, could you just be byte in byte order ?



+1


a source


Try

String s = System.Text.Encoding.UTF8.GetString(rawData);
if (s.Length > 0 && s[0] == '\uFEFF')
{
    s = s.Substring(1);
}

      



instead String s = System.Text.Encoding.ASCII.GetString(rawData);

0


a source







All Articles