0

I use the same code in custom action wix and console app. In custom action it dont return polish symbol "ł" and "ą" in custom action. Replaces them with other symbols or spaces. In console app it works well.

Already in the "message" variable there isnt polish letters.

    private static void RunTest(Session session)
    {
        try
        {
            Process p = CreateProcess();
            p.StartInfo.FileName = "....exe"; ;
            p.StartInfo.Arguments = "-t";

            string message = "";
            int errorCount = 0;
            p.OutputDataReceived += (sender, args) =>
            {
                if (args.Data == null)
                    return;
                message += args.Data;
                message += "\n";
            };
            p.ErrorDataReceived += (sender, args) =>
            {
                if (args.Data == null)
                    return;
                errorCount++;
                message += args.Data;
                message += "\n";
            };

            p.Start();
            p.BeginOutputReadLine();
            p.WaitForExit();
            SaveNewRtf(session, message);
        }
        catch (Exception)
        {
        }
    }

    private static Process CreateProcess()
    {
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        return p;
    }

Edit:

This is happen because Massages from Process are in Unicode. But unfortunately I don't know how repair that. I changed encoding to utf-8 for messages in Program I run by Process and still the message are download in Unicode.

Silny ToJa
  • 1,815
  • 1
  • 6
  • 20
  • 1
    This might be something different ([code pages etc...](https://stackoverflow.com/a/49605354/129130)), but off the top of my head: [try saving the RTF with Wordpad](https://stackoverflow.com/a/6380750/129130) before trying anything else. – Stein Åsmul Jul 03 '20 at 10:28
  • @SteinÅsmul I didnt add I am create my file .rtf in custom action and repleces Doc.rtf. I am testing this what you send. And it look like there isnt problem with save but with read message. I use Process and read message from command line. And it dont return polish letters "ł" and "ą" in message. I copy this script to new project console app .Net Framework, and it return this polish letters. Both programs have encoding="utf-8". – Silny ToJa Jul 03 '20 at 11:26

1 Answers1

1

I solved it by

p.StartInfo.StandardOutputEncoding = OemEncoding.GetDefaultOemCodePageEncoding();

and

public static class OemEncoding
{

    private const Int32 MAX_DEFAULTCHAR = 2;

    private const Int32 MAX_LEADBYTES = 12;

    private const Int32 MAX_PATH = 260;

    private const UInt32 CP_OEMCP = 1;

    public static Encoding GetDefaultOemCodePageEncoding()

    {

        CPINFOEX cpInfoEx;

        if (GetCPInfoEx(CP_OEMCP, 0, out cpInfoEx) == 0)

            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,

                                                              "GetCPInfoEx() failed with error code {0}",

                                                              Marshal.GetLastWin32Error()));

        return Encoding.GetEncoding((int)cpInfoEx.CodePage);

    }

    [DllImport("Kernel32.dll", EntryPoint = "GetCPInfoExW", SetLastError = true)]

    private static extern Int32 GetCPInfoEx(UInt32 CodePage, UInt32 dwFlags, out CPINFOEX lpCPInfoEx);

    [StructLayout(LayoutKind.Sequential)]

    private unsafe struct CPINFOEX

    {

        internal UInt32 MaxCharSize;

        internal fixed Byte DefaultChar[MAX_DEFAULTCHAR];

        internal fixed Byte LeadByte[MAX_LEADBYTES];

        internal Char UnicodeDefaultChar;

        internal UInt32 CodePage;

        internal fixed Char CodePageName[MAX_PATH];

    }
}
Silny ToJa
  • 1,815
  • 1
  • 6
  • 20