Skip to content

Instantly share code, notes, and snippets.

@hasokeric
Created March 23, 2015 18:58
Show Gist options
  • Save hasokeric/e9230a53a8a9b280b465 to your computer and use it in GitHub Desktop.
Save hasokeric/e9230a53a8a9b280b465 to your computer and use it in GitHub Desktop.
EpiConsole shows a Command Prompt Window for Debugging purposes
internal static class EpiConsole
{
public static void Start()
{
IntPtr hw = GetConsoleWindow();
if (hw == IntPtr.Zero) AllocConsole();
Console.Clear();
ShowWindow(hw, 8);
Debug.Listeners.Clear();
TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);
}
public static void Close()
{
IntPtr hw = GetConsoleWindow();
if (hw != IntPtr.Zero) ShowWindow(hw, 0);
}
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
internal static extern int AllocConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
internal static extern int FreeConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
@tdcbm
Copy link

tdcbm commented Mar 7, 2018

Hi @hasokeric,

How to use this class in epicor form customization?

@hasokeric
Copy link
Author

hasokeric commented Jun 1, 2018

Hello @tdcbm you should be able just to call EpiConsole.Start(); and put the class above your class Script {}

internal static class EpiConsole
{
	public static void Start()
	{
		IntPtr hw = GetConsoleWindow();
		if (hw == IntPtr.Zero) AllocConsole();
		Console.Clear();
		ShowWindow(hw, 8);
		Debug.Listeners.Clear();
		TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
		Debug.Listeners.Add(writer);
	}

	public static void Close()
	{
		IntPtr hw = GetConsoleWindow();
		if (hw != IntPtr.Zero) ShowWindow(hw, 0);
	}

	[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
	internal static extern int AllocConsole();
	[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
	internal static extern int FreeConsole();
	[System.Runtime.InteropServices.DllImport("kernel32.dll")]
	private static extern IntPtr GetConsoleWindow();
	[System.Runtime.InteropServices.DllImport("user32.dll")]
	static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}


public class Script
{
	private void HHPackOutForm_Load(object sender, EventArgs args)
	{
		EpiConsole.Start(); // LAUNCH DEBUG CONSOLE
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment