Visual C++ Enable Console - c++

I created an Empty Project in Visual C++, but now I need the Console to display debug output.
How can I enable the Console without recreating the project or show the output in the VS output window?

Here's some code you can insert to get a console window in a GUI'd windows app that starts in WinMain. There are other ways to accomplish this but this is the most compact snippet I've found.
//Alloc Console
//print some stuff to the console
//make sure to include #include "stdio.h"
//note, you must use the #include <iostream>/ using namespace std
//to use the iostream... #incldue "iostream.h" didn't seem to work
//in my VC 6
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf("Debugging Window:\n");

You can always call AllocConsole in code to create a console for your application, and attach it to the process. FreeConsole will remove the console, detaching the process from it, as well.
If you want all standard output stream data to go to the console, you need to also use SetStdHandle to redirect the output appropriately. Here is a page showing working code to do this full process, including allocating the console and redirecting the output.

You can write to the vs output window with OutputDebugString. http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx

Related

Key logger wont record key strokes without console

I created a small basic key logger in C++. For some reason when I compile and run the program with the console displayed, it will record every key stroke I make in whatever program I am using such as a browser and store it in a text file. However when I make it so that it WON'T display a console window, it will not record anything and it's just a process in the background doing nothing. Here is the link to my code: http://pastebin.com/4wqQyLJ9
The function that is giving me trouble with hiding the console, is the Stealth() function. Any suggestions, tips or hints will be helpful.
Use this function , it works for me pretty well.
ShowWindow(GetConsoleWindow(), SW_HIDE);
Instead of hiding the window after the program starts, I solved this by not
having a window to begin with. Compile with -mwindows and a window is not
created when the program starts.
Example
I would consider a Windows Service for this kind of thing if you don't need UI. Also using GetAsyncKeyState can be more stealthy if required. This C++ source might be of use...
Windows Service Keylogger

Win32 application with console output and without new window

I'd like to create a tool that can either act as command line (display some console output based on input parameters), or display a window, based on input parameters.
I'm using MSV2012 with C++, and it seems you have to 'choose' between console and window app.
I know the net is filled with samples which use AllocConsole() and redirect std::out, but it doesn't make it feel like a command line application : calling the exe from the windows console will open a new window with the console output...
Is there a way to have it use the current console window instead of allocating a new one?
If not possible, I'll make 2 applications instead, but that's a pity..
Someone else may have a more authoritative answer, but I don't believe it's supported.
The usual workaround is to create a Windows app, but have a command-line wrapper that launches it from the CLI (and provides a channel for communicating with the original console).
It's not technically supported but I found a good solution by getting a snapshot for the current process, finding the parent process, attaching to it's console if it's a console app or creating one with AllocConsole, redirecting the output, getting the thread of the parent process if it's cmd.exe and suspending it, resuming it just before I exit my app

How to call _wsystem without showing console?

I would like to copy a file to a destination by using batch command in my code.
szCommand.Format(_T("copy \"%s\" \"%s\""), szOrg, szTargetFile);
_wsystem(szCommand);
However, each time _wsystem is called, a console window will be prompted, which is very unpleasant.
Is there a way to call _wsystem without showing out the console window? Or any other alternative?
To exert control over how a new program appears, use CreateProcess. Then you can use the CREATE_NO_WINDOW process-creation flag to hide the window of a console program.
But to copy a file from one place to another, skip the external programs and just call CopyFile.
Why shell-out when there's a Win32 API that will copy a file for you. It's called CopyFile!
Details here:
http://msdn.microsoft.com/en-us/library/aa363851%28VS.85%29.aspx
#include <windows.h>
CopyFileA(szOrg, szTargetFile, FALSE); // use CopyFileW if szOrg and szTargetFile are unicode strings
Would having the window minimized be okay? See this.

Console output window in DLL

I am trying to redirect the output from my DLL to an external console window for easy debugging.
I have been told about AllocConsole but I am not able to reproduce it, i.e. the console window does not appear.
My current environment is Visual Studio 2005.
I tried the following example which is gotten off the Internet,
AllocConsole();
HANDLE han = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(han,"hello",6,new DWORD,0);
yet nothing happens. Can someone point me in the right direction if creating a console window via DLL is possible in the first place.
Thanks in advance!
The proper way to output debug strings is via OutputDebugString(), with an appropriate debugging tool listening for output strings.
Once loaded, there is nothing special about DLLs, so there is no way that allocating consoles would be any different for a DLL than for the EXE that originally loaded it.
Having said that, a process can be associated with only one console at a time, so if there is already a console attached to the process, then allocating a new one is not going to do anything (I assume you're checking the return value of AllocConsole? What does it return? What does GetLastError return?)
There are some other possibilities. For example, if your DLL is loaded into a service, then the service will (likely) be running under a different window station to the currently logged-in user so if you create a console window, you won't be able to see it.

How to make a console program doesn't have console window

I'm writing a console program.
The program doesn't print anything.
So, it doesn't need to a console window.
I tried to call FreeConsole() function at program starting point.
When I execute the program from windows explorer, a console window appears and then disappears.
But I wish the console window never appears.
How can I do that?
Thanks in advance.
If you are using Visual Studio .Net then create a normal console application and change the output type to Windows application.
Use WinMain instead of main as your program's entry point: WinMain at MSDN