How do I write to the CommandPrompt from Windows GUI? - visual-studio

Operating Environment: Windows 7, Visual Studio 2010, CLR GUI.
So I've been given the unglorious task of enhancing a GUI application that is started from a command prompt. Because it is. Because poor design decisions by previous implementers. Anyway, it launches one of several GUIs depending upon the input arguments.
I'd like to be able to print back to the same command prompt window if (when) the user types something that the code doesn't understand.
Here's what I've tried (none of which output anything):
int main( array<System::String^>^ args )
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
OutputDebugString("hello");
Trace::WriteLine("hello");
Debug::Trace::WriteLine("hello");
Console::WriteLine("hello");
std::cout << "hello";
printf("hello");
return 0;
}
Thanks in advance!
Update: I don't want to use AllocConsole(), as that opens a new console that disappears along with all of the data when the application exits. Similarly, a pop-up message box won't work. I'm looking for a way to make the output persistent.
The only way I can get output from the application to date is via a message box (non-persistent) or opening a new console that disappears when the application exits (via AllocConsole() ). And I'm running from a command prompt, not the debugger's "Play" button.
Update
Why the down-vote for not doing research? I spent a day trying to solve this, looking through dozens of posts trying to find a solution, and to date I've found others looking for the same answer, but not finding it. AllocConsole() or changing the project type is always the solution, but neither is a solution for me.
Update
I added the "full code", which is the 2 statements. THAT IS ALL THE CODE. So simple. I'm skipping the start of the GUI because I don't care about that right now, I just want it to print back to the console where the application was started. The most basic HelloWorld. If there are project settings I need to post, I don't know which ones would be relevant. This is where I want to print to the console, before the GUI is ever up. I can't show the GUI if there is an error in the user input.

Right click on the project, select Properties
Under Linker -> System, Change Subsystem from Windows to Console.
A Windows subsystem application cannot write to console, but by changing the subsystem to Console (which can write to the calling console), the Form part of the application can still work (tested in Visual Studio 2010).

Related

Use a console window when debugging unit-tests in Visual Studio

When I debug a unit-test with Visual Studio 2017, all the console output is logged and I can examine it by clicking the "Output"-link in the result box of the unit-test (I'm using the built-in Test-Explorer).
However, since I do log a lot to console at runtime which is even color coded so important stuff is more visible, I can't see this color coded output using the "Output"-link, because it's just plain black text on white background.
Is it possible that Visual Studio shows all the output directly in a console window when debugging the tests so I see the output as I would see it when actually running the application outside of a test?
You cannot (or it is very tricky at least) open a console window from a unit test - I've tried with the top 2 answers from the following post, and they didn't work: Show Console in Windows Application?
You can make the Debug.Write... methods write to the Console when you are running/debugging the application itself (not the unit tests) with this code, though:
ConsoleTraceListener listener = new ConsoleTraceListener();
Debug.Listeners.Add(listener);
Any calls to Debug.Write... methods after this code will also output to the console.
Standard output is the only way to display the test output, when we use some output method in our test like Debug.WriteLine(); Console.WriteLine(); etc.
I also tried to change the font of the test output but it seems there is no that option is VS 2017, even someone though someone mentioned this before:
Is there a way to change the Font used in the VS2012 Test Output Window?
Similar case discussed in this thread, even some reply claimed that we could manually launch the console window, but they are 100% sure about it.
If you really need change the font or the color of the test output, in Visual Studio you go to Help -> Send feedback to VS developing team to provider your suggestion .

Avoiding "Press any key to continue" when running console application from Visual Studio

When running a console application in Visual Studio via "Start without Debugging" (Ctrl+F5), the console remains open at the end of the run asking to
Press any key to continue . . .
thus requiring to activate the window and hit a key. Sometimes this is not appropriate.
Why this matters:
At the very moment I write json serialisation code, my workflow goes like this:
adapt c# code
run a console app that writes file out.json
view out.json in the browser with a json viewer
do this again and again, no need to debug anything, just trimming serialisation and check output is good.
It is workflows like this, where the "press any ..." behavior is hindering as it requires the steps
activate the console window
press key
.
No answers:
Starting the application outside VS in a separate console is not an answer.
Saying, you dont need this.
I'm pretty sure that you cannot affect or change this behavior.
As you mention, it has nothing to do with your application itself, because it doesn't do it when you double-click on the EXE. You only see this effect when you run the app from within Visual Studio without the debugger attached.
Presumably, when you invoke Ctrl+F5, Visual Studio is running your app in a particular way that causes the console window to remain open. I can think of two ways it might be doing it:
%COMSPEC% /k "C:\Path\To\YourApplication.exe"
or
%COMSPEC% /c ""C:\Path\To\YourApplication.exe" & pause"
With either of these, the pausing behavior you're seeing is baked right into the command used to launch your app and is therefore external to your application. So unless you have access to the Visual Studio sources, you're not going to change it. Calling an exit function from your app won't have any effect because your app has already quit by the time that message appears.
Of course, I can't see why it really matters, aside from an issue of curiosity. This doesn't happen when you start the app with the debugger attached, which is what you'll be doing 99% of the time when you launch the app from the IDE. And since you don't ship Visual Studio along with your app, your users are going to be starting the app outside of VS.
In response to the updates made to your question, the best solution would be to change your app so that it is not a console application. This behavior doesn't affect standard Windows applications; when they get closed, they close for good.
If you do not require any output on the console window, then this is very simple to do: just change the "Application type" in your project's properties. A Windows Forms application will work just fine. If you do not display a window (aka form), one will not be automatically created. This is the difference between regular Windows applications and console applications, which always create a console window, whether you need one or not.
If you do need to display output on the console window, you have a couple of options:
Create and use a simple form with a ListBox or ListView control. Each line that you would normally output to the console, you add as a new item to the list control. This works well if you're not using any "advanced" features of the console.
P/Invoke and call the AllocConsole function to create a console that your Windows application can use. You do not need a form for this.
I found a solution that works if you are using python (I could not test anything else).
You need to go to
Tools -> Options -> Python Tools -> Debugging
Uncheck Wait for input when process exits normally.
I hope you can apply this somehow to your problem.
2020 UPDATE : Microsoft has listened.
It also closes the console in "Start Without Debugging" mode ;)
The setting is a little buried, but works :
Well, at least in Visual Studio 2010, typing
Console.ReadKey(true);
Removes the "Press any key to continue....."
According to the VS2019 documentation:
Automatically close the console when debugging stops: Tells Visual Studio to close the console at the end of a debugging session.
It works, but only if you make sure your project starts with the debugger on. This sounds trivial, but I was trying at first with a solution with two projects, one Console one to copy files to use in my app, the other to run the actual app. I set the Console one to Start without debugging because I don't need debugging on it, but that did not close it after it ran. Only when setting it to Start (with debugging) this option worked.
In vs2017 you have to uncheck the python environment setting under the vs-options:
in german: Auf Eingabe warten, wenn der Prozess normal beendet wird

