PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] how to save a binary output of a Process and save it to file

 August 18, 2022     asynchronous, binary, c#, output, process     No comments   

Issue

I'm running an exe file through Process, which generates binary data, and I want to save that data into a file.

while using usual proposed methods of doing so (Sync and async) I've ran across couple of hurdles, and I hope you can help me out.

I've run the commands through code, and through prompt, and here are my observations

1) When using sync methods, the file is by default encoded with UTF-8 (File ends up larger than the prompt one 2) when changing the encoding method to ASCII/default the encoding seems to be handled properly and file seems to be similar in size to the prompt one, unfortunately the process designed to read that file considers it "errored" 3) using async method and saving using BinaryWriter creates a seemingly accurate file, but still cant be read by output. I believe my error might be how I append "new line"

below is my code for async method. While sync one just has "Output.ReadToEnd()" between start/stop.

code:

        private void createOCTree()
    {
        //Create process
        var cmd = @"oconv.exe";                         //OUTPUT IS C++ binary
        var arguments = @" C:\RadianceGeometry.rad";
        var outputFile = @"C:\test.oct";
        var workingDir = m_FileData.TemporaryFilesFolder;

        ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
        cmdStartInfo.RedirectStandardInput = false;
        cmdStartInfo.RedirectStandardError = false;
        cmdStartInfo.RedirectStandardOutput = true;
        cmdStartInfo.UseShellExecute = false;
        cmdStartInfo.CreateNoWindow = true;
        cmdStartInfo.FileName = cmd;                    ////file name
        cmdStartInfo.Arguments = arguments;             ////arguments
        cmdStartInfo.WorkingDirectory = @"C:\";         ////working dir

        using (StreamWriter writer = new StreamWriter("C:\\testStream.oct"))
        {
        }

        var process = new Process();
        process.StartInfo = cmdStartInfo;

        process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
        process.Start();
        process.BeginOutputReadLine();
        process.WaitForExit();
    }

    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        try
        {
            byte[] bytes = Encoding.Default.GetBytes(outLine.Data);
            byte[] newLine = Encoding.Default.GetBytes("\n");                       
            AppendData("C:\\testStream.oct", bytes);
            AppendData("C:\\testStream.oct", newLine);     ////otherwise all is in one line     
        }
        catch { }   ////possibly last line is not properly handled
    }

    private static void AppendData(string filename, byte[] bits)
    {
        using (var fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None))
        using (var bw = new BinaryWriter(fileStream))
        {
            bw.Write(bits);
        }
    }

my suspicion is that either I'm missing something with my "Try/Catch", or that newlines should be parsed differently. Set all paths to c:\ just to make it easier to read (instead of using variables, or actual paths).

edit:addition; I've noticed when comparing prompt output with the one from code, and they are the same, except that first char of some lines "output" is "?" instead of random "glyphs" in the working file.

edit: added comparison screen shots. code comparison:

with newline: https://ibb.co/HpztQ0Q

without newline: https://ibb.co/Lvcr9pH


Solution

the way I Got it to work is use cmd as the command, and in the argument field, I used /c" flag as described here: What does cmd /C mean?

    String exeFileName = @"cmd.exe";
    String Arguments = @"/c oconv.exe C:\test\radgeom.rad > c:\test\scene.oct"; 


Answered By - Lilith5th
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing