How to call _wsystem without showing console? - c++

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.

Related

getenv("LINES") doesnt work on windows

i tried to use LINES = atoi(getenv("LINES")) in windows (visual studio 2012), but it doesn't work. Someone told me that I have to add export LINES to .bashrc or .profile. Will that solve my problem?
If it does, how to add export LINES to .bashrc or .profile?
You almost certainly want GetConsoleScreenBufferInfo to retrieve the screen buffer info (and GetStdHandle to get the console handle).
There's neither .bashrc nor .bash_profile on Windows. In order to get the current console window size, use the GetConsoleScreenBufferInfo API. Look it up. Since the window is resizable, you might want to watch that size. Use ReadConsoleInput or PeekConsoleInput to check for window resizing.
Why are you trying to get the console window dimensions, and why won't you write a proper GUI application instead?
If you are working in Visual Studio, and you just set the LINES variable, you need to first restart the IDE after setting the environment variable. Otherwise, getenv() will not return it in the processes spawned by the IDE.

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

How can I hide the command prompt of an application after it has started?

How do I go about suppressing the command prompt of a c++ application using the mingw compiler AFTER the program has started.. -mwindows works great in the linker settings but I want to be able to toggle it whilst the program is running, is this possible at all?
I am using a text editor and command line so no IDE related answers please.
As far as I know: no, at least not with a single executable. When you open an application in a Windows based console, it will start an instance of conhost.exe in order to provide an environment to your command line application. The console host will run as long as your applications hasn't exited.
It's hard to determine in which circumstances you'll need this behavior. But you could create two application - one which is a simple command line application, and one which has been compiled with -mwindows. The latter could call the first. After the first has exited the second will continue executing.
Note that this will leave the user bewildered, as it seems to him your application has stopped (as the console window has been closed) and a -mwindow compiled application doesn't create any GUI elements.
You can use WinAPI function ShowWindow to hide and show any window. There is a quirk, however - this function accepts an HWND handle as its argument and there is no obviuos way to obtain console HWND. Following is pretty convoluted way to get it:
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
TCHAR pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
TCHAR pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
TCHAR * format=_TEXT("%d/%d");
wsprintf(pszNewWindowTitle,format,
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
Forgive me for this dirty trick, but it works perfectly in my code and is an official way of getting console HWND.
Some programs have to be of a console type. Like Emacs where the same executable can be launched to operate in console (with -nw option) and in GUI.
To hide that console there are lots of methods (including esoteric WSH scripts, or 3rd party utils, like nircmd exec hide notepad.exe) but there is a simple modern portable way:
powershell -c Start-Process -WindowStyle Hidden -FilePath notepad.exe
You can wrap that ugly command into .bat script alias.
PS Use Task Manager to kill hidden Notepad ))

Console App Whereabouts c++

How would you set the initial position of a Console App on your Screen?
It's a console app, so it has no concept of where its window is, as it doesn't know what a window is.
For Windows, you could use the GetConsoleWindow function followed by SetWindowPos with the SWP_NOSIZE and SWP_NOZORDER flags set.
I think that you're going to be more specific. With a console app, the output goes to stdout without any real control over how the console deals with it. The console deals with displaying it and normally just prints it out.
If you want more control over the console like being able to reposition the cursor or being able to erase or redraw portions of the console, then you'll likely need to look into a library like ncurses.
You can't. Put simply. If you use non-standard extensions, for example, if you made your own console via WinAPI, you might be able to make such an effect. However, within terms of just cin/cout, then you can't.
If you're in windows, then you have to set the position of the final executable. If you click the application icon and then click "Defaults" on the resulting menu, one of the options is for position.
Unfortunately, no idea how to do that on other platforms.

Visual C++ Enable Console

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