debugging vb6 form inside 3rd party application

I have to travel back in time and debug a VB6 form. It is used inside an application (not ours, Esri ArcMap.exe). I see VB6 has some debug capabilities, but nowhere do I see, like in .NET, the ability to "launch another app" or "attach" to a running application.
How can I debug my VB6 code while running inside a 3rd party application?
It's right there; you just didn't see it.
If your form is hosted inside a DLL (most likely from your description), you have two options, both of which can be set from the "Project"-> "Projectname Properties" dialog box, Debugging tab.
"Wait for components to be created"
When click on Run (F5), VB6 will alter the registry entries for your DLL so that they point to the VB6 debugger.
Launch your third-party program as usual.
When any program tries to create objects and forms hosted by your DLL, it will do so through the debugger.
"Start a program"
Same as before, plus VB6 will execute the command line entered in the dialog box.
It should be noted that VB6 must be run in full "As Administrator" mode, because it needs to write to normally-protected areas of the registry.
Don't worry about the third option. Let's just say that some things are better forgotten.
My first-hand knowledge is a little hazy (I've inherited a PC with all of this stuff configured, and I have no intention of ever setting it up again from scratch...), but here goes:
There is an ESRI VB6 add-in called ESRI Compile and Register, which is supposed to make this whole process a lot easier.
There's a lot of instructions on that link, but one of the key things is under Options->Support Visual Basic Debugger, which creates a file ESRIVBDebugHelper.exe.
In Project -> Properties -> Debugging tab, under Start Program, you enter the full path to this ESRIVBDebugHelper.exe. Then when you debug the VB6 application, it should spawn ArcMap and allow you to debug on the fly within the VB6 IDE.

Win32 Application Console Output?

When developing a Win32 Application (non-console application) in Visual Studio 2005, is there any way to get the same sort of output as you do from the console?
For instance, say I want to see log statements (such as I would with cout in a console application) to trace the path my program has taken in the code.
My first thought would be that this could be done through the Output tab selecting something from its "Show output from:" dropdown, when debugging but I don't know what API I need to do this...
For example say I had the following in my windows application and wanted to know when the following function enters and exits writing the result to the Visual Studio window above.
void someFunction(void)
{
Win32APIConsoleLog("BEGIN: someFunction()");
// ...
Win32APIConsoleLog("END: someFunction()");
}
Is this possible? And if so, what what libraries do I need to include and what function calls do I need to make to write out to the console window?
OutputDebugString.
I assume that you want to write to the debug console, since that's what your screenshot shows. OutputDebugString is a nop when no debugger is attached, but it allows you to log whatever you want to the debugger's output.
OutputDebugStringW(L"This will go to the output.\n");
// or
OutputDebugString("This will go to the output.\n");

Using "run as" with Visual Studio debugger

Is there any way to use the "Run As" option in Windows XP in conjunction with Visual Studio's debugger, to debug an issue that occurs in my application only when certain users are logged in?
I have ran the application from my machine using "run as" to pretend to be the user in question, and I got the same error as they did. I would like to debug this error and see where and what exactly is causing it. The error occurs specifically when a certain domain user is logged in, and never otherwise.
Is there any script or approach I can take to debug this error; that is too launch the application, as the problem user, and then use the debugger?
Trying to attach to the process didn't work since it was a C# managed process and VS didn't let me attach.
The first two options that come to mind are...
Log onto the machine as the user (simplest approach)
Right-click on the Visual Studio executable and run as that user.
I think that you could edit the .config file to use impoersonation, but I'm not sure if that will result in the app running truly the way it would for the user.
Although with good error handling, the error message itself should be enough to tell you where in the code the problem is... At the very worst, you could compile it in debug mode (so you have all the symbols) and add some global error hanlding and get the exact stack trace...
I use David Strattons second solution (run as Administrator) because my application requires administrator privileges (-> elevated).
Another solution could be to start the application as the user and use "Debug | Attach to process..."

Resources