How to set breakpoint at the very beginning of program execution

c++, visual-studio, visual-studio-2010

Solution

You can do this by adding a registry key to "Image File Execution Options" with the name of your exe. Add a value of type string named "Debugger" and set it to vsjitdebugger.exe to launch the just-in-time debugger dialog. Which then lets you pick one of the available debuggers, including Visual Studio. This dialog is triggered right after Windows has loaded the EXE, before any code starts running.

Here's is a sample .reg file that triggers the dialog when you start notepad.exe. Modify the key name to your .exe:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
"Debugger"="vsjitdebugger.exe"

Problem

How can I stop the program before loading any of the linked DLLs? I've tried to set `LoadLibraryExW` function in the `Break At Function` debugging option and it stops at that function, but before that I have the following in Visual Studio output windows: ``` 'test.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped). 'test.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped). 'test.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Symbols loaded (source information stripped). 'test.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Symbols loaded (source information stripped). 'test.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Symbols loaded (source information stripped). ---- plus about 30 DLLs --- ``` So how can I stop the program in the debugger before loading the `ntdll.dll`? Ok, not before loading, but before executing any of `DllMain` functions and before initializing any of static objects.

Original source