Windows: eseguibile senza console e CLI che riaggancia il terminale
OutputType passa a WinExe: nessuna finestra di console al doppio clic. Con argomenti Core/ConsoleHost.cs chiama AttachConsole sul terminale padre e collega stdout e stderr a CONOUT$ solo se non sono già reindirizzati; senza terminale padre non fa nulla. Su Linux il comportamento non cambia. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4b6c3693a
commit
c0c140b08d
3 files changed
+43
-1
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace AgentWikiUi.Core;
|
||||
|
||||
// Su Windows l'eseguibile è senza console (WinExe, ADR-0005). In modalità CLI si riaggancia il terminale
|
||||
// da cui parte, così l'output è visibile; senza terminale padre non succede nulla.
|
||||
static class ConsoleHost
|
||||
{
|
||||
const int AttachParentProcess = -1;
|
||||
const int StdOutput = -11, StdError = -12;
|
||||
const uint GenericReadWrite = 0xC0000000, ShareReadWrite = 3, OpenExisting = 3;
|
||||
static readonly nint InvalidHandle = -1;
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)] static extern bool AttachConsole(int processId);
|
||||
[DllImport("kernel32.dll", SetLastError = true)] static extern nint GetStdHandle(int stdHandle);
|
||||
[DllImport("kernel32.dll", SetLastError = true)] static extern bool SetStdHandle(int stdHandle, nint handle);
|
||||
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
static extern nint CreateFile(string name, uint access, uint share, nint security, uint disposition, uint flags, nint template);
|
||||
|
||||
public static void AttachToParent()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows()) return;
|
||||
try
|
||||
{
|
||||
if (!AttachConsole(AttachParentProcess)) return; // nessun terminale padre (doppio clic, avvio da altro programma)
|
||||
Connect(StdOutput);
|
||||
Connect(StdError);
|
||||
}
|
||||
catch (Exception ex) when (ex is DllNotFoundException or EntryPointNotFoundException) { }
|
||||
}
|
||||
|
||||
// Collega l'handle alla console solo se non è già valido: un file o una pipe ereditati (> file, | altro) restano com'erano.
|
||||
static void Connect(int std)
|
||||
{
|
||||
var current = GetStdHandle(std);
|
||||
if (current != 0 && current != InvalidHandle) return;
|
||||
var console = CreateFile("CONOUT$", GenericReadWrite, ShareReadWrite, 0, OpenExisting, 0, 0);
|
||||
if (console != InvalidHandle) SetStdHandle(std, console);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using AgentWikiUi.Gui;
|
||||
// il resto degli argomenti è unito in un solo prompt. Senza argomenti: GUI.
|
||||
if (args.Length > 0)
|
||||
{
|
||||
ConsoleHost.AttachToParent(); // Windows: l'eseguibile non ha console, si usa quella del terminale padre
|
||||
var engine = Engines.Claude;
|
||||
var words = args;
|
||||
if (args[0] == "--engine")
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<!-- WinExe: su Windows nessuna finestra di console al doppio clic; la CLI riaggancia il terminale (ADR-0005). Su Linux è identico a Exe. -->
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<!-- Unica sorgente del nome dell'app (vedi docs/README.md → "Nome del progetto") -->
|
||||
<AssemblyName>agent-wiki-ui</AssemblyName>
|
||||
|
||||
Reference in new issue
Block a user