Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure a console has been allocated before using it #948

Merged
merged 2 commits into from
Feb 13, 2017
Merged

Ensure a console has been allocated before using it #948

merged 2 commits into from
Feb 13, 2017

Conversation

BillHally
Copy link
Contributor

When an exception is raised by Program.main, Program.Main will attempt to write it out to the console before returning with the exit code of -1.

However, if the console has not yet been allocated, then the user cannot see the error. This pull request ensures that the console has been allocated before the exception is written out to the console.

@anaisbetts
Copy link
Contributor

This means that when things fail, random DOS boxes pop up, that seems Bad. We only want to call ensureConsole in places where we know the command was invoked by a developer, not as part of the installation

…ens when the program is invoked by a developer
@BillHally
Copy link
Contributor Author

That's a good point.

I've moved the call to ensureConsole to the start of Releasify.

@anaisbetts anaisbetts merged commit 04cfe50 into Squirrel:master Feb 13, 2017
@anaisbetts
Copy link
Contributor

👍

@lukeskinner
Copy link
Contributor

In my testing of this I can't get any console messages to appear. The following code is present in the ensureConsole() method:

NativeMethods.GetStdHandle(StandardHandles.STD_ERROR_HANDLE);
NativeMethods.GetStdHandle(StandardHandles.STD_OUTPUT_HANDLE);

The GetStdHandle returns a IntPtr which is never used.

I got it working by adding the following for both handles - I don't know if this is the desired way of handling this so I haven't submitted a PR for it yet:

            IntPtr stdHandle = NativeMethods.GetStdHandle(StandardHandles.STD_OUTPUT_HANDLE);
            SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
            FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
            Encoding encoding = Encoding.Default;
            StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
            standardOutput.AutoFlush = true;
            Console.SetOut(standardOutput);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants