Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created September 25, 2017 10:29
Show Gist options
  • Save hisasann/11565cf2be47e71f18fa8882328d3a27 to your computer and use it in GitHub Desktop.
Save hisasann/11565cf2be47e71f18fa8882328d3a27 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AttachConsoleSample
{
class Program
{
[DllImport("kernel32.dll")]
public static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
private bool writeConsole( string msg ) {
if ( !AttachConsole( System.UInt32.MaxValue ) ) {
return false;
}
// stdoutのストリームを取得
System.IO.Stream stream = System.Console.OpenStandardOutput();
System.IO.StreamWriter stdout = new System.IO.StreamWriter( stream );
// 指定された文字列を出力
stdout.Write( msg );
stdout.Flush();
FreeConsole();
return true;
}
static void Main(string[] args)
{
writeConsole("出力!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment