I have used this method before, but now I am stuck. I have a Windows Forms application (actually a Windows Service) that can be run as an "interactive" application. In this case, I am using AllocConsole() to create a console for the application. The console is created, I see it on the taskbar, but it is only partially available to the application.
NativeMethods.AllocConsole(); // console window is created, at this point GetConsoleWindow() is returning nonzero handle
Console.Title = $"{service.ServiceName} console mode log"; // Console title is set
Console.SetBufferSize(140, 100); // Exception is thrown: "The handle is invalid."
Stack trace is:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
at System.Console.GetBufferInfo()
at System.Console.SetBufferSize(Int32 width, Int32 height)
at PCSService.PCSService.Main(String[] args) in D:\Workspace\SolutionFolder\ProjectName\ServiceRunner.cs:line 84
When this occurred before, I have deleted the key from the HKCU/Console, but there is nothing now to delete. I can't imagine, that it is related to the fact that I have switched from .net 4.6 to .net 4.7, but who knows...
Update:
On the very same machine, a solution that is using the exact same code for this, but compiled with VS2015 targeting .net 4.6.2 is running as expected:
The new project is targeting .net 4.7 and is built in VS2017.
Any advice is welcome...