UNC-Paths aren't supported - c++

I am trying to color my console outputs, but while doing so ran into the following warning message:
CMD.EXE was started with the Path given above as the current Directory.
UNC-Paths aren't supported.
The Windows - Directory will be set as current Directory.
That warning comes from this line:
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
Does anyone has an idea how I would get rid of that message?

GetStdHandle is probably returning a handle which is incompatible with the debug console you have running. Calling the function once at program start-up and keeping hold of the handle might work better.

Related

How to enable System Error message when running executable from command line

According to Microsoft, "If [the system] cannot locate the DLL, the system terminates the process and displays a dialog box that reports the error. " This is the result I get when I run my application outside of the command line, but I do not get the same system error when I run the application from a shell environment such as command prompt or powershell.
Is there a way to show the same error message when the application is run from a command line interface?
https://msdn.microsoft.com/en-us/library/aa271571(v=vs.60).aspx
SetErrorMode(GetErrorMode() & ~SEM_FAILCRITICALERRORS);
but I don't think you want to do this, as you do not know in which environment the user will run your application.
It is usually now a good idea to popup a dialogbox in e.g. a service environment.
What is the problem with examining the error code of whatever is failing e.g. LoadLibrary() and reacting to this error?

Is it possible to suppress MessageBox when running a specific command?

I'm using Tortoise SVN, and TortoisePlink for remote ssh connections.
From time to time TortoisePlink is displaying errors like:
---------------------------
TortoisePlink Fatal Error
---------------------------
Network error: Software caused connection abort
---------------------------
OK
---------------------------
And they appears as messagebox and needs to be clicked by end-user.
I have tried to suppress messagebox by using -batch from command line, but problem still can be reproduced. For me it's sufficient if I'll disable messagebox prompting and print for example in command line same information - since my batch will retry later on again automatically.
Does there exists such software which can override default windows behavior and print in console window whatever is displayed in message box.
I guess in simplest terms such command line tool can be easily created, using for example
http://www.codeproject.com/Articles/44326/MinHook-The-Minimalistic-x-x-API-Hooking-Libra
and override MessageBoxA / MessageBoxW - but have anyone done this kind of thing ?
I think you're going about this the wrong way. Instead of trying to suppress the dialog boxes that TortoisePlink is showing, use the original Plink. TortoisePlink is a fork of Plink to explicitly show message boxes and not print anything on a command line, because it's designed for an UI app and not for console apps.
And if you really only want to use svn from a batch file, don't use TortoiseSVN but the original svn command line client (can be installed with the TSVN installer as well) and use the 'svn' command from the command line.
TortoisePLink.exe is targetted for end-user - so it's intended to display message boxes, however - there also exists a command line version of PLink.exe, and it can be downloaded from here:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Please notice that "TortoiseSVN" > "Settings" > "Network" > "SSH Client" should use TortoisePLink.exe - errors will be displayed to end-user.
For following file however -
%appdata%\Subversion\
file ‘config’:
In section [tunnels]
Add line like this:
ssh = Plink.exe -l <your login> -pw <password>
You should use command line version of PLink.exe (that is also displaying command prompt when launched).
It's easier to save PLink.exe next with TortoisePLink.exe, that is into
C:\Program Files\TortoiseSVN\bin.
I have tried to make a command line tool for supressing messageboxes, but it's useless for TortoisePLink.exe (Haven't debugged what is the problem), but in case if you're interested (Have other means to use it):
https://sourceforge.net/p/diagnostic/svn/HEAD/tree/Tools/msgboxSupress/
msSupress.cpp is compiled into .dll and it uses minimalistic hook for windows to disable MessageBoxA and MessageBoxW - actually they will be printed to console or redirected to file.
msSupressExe.cpp is compiled into .exe and it starts process in suspended mode, replaces .exe's entry point with EB FE (jump short into itself), waits until that address is reached, injects .dll and .dll will hook new process.
I have tested only on 64-bit platform - I suspect that command being executed must match same platform as as msSupress.exe.
So typically TortoisePlink.exe will display messagebox - but when executed like this:
mbSupress.exe "C:\Program Files\TortoiseSVN\bin\TortoisePlink.exe"
Same stuff as shown over message box will be displayed in console.
I think this solution is good prototype for further virus or trojan construction - using technique like this will allow to intercept even keyboard presses on any application - but I now made a command line utility, not a virus.

Run a console application and then close the console and run in background in C++

I want to make a console application in C++ and then when the information is displayed, close the console and run in background. Is this possible? Is another way to do that? Python maybe?
You will have to either close the console window while the process is still running, which is system dependent, or start another process, and even though the standard library offers the system function to do that, its argument is a system dependent command line.
So the upshot is: this is system dependent.
In Windows the full-version of Microsoft's Visual Studio IDE has always, as far back as I can remember, used a peculiar approach for this, with two executable files devenv.com and devenv.exe. The former is a console subsystem executable, which by default runs the latter, which is a GUI subsystem executable:
[C:\]
> where devenv
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.com
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe
[C:\]
> _
The basic idea here is that for historical reasons the command interpreter's search for an executable finds the .com file first, so the command devenv just works, either for starting the IDE or just getting the help text via the /? option.
Yes, this is possible with a small variant:
fork another process. But this is heavily system dependent:
posix/linux allow to simply clone the process;
windows requires new process to be created from an executable. You then have to communicate the state. Its less trivial as explained in this article, in the paragraph on porting fork())
then exit the program (it's the only way to give back control to the console).
On Windows use ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false) to hide the console window. It will still be running in the background and is not visible on the taskbar.
However you will have to run a task manager like Taskmgr.exe to find it and shut it down.
#include <windows.h>
#include <iostream>
using namespace std;
int main () {
cout<<"Some information is displayed.. \n\n";
Sleep(5000);
cout<<"wait.. the console is going to hide and run in background.. \n";
Sleep(5000);
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
while(true) {
// Do your hidden stuff in here
}
return 0;
}
The other answers given here overcomplicate things. The most easy way to close the console window in Windows is to simply detach from it. Once the last user of a console window datached, the console window gets closed.
If you start a program from a CLI (e.g. cmd.exe) then this CLI is also attaches to to the console and thus the console window will not close.
Anyway, detaching from a console is as simple as calling
FreeConsole();
… done!
Also you can attach to a newly created console at any time using AttachConsole, which takes a process ID. Now in a CLI situation the parent will usually be the CLI shell, so you may want to attach to the console of that.

wxExecute can't execute file that needs admin permissions UAC

From wxWidgets application I try to run installer, made with NSIS, in silent mode (/S flag). Got this error:
Execution of command: "C:\somecommand.exe" failed (error 0: Операция
завершена успешно / Successful).
When I run installer silently from command line, I got UAC popup, but when I execute it from wxWidget application I got only this error. And what this error means? Like "Fatal Error: All is fine".
In NSIS script I have RequestExecutionLevel admin, because I really need it to have ability to install program in directories like Program Files.
To start a Windows application that requires elevation you must use ShellExecute and not CreateProcess, CreateProcess will just fail with a elevation required error.
I would imagine that wxLaunchDefaultApplication uses ShellExecute internally but it seems a bit risky to rely on that so you might as well use a ifdef:
#ifdef __WINDOWS__ // __WXMSW__
ShellExecute(0, 0, pathtoexe, parameters, 0, SW_SHOW);
#else
wxExecute(something);
#endif

Qt Creator - Code is running but I get an error message from the IDE

As soon as I run my code (note that I'm using C only, no QT and no C++) I get the following message from the application output inside the IDE:
Cannot obtain a handle to the inferior: The parameter is incorrect.
When I delete the makefiles and debug/release folders it's running but after some time I still get the error. It's not that much of a problem though, the code runs and everything is fine but this error pops up and it's quite annoying.
The content of the .pro file is:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_CC = gcc -std=c99
SOURCES += main.c
Thanks in advance!
edit: I have added C++ tag, because this error occurs also for C++ console application as in my case.
I have met same problem. Tips and advices on forums are rather clueless, so I investigated problem myself and I have found that it's a bug in QtCreator.
There is dirty little program called qtcreator_process_stub. Whenever you run your program within IDE, IDE first runs qtcreator_process_stub and passes your program name as parameter (among some other parameters). qtcreator_process_stub then starts your program as separate process and prints its PID (and on windows also thread id). Information is printed to the pipe and then it is read by ConsoleProcess::readStubOutput(), which is part of Utils.dll library used by QtCreator.
The problem occurs when "inferior" process (your application) finishes execution before whole communication has been processed. ConsoleProcess::readStubOutput() attempts to use OpenProcess() on non-existing process with closed handle. OpenProcess() fails hence error "Cannot obtain a handle to the inferior: The parameter is incorrect.". Whole error is not handled very gently (Uhm, and now what?)... :-/
Solution:
When you add some user input action, pause, sleep, delay or just some loops, so that execution of your application is a bit longer error vanishes. There is enough time for ConsoleProcess::readStubOutput() to execute OpenProcess(), before your application quits. So as a workaroud I suggest to do that until the bug is fixed.
I got the same error repeatedly while working on a C++ project in Qt. I was able to solve it by copying one of the shared libraries(.dll) I was using in to the build folder.
So, if you are using any shared external libraries that are needed at runtime, make sure they are in the builds folder.