0

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: enter image description here The new project is targeting .net 4.7 and is built in VS2017.

Any advice is welcome...

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
  • 1
    Did you last use it a while ago? Interactive services have not been supported since Vista, the way to do it is via IPC with a desktop app. – Alex K. Mar 06 '18 at 12:51
  • Please read the post carefully: I never said that the console is attached while it is running as service. When not run by svchost but simply started from windows explorer (or by the VS itself), it is a regular windows forms application. This mode is used for debugging, and it worked like a charm on the same windows 2016 server in several of my previous solutions. – ZorgoZ Mar 06 '18 at 13:07
  • @HansPassant: unfortunately, the same exception is thrown when these are the very first statements in Main(). – ZorgoZ Mar 06 '18 at 13:21
  • I see it. Hmm, the handle doesn't actually look invalid. The Windows version matters, they've been hacking the console heavily to support the Linux sub-system. Mine is the dreaded Win10 Fall Creator's edition. – Hans Passant Mar 06 '18 at 13:39
  • Well, mine is 2016 Standard, but as said before, this portion of the code is totally identical with one that is currently running as expected from VS2015 .net 4.6.2, see update. – ZorgoZ Mar 06 '18 at 13:54
  • That is not it. Repro the issue in VS2015 with Project > Properties > Debug, untick "Enable the Visual Studio hosting process". – Hans Passant Mar 06 '18 at 14:19
  • Very interesting. Indeed, disabling vhost makes the code in VS2015 behave in the same way. But there is no such setting in VS2017. What difference does this setting make that can cause such issue? – ZorgoZ Mar 06 '18 at 14:50
  • What the heck?! I have ported it back to 4.6.2 and reopened the solution in VS2015 and voilá, it works.... but this is not a solution, not even a good workaround :( – ZorgoZ Mar 06 '18 at 14:58
  • At the end I abandoned the console and replaced it with a form. Does the job too. It would be still interesting investigating further... Thank you @HansPassant for the hint! – ZorgoZ Mar 07 '18 at 09:53

0 Answers0