Geet
Geet

Reputation: 351

Debugging an executable in visual studio

Want to debug an executable under debugger. How to do it in visual studio.

Windbg has an option of open executable. But I find this is missing in VS 2010.

The question is not exactly same as Debug exe in visual studio 2010 as I am not really interested in image file execution to debug a start up.

Rather want to just debug the exe under debugger, once it is broken, want to set some break points and understand the flow of execution.

Albeit image file execution is a workaround for this.

I am not sure if this question is naive; But this is a very straight line use case scenario, I find missing in MS VS 2010.

Upvotes: 34

Views: 84159

Answers (4)

rrreee
rrreee

Reputation: 893

This is an old question, but I ran into it since debugging whatever command you wanted in versions of Visual Studio before 2022, even with .netcore projects, could be done via the VS gui: you just went to the project properties went to the debug section, and could enter any exe or batch file command even, as the Executable. But in Visual Studio 2022 this became undocumented, and I had to use Visual Studio 2019 to figure out how to do the same in VS 2022. Here's how (and yes VS 2019 will do this for you, but 2022 will not):

In the launchsettings.json file you will find this line :

"commandName": "Project",

Replace it with these:

"commandName": "Executable",
"executablePath: "c:\myexe.exe",

That commandName property can not be set via the VS gui any more, but it still works, and the gui will reflect it after you set it in the launchSettings.json file; after you do this, you can use the gui to modify the executablePath and commandLineArgs, but not the commandName, other than to reset it to the default.

And to specify arguments (which the 2022 will help you with) add this:

"commandLineArgs": "arg1 arg2"

And that should get you going.

Upvotes: 1

matra
matra

Reputation: 10183

You did not specify it in the question, but I assume you do not have the source code. Just use File/Open Project/Solution, select EXE file and Open it. Then select Debug/Start debugging.

The other option is to run the EXE first and then Select Debug/Attach to process.

Upvotes: 45

Kraken
Kraken

Reputation: 68

If you have the source code, you can use Debugger.Launch();

You put it anywhere in your code, build the .exe and then once it gets launched (by Process.Start for example) and reaches Debugger.Launch();, a window will be asking you how you want to attach to the process.
enter image description here

Typically, you'll attach to some Visual Studio instance and it'll automatically pause the debugger where Debugger.Launch(); has been placed.

You can then open the project's files (File -> Open -> File...) and place breakpoints wherever you want.

Upvotes: 2

gadildafissh
gadildafissh

Reputation: 2402

If what your asking is how do you attach VS to the exe you want to run then you can follow these steps:

  1. Run the executable
  2. In VS navigate to Debug -> Attached to Process
  3. Find your process created by running your executable and click "attach".

However, if the executable you are trying to run fails almost immediately or runs quickly and exits then you could try the following steps:

  1. Set a debug point at the start of the code
  2. Switch your build to Debug and run the application.

If your application is running in Debug, but failing when you execute the exe then you could try these steps to see if the your app will give more information in a console window or other.

  1. Make sure your build is set to Release.
  2. Navigate to Debug -> Start Without Debugging

Upvotes: 3

Related Questions