Why using WriteConsoleA in mingw has no output? - windows

I am trying to compile on mingw a program that prints to console using Windows.h functions. Why do I get no output?
C file:
#include <Windows.h>
int main() {
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(out, "hello", 5, NULL, NULL);
return 0;
}

To print to a console your application must be a console application in the first place. A Windows GUI application can
Make sure you linked the application using the -mconsole flag.
Also make sure that you are not running the application from somewhere where the output may disappear right away (like an IDE). Instead open a command prompt and run your .exe file from there to see the output. Or you could add some code to wait for a key in your code so you can see the output before the window closes automatically.

Related

Stop command prompt from opening when running exe file built from haskell project

I have created a small application that uses the gloss library for a GUI. When typing "cabal run [cabal file]" the project is built and run, and only prompts the GUI. When I instead go and click on the .exe file that was generated from the build, the GUI opens but so does the command prompt. I want to stop the command prompt from opening and only have the window from gloss open.
By default on Windows GHC builds console programs, which open a Command Prompt terminal if one isn’t open already. If you want to build a GUI-only program, you can pass -mwindow to the linker by giving the -optl -mwindow flag to GHC. If you’re using Cabal, you would add this flag to the ghc-options field in the executable stanza.
Beware that if you use this flag, your program will not have any stdin, stdout, or stderr file handles, so actions like putStrLn, print, and getLine will not work, since they’re equivalent to hPutStrLn stdout, hPrint stdout, and hGetLine stdin. If you want to do any logging or printing, you’ll need to open a handle yourself (e.g. a file, terminal, or socket with another program reading the other end); however, Debug.Trace will still work if you need temporary tracing for debugging.
You can find more helpful reference information in the GHC User’s Guide §16. Running GHC on Win32 Systems.

How to make one executable file that has a bash script and dependency files?

Here is my bat script (TestBatScript.bat):
set /p args=Enter some args:
TestApp.exe %args%
pause
For this script to work I have to have TestApp.exe and TestBatScript.bat in the same folder. Is it possible to include these two files in one file .bat or .exe or whatever?
If this is what you are asking for, you can't aggregate batch file with exe file. Indeed exe file are compiled file (open it on a text editor, you won't be able to read it). Batch files are scripts that are interprated by terminals.
If you can recompile your exe file, you can add to source code to pass argument with hands (for instance use scanf in C, ...) if there is no argument passed by the system
int main(int argc, char** argv) {
if(argc==0) {
/* get here the argument (no argument passed)*/
}else {
/* use here argument passe to your function */
}
/* Do stuff */
}
You can use Iexpress that is a built in program in your windows OS. To access it hir win+R and then type iexpress or iexpress.exe. Do some experiments and you will eventually understand. Here is the Wikipedia in case want it. And Here is a tutorial on How to use it.

Print to console/command prompt

I want to write text to console/Windows command prompt in AutoIt. I made a test script as shown below:
Func Test()
ConsoleWrite("Hello")
EndFunc
Test()
I saved the script as test.au3. When I run it, it does not print to console. I checked ConsoleWrite(); it should print to DOS console if it the script is compiled as a console application.
I compiled the script using Aut2Exe. It still does not print to console. How do I write to console in AutoIt?
You can also add the following compiler switch to the top of your script:
#pragma compile(Console, True)
Just compile your test.au3 like this:
%PathToAutoItVersion%\Aut2Exe\Aut2exe.exe /in test.au3 /out test.exe /console
And then you can Run test.exe and it will print out:
hello
How do I write to console in AutoIt?
As per Documentation - Function Reference - ConsoleWrite() :
The purpose for this function is to write to the STDOUT stream. … Scripts compiled as Console applications also have a STDOUT stream.
Save script as .au3 file, then:
press F5 (Tools > Go) in editor. Console output will be displayed in the editor's lower pane:
or press Ctrl + F7 (Tools > Compile), enable Create CUI instead of GUI EXE., then click Compile Script and run the resulting executable.
or add #AutoIt3Wrapper_Change2CUI=Y (or #pragma compile(Console, True)) to top of script, then press F7 (Tools > Build) and run the resulting executable.
or execute:...\AutoIt3\Aut2Exe\Aut2exe.exe /in ...\script.au3 /out ...\script.exe /consoleand run the resulting executable.
I compiled the script using Aut2Exe. It still does not print to console.
For compiled scripts a console window is visible during runtime only. Example:
#AutoIt3Wrapper_Change2CUI=Y
Global Enum $EXITCODE_OK
Global Const $g_sMsg = 'Hello, World!' & #CRLF
Global Const $g_iDelay = 1000 * 10
Main()
Func Main()
ConsoleWrite($g_sMsg)
Sleep($g_iDelay)
Exit $EXITCODE_OK
EndFunc
Related: Console and graphical user interface.

Boost.Asio object_handle doesn't wait for stdin when debugging

I am trying to use Boost.Asio's object_handle to wait for input from the console:
int main()
{
using namespace boost::asio;
io_service io;
windows::object_handle in(io);
in.assign(::GetStdHandle(STD_INPUT_HANDLE));
in.wait();
io.run();
return 0;
}
This works if I run it from the terminal, but when I try to debug into it with Visual Studio it skips wait(). What's going on?
A windows console application can create it's own console, or it can attach to an existing console (e.g. the parent command shell). This is likely what creates the difference.
You can influence the console allocation usually with things like start cmd /c myprog vs. ``start /b cmd /c myprog`, or you can explicitly create you console
The MSDN article that has the backgrounds and APIs is here:
Creation of a Console

Create .exe file to open .html document

I need to create a .exe file that opens an .html document with the default browser.
In command prompt, start myfile.html works, but I don't know how to turn that into an .exe file.
How can I do that please?
Thank you.
Quite useless IMHO... Anyway use a .BAT file instead and use some app around there that converts .BAT to .EXE.
Why not just use a .bat file? Much less hassle than compiling an .exe. As well, in Windows, double-clicking the .html file would open it in the default browser anyways, so you're not gaining much.
I'd echo the questions about why you'd want to do this, but if you really insist on doing it, it shouldn't be terribly difficult.
#include <windows.h>
int main(int argc, char **argv) {
if (argc !=2) {
MessageBox(NULL, "Usage: exec_html <html_file>", "Usage Error", MB_OK);
return 1;
}
if ((int)ShellExecute(NULL, "open", argv[1], NULL, NULL, SW_SHOWNORMAL) < 32) {
MessageBox(NULL, argv[1], "Could not open file", MB_OK);
return 1;
}
return 0;
}
Compile with a C or C++ compiler of your choice. If (as sounds like the case) you don't normally use either, the easiest way to go is probably get Microsoft Visual C++ Express, a free (as in beer) download.
First download a bat to exe converter here, then open notepad and type the following:
#echo off
start myfile.html
exit
Save it as whatever you want (.bat) and then start bat to exe converter and select the batch file and click the compile button.
This way works and too everyone wondering why you might want to do this, I infact have been looking everywhere for this and am glad I found it. I needed to make a ZIP file and inside I needed my game to be runable through an exe file and my game could only be run through HTML so I found this and have been able to do so. :)
You can use ShellExecute with "open" as verb from C or C++.
First download bat to exe converter from the following link:
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
Then open notepad and type the following:
#echo off
start myfile.html
exit
Save it as whatever you want.bat and then start bat to exe converter and select the batch file and click the compile button.
Now you get the exe file you wanted.

Resources