# Compilation switch to avoid the debug console under Windows?

**URL:** <https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356>\
**Category:** Help & Support\
**Created:** [February 9, 2023, 6:43pm UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356 "2023-02-09T18:43:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 9, 2023, 6:43pm UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/1 "2023-02-09T18:43:38Z")

</div>

Compilation switch to avoid the debug console under Windows : Is there one ?

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [February 10, 2023, 9:05am UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/2 "2023-02-10T09:05:16Z")

</div>

I’ve created issue about it:

> <https://github.com/crystal-lang/crystal/issues/13058>
>
> \## Feature Request
> 
> \- Is your feature request related to a problem? Please des…cribe clearly and concisely what is it.
> Right now, every crystal program on Windows creates console window. It is fine for experimenting, but for polished programs (GUI, games, headless servers, etc) there should be a way to build without showing console window.
> 
> \- Describe the feature you would like, optionally illustrated by examples, and how it will solve the above problem.
> I've tried adding \`@\[Link(ldflags: "/subsystem:windows")\]\` - application just silently closes after start.
> I think the problem is in https://github.com/crystal-lang/crystal/blob/master/src/crystal/system/win32/wmain.cr so tried to change it in different ways (change entry point to \`fun wWinMain(ptr1 : Void\*, ptr2 : Void\*, argc : Int32, argv : UInt16\*\*) : Int32\`, change flags) but no luck - application still closes at start.
> 
> - Minimal example:
> \`\`\`crystal
> @\[Link(ldflags: "/subsystem:windows")\] # it works (but show console window) if this line is commented
> @\[Link("user32")\]
> lib WinAPI
> fun message\_box = MessageBoxA(hWnd : Void\*, lpText : UInt8\*, lpCaption : UInt8\*, uType : UInt32)
> end
> 
> WinAPI.message\_box(Pointer(Void).null, "Hello", "World", 0)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [February 10, 2023, 10:07am UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/3 "2023-02-10T10:07:35Z")

</div>

Actually, the answer is not far away: [How to receive different kinds of parameters in main function(eg `winMain`) - #4 by HertzDevil](https://forum.crystal-lang.org/t/how-to-receive-different-kinds-of-parameters-in-main-function-eg-winmain/5039/4)  
I’ve tried and yes, it works. Thank you @HertzDevil !

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 16, 2023, 12:47pm UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/4 "2023-02-16T12:47:41Z")

</div>

Do you know how to use this to start an app without console ?

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [February 16, 2023, 2:35pm UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/5 "2023-02-16T14:35:18Z")

</div>

add

```auto
{% if flag? :windows %}
  module Crystal::System::FileDescriptor
    def self.from_stdio(fd)
      console_handle = false
      handle = LibC._get_osfhandle(fd)
      if handle != -1 && handle != -2
        handle = LibC::HANDLE.new(handle)
        # TODO: use `out old_mode` after implementing interpreter out closured var
        old_mode = uninitialized LibC::DWORD
        if LibC.GetConsoleMode(handle, pointerof(old_mode)) != 0
          console_handle = true
          if fd == 1 || fd == 2 # STDOUT or STDERR
            if LibC.SetConsoleMode(handle, old_mode | LibC::ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0
              at_exit { LibC.SetConsoleMode(handle, old_mode) }
            end
          end
        end
      end

      io = IO::FileDescriptor.new(fd, blocking: true)
      # Set sync or flush_on_newline as described in STDOUT and STDERR docs.
      # See https://crystal-lang.org/api/toplevel.html#STDERR
      if console_handle
        io.sync = true
      else
        io.flush_on_newline = true
      end
      io
    end
  end

  @[Link(ldflags: "/ENTRY:wWinMainCRTStartup")]
  @[Link(ldflags: "/SUBSYSTEM:WINDOWS")]
  lib LibCrystalMain
  end

  lib LibC
    fun CommandLineToArgvW(lpCmdLine : LPWSTR, pNumArgs : Int*) : LPWSTR*
    fun LocalFree(hMem : Void*) : Void*
  end

  fun wWinMain(
    hInstance : Void*,
    hPrevInstance : Void*,
    pCmdLine : LibC::LPWSTR,
    nCmdShow : LibC::Int
  ) : LibC::Int
    argv = LibC.CommandLineToArgvW(pCmdLine, out argc)
    wmain(argc, argv)
    ensure
      LibC.LocalFree(argv) if argv
  end
{% end %}

```

anywhere and it should works. Note that any `puts` will silently crash application.

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 16, 2023, 5:27pm UTC](https://forum.crystal-lang.org/t/compilation-switch-to-avoid-the-debug-console-under-windows/5356/6 "2023-02-16T17:27:35Z")

</div>

Amazing !

Thank you very much @konovod !
