1

I am new to C programming and I'm currently developing a simple Win32 console application to help me learn. I'm doing this at work and they have provided me with Visual Studio 2010. Our work computers are restricted so I do not have the option to install additional software or make changes which require admin access.

While attempting to debug my program, I discovered that there are several options available. The two below are the ones I want to use:

  1. Start Debugging
  2. Start Without Debugging

When I pick option 1, the application is launched directly (as if I'd double clicked the .exe file). However, when I pick option 2, the application is launched indirectly via cmd.exe. Also, at the point where the application would normally terminate, a line of text comes up which says 'Press any key to continue'. That keeps the console open until I press another key.

Is there a way to to modify the settings in Visual Studio so that option 1 behaves the same as option 2 but still allows debugging? If not, are there any other workarounds which will accomplish the same thing and can be initiated with a single keyboard shortcut?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Chris D
  • 351
  • 1
  • 13
  • What is "directly" ? – Eugene Sh. Aug 12 '16 at 17:00
  • @EugeneSh. As far as I can tell, it seems as though VS is loading the executable file by calling it directly instead of using the windows command prompt (cmd.exe) to load it. I apologize if my terminology is misleading and/or incorrect but I have very little experience in application development. – Chris D Aug 12 '16 at 17:35
  • If that 'Press any key to continue' prompt before your program exits is all what you need then @BenVoigt's comment to my answer below is actually answer to your question – mvidelgauz Aug 12 '16 at 20:36
  • @mvidelgauz Adding a pause before exit feature into the program is not what I wanted to do. I like loading my console app through the windows command prompt because doing so adds additional functionality (i.e. copy / paste). Also, I like the idea of having a pause automatically added only while debugging. When I posted this question, I assumed there were either some settings in VS or a simple workaround which would allow the Start Debugging feature do do this since the other one could. – Chris D Aug 12 '16 at 20:50
  • I usually leave a breakpoint on the last line of `main`. – Adrian McCarthy Aug 12 '16 at 23:33

1 Answers1

1

Unfortunatelly, I don't know any "out-of-the-box" (see alternative below) way to accomplish the same thing with a single keybard command but the following is what I think could be help you:

  1. Add a call to MessageBox or _getch or something else to your code to pause execution at this point until user clicks a button or presses a key
  2. Start your application using 2nd method ("Without Debugging")
  3. When you see message box or user input prompt created in step 1 DO NOT continue yet
  4. In Visual Studio under menu "Debug" invoke "Attach to process...".
  5. In the list of currently running processes find your application. You can recognize it by message box title or user prompt or by process ID. Select that process and click "Attach" button
  6. After you will see that VS is attached to that process switch to application (cmd window) and click button or press a key to continue execution. From that point your application is running under debugger

If you really need a single command to do all this I think you'll need to write your own VS macro. If you need help with such macro it is probaly needs to be a different question.

[EDIT]

You said in your question that you are with VS2010, so the following is probably not an option for you, but just in case I'll add it here: If you were using VS2013 or (2015) you could use Child Process Debugging Power Tool (another link), which will allow you to specify cmd $(TargetPath) as "command" and then use your 1st option to debug cmd together with its child process, which will happen to be your process (screenshot from VS2012, but the same option exsists in VS versions)

enter image description here

mvidelgauz
  • 2,176
  • 1
  • 16
  • 23
  • Thank you for the information. I tested your step by step approach and that does allow me to attach the debugger after the program has been loaded. However, I am hoping to find a solution that will not require multiple steps each time and pausing the program. I will do some research on VS macros. – Chris D Aug 12 '16 at 18:40
  • Based on the answer given in [this question](http://stackoverflow.com/questions/21762194/macros-dont-run-in-visual-studio-2010), VS macros will not be an option for me. – Chris D Aug 12 '16 at 19:53
  • So how about going beyond macros to [VS extenstions](https://code.visualstudio.com/docs/extensions/overview)? – mvidelgauz Aug 12 '16 at 20:10
  • Wouldn't it be easier to add a blocking read at the program exit, and then start debugging as normal? – Ben Voigt Aug 12 '16 at 20:14
  • @BenVoigt Sorry, I am not sure I understand what your mean. Can you please elaborate? – mvidelgauz Aug 12 '16 at 20:19
  • The desired behavior according to the question is "keeps the console open until I press another key". You can do that by adding your `_getch()` or similar call as the last action the program takes. – Ben Voigt Aug 12 '16 at 20:21
  • @BenVoigt Yes, but the question is how to attach debugger to the process while seeing it in cmd window. In other words, OP wants to debug their software while it is running under `cmd`'s control and not directly under VS. They want (for whatever reason) see that black `cmd` window – mvidelgauz Aug 12 '16 at 20:24
  • @BenVoigt Oh, now I see what you mean. It is very well possible that 'Press any key to continue' prompt is entire reason why OP wants all this. (Currently it is provided by `cmd`). So what you wrote could be a good answer to the question asked. If I were you I would post it as such ))) (In fact, I am doing it myself quite ofthen :-) ) – mvidelgauz Aug 12 '16 at 20:28
  • @mvidelgauz VS extensions look cool but I would need to learn how to develop them first. At present, I need to keep my focus on C programming. Thank you for all of the info though. I truly do appreciate it. – Chris D Aug 12 '16 at 21:10