Is it possible to attach a non-console Win32 application to the invoking cmd shell? - windows

When I have a Win32 non-console application (AFAIK, the console-ness of a Win32 app is linked into the exe), starting it from the console cmd.exe will return to the command prompt immediately, running the application "in the background" (o.c. it can have a GUI of sorts, or even open its own console window)
Is it possible in the non-console executable to detect that it was launched from cmd.exe and "attach" it to the launching cmd.exe?
And note that there are various questions/answers related to this, but it seems that this exact approach hasn't been investigated. (Maybe it's not possible like that.)

You can do this very easily. Simply pass ATTACH_PARENT_PROCESS to AttachConsole.
Whether or not the end result is sensible or practical is something I could not say. Both processes would read and write to the same console which could get pretty weird.

Related

How can I show the console for a Windows GUI application only when it is started from the console? [duplicate]

My goal is to have it so when the executable is just double clicked no console spawns but also have it able to print to the console when the user launches it from the command line.
I have the following Windows-specific code set to not spawn a console:
#![windows_subsystem = "windows"]
fn main() {
println!("Hello world");
}
Unfortunately, it never hooks stdout/stderr to anything since its set to not create a console on startup. Is there any way to achieve my goal despite this?
This is not really specific to Rust, but instead is the way Windows works: you either write for the "console" subsystem and your program opens a console if it is started without one, or you write for the "windows" subsystem and your program detaches itself from the console if it is started from the command line.
The PATHEXT environment variable — which CLI shells use for the file extensions to add when searching PATH for a command — normally includes ".COM" before ".EXE", in which case one can create a console version of the application with a ".com" file extension (e.g. "app.com"). This can be a standalone version or just a launcher that spawns the main executable (e.g. "app.exe", a GUI app). A launcher can pass a command-line option that allows the main application to attach back to the console via AttachConsole.
Without a launcher, it's a bad idea to attach back to the console since the parent application (e.g. a CLI shell or a full TUI app) normally will not wait for a GUI application to exit, in which case the result is chaotic interleaved I/O with two applications competing for access.

Windows Batch: Running a Ruby program opens a cmd window

I basically would like to execute a (Cygwin-) Ruby program by clicking on some icon on my desktop. My first attempt went like this:
Create a desktop link
As a link target, have something like
c:\cygwin64\bin\ruby /path/to/my/ruby/program
This works, but it also opens a window where Ruby "runs in", which is not what I want to have.
If it were ActiveState Perl, I would have a command "wperl", which executes Perl without creating a Window, but such an feature doesn't seem to exist for Ruby, at least not for the Cygwin distribution.
I tried to change the link to
cmd /MIN /C c:\cygwin64\bin\ruby /path/to/my/ruby/program
hoping, that this would run the window minimized, but same effect as before, so I think I need to program somehow a wrapper script which suppresses the creation of this window. Does anybody know how this can be done, preferably using the Windows Batch language or some clever commands in the Cygwin tool chain?

Trying to run .vbs as a startup program. Forcing to run in cscript. Want it to run in background, instead of having a script host window open

So I've done some digging to try and find a way to run a script in the background on startup. The only solution I've found is to use:
Set WinScriptHost = Nothing
Is this a reasonable way to do it? Or could it cause some issues? I mean, from what I can tell, it just stops WinScriptHost from being used to refer to an object. But I feel like that could cause trouble in some scripts. So, should I avoid using this method and do something else?
Thanks!
CScript is for console windows, consoles programs by their definitions have a console. WScript is for GUI programs, unlike console programs, windows are optional (although almost all programs will create a hidden main window if not creating a visible window, as windows are how Windows communicates with a program).
Using WshShell.Run, which has a window style parameter, you can run cmd hidden eg cmd /c cscript script.vbs.

Is there a way for a windows-subsystem application to offer optional console output?

I have a Windows application application that is normally GUI-only and declares the WINDOWS subsystem. It doesn't open a console window when launched.
Alas, I would like to offer additional console output when the application happens to be started from console window (say, from interactive cmd.exe).
Is there a way of detecting if some process "up the chain" has an open console, and to attach to that console?
The closest I've found is a way to explicitly open a console for a gui application, but I don't want to open a console if there isn't one already there.
At first glance it seems like the AttachConsole() function will let you attach to the console of your parent process:
AttachConsole(ATTACH_PARENT_PROCESS);
If the process doesn't actually have a console the function will fail with ERROR_INVALID_HANDLE. The function will also fail if your parent process no longer exists. If it fails you can then call AllocConsole() to create your own.
I've never tried this so don't actually know if it will work or not :)

Opening console applications in powershell

I'm currently developing a win32 console application, and wondering if there is any way to make visual studio open it in powershell instead of cmd.exe when I'm debugging it.
All I really want is a better shell, where I can copy/paste etc. without clicking.
Thanks
I think you're mixing up the NT console subsystem (an app framework offerring common services) with cmd.exe (an application consuming those services.) When visuals studio runs a console application, it's not actually running CMD. CMD is a console application itself, no different than the app you are trying to debug, therefore running your application "in powershell" is equally as mistaken a concept.
If you mean trying to run it in PowerShell ISE, this is impossible. ISE is a Windows Application (NT GUI subsystem), which is an entirely different subsystem than that of the console.
-Oisin
You can try this.
Go to properties of your console project.
In Debug tab select Start external program and enter path to powershell.exe (C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe)
Enter ".\YourApp.exe" into Command line arguments
This starts your app in powershell. However it breaks a lot of things (like debugging, ...)
In Visual Studio goto Tools > External Tools...
In this window you can change the Title to Powershell. Set the Powershell Startup to execute and the check the output window box and it will dump all the output that would typically goto cmd into Powershell.
This link will explain it a little bit more for you.

Resources