Is it possible to detect if an application has an console window? Either by have used AllocConsole or if it's a regular console application.
Edit:
Solution (thanks to ho1's answer):
public static class ConsoleDetector
{
private const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;
private const int ERROR_ACCESS_DENIED = 5;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeConsole();
/// <summary>
/// Gets if the current process has a console window.
/// </summary>
public static bool HasOne
{
get
{
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
FreeConsole();
return false;
}
//If the calling process is already attached to a console,
// the error code returned is ERROR_ACCESS_DENIED
return Marshal.GetLastWin32Error() == ERROR_ACCESS_DENIED;
}
}
}