public static void Open() { #if UNITY_STANDALONE_WIN #pragma warning disable 0618 if (realOut == null) { realOut = Console.Out; } var hasConsole = PInvoke.AttachConsole(0x0ffffffff); if (hasConsole == false) { PInvoke.AllocConsole(); } try { // grab handle ptr var outHandlePtr = PInvoke.GetStdHandle(PInvoke.STD_OUTPUT_HANDLE); // we can then create a filestream from this handle var fileStream = new FileStream(outHandlePtr, FileAccess.Write); // and then create a new stream writer (using ASCII) var stdOut = new StreamWriter(fileStream, Encoding.ASCII); stdOut.AutoFlush = true; // and force unity to use this Console.SetOut(stdOut); } catch (System.Exception e) { Debug.Log(e); } #pragma warning restore 0618 #endif }
private static void InitConsole() { PInvoke.AllocConsole(); ConsoleHwnd = PInvoke.GetConsoleWindow(); #if RELEASE // hide console window PInvoke.ShowWindow(ConsoleHwnd, SHOW_WINDOW_CMD.SW_HIDE); #endif }
public static void AllocDebugConsole() { if (fConsoleAllocated) { return; } const int STD_OUTPUT_HANDLE = -11; const int MY_CODE_PAGE = 437; PInvoke.AllocConsole(); IntPtr stdHandle = PInvoke.GetStdHandle(STD_OUTPUT_HANDLE); SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true); FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write); Encoding encoding = Encoding.GetEncoding(MY_CODE_PAGE); StreamWriter standardOutput = new StreamWriter(fileStream, encoding); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); fConsoleAllocated = true; }