Tip: Automatically create a crash dump file on error

Crash dumps are very useful to debug an application. Recently, I worked on a Visual Studio extension we use in my company. It's very easy to develop such kind of application. However, there are lots of reasons for your extension to crash. Of course you can add lots of try/catch, but you'll for sure forget the good one, so VS will crash. When it's happening, you would like to be able to attach a debugger and see the exception and the stack trace. Instead of attaching a debugger, you can automatically generate a crash dump that you can use to debug the application later.

In Windows, you can configure Windows Error Reporting (WER) to generate a dump when an application crashes.

  1. Open regedit.exe
  2. Open the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
  3. Set the value DumpFolder (REG_EXPAND_SZ) to the directory you want the dump to be created
  4. Optionally, you can prevent WER to keep lots of crash dumps by setting DumpCount (DWORD) to a low number

Maybe you prefer set the configuration using PowerShell:

New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting" -Name "LocalDumps"
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" -Name "DumpFolder" -Value "%LOCALAPPDATA%\CrashDumps" -PropertyType "ExpandString"
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" -Name "DumpCount" -Value 10 -PropertyType DWord

You can also configure WER per application. So, if you want to generate a full dump only for one application, you can create a key for your application under LocalDumps with the configuration you want. If you application is devenv.exe the key name is devenv.exe. This is very simple!

WER configuration

When you application crash, you can go to %LOCALAPPDATA%\CrashDumps:

WER configuration

Find the latest dump and open it using Visual Studio to start debugging the application. By default, it shows system information, the exception that crahes the application, and the list of modules. You can use the action section on the right to start the debugger and find more information about the exception.

Debug crash dump in Visual Studio

Documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181.aspx

Comparing files using Visual Studio Code

In the previous post, I showed you how to use Visual Studio to compare 2 files. I also use Visual Studio Code from time to time. And, as every great IDE, Visual Studio Code also have a great diff tool. As with Visual Studio, you can use it to compare 2 versions of the same file if you use a source control. But you can also compare 2 files from your file system.

Comparing files using the User Interface

  1. Open the 2 files in Visual Studio Code
  2. Right click on one file and click "Select for compare"

  1. Right click on the other file and click "Compare file file1 with file2"

You should see the result:

Comparing files using the command line

  • Using Visual Studio Code
"%ProgramFiles%\Microsoft VS Code\bin\code" --diff file1.cs file2.cs
  • Using Visual Studio Code Insiders
"%ProgramFiles%\Microsoft VS Code Insiders\bin\code-insiders" --diff file1.cs file2.cs

Comparing files using Visual Studio

The diff tool of Visual Studio is very good to compare 2 files. You can use it to compare two versions of the same file directly in Visual Studio. But you can also use it to compare 2 files that are not in a solution.

Using the command line

  1. Open the developer command prompt from the start menu, so devenv.exe is already in the path
  2. Use the following command line:
devenv /diff file1.cs file2.cs

Using the Command Window in Visual Studio

If you have already opened Visual Studio, you can use the Command Window to diff files.

  1. Open the Command Window using the Quick Launch or using the keyboard Ctrl+W, A

  1. Using the command Tools.DiffFiles with the 2 files to compare:
Tools.DiffFiles "file1.cs"  "file2.cs"

You'll get the same result as with the command line:

Windows Console (cmd) Tips

As a developper or an administrator, you often use the Windows Console. Before Windows 10 and Windows Server 2016, it was a pain. The main burden was the 80 columns limitation. But Microsoft has finally decided to make some improvements. The major improvement is the console now support more than 80 columns and common shortcuts such as Ctrl+C and Ctrl+V. Here's are some tips to help you using the console.

Quickly open a cmd from the explorer

You often want to open a console in a specific directory. Instead of using the cd command, you can open it from the explorer. Simply enter cmd in the search bar and press enter. You can focus the search bar by pressing CTRL+L.

Open Windows Console

Of course, you can replace cmd by powershell 😉

Change the transparency (Ctrl+Shift+Mouse wheel)

You can change the transparency of the console using Ctrl+Shift+Mouse wheel or Ctrl+Shift++ and Ctrl+Shift+-. This allows to view the window behind.

Full Screen (F11)

If you are only using the console, such as on Windows Server Core, you can show the console in full screen using F11 or Alt+Enter. This will make your Windows looks like Linux 😃

Windows Console fullscreen

Commands history (F7)

It's nice to avoid typing a command twice. The console keeps an history of all the executed commands, so you can reuse them latter. Navigating using up and down is not so useful as you don't always know which is the previous command. The console has an awesome ui for showing previous commands 😃 You can show it using F7

Windows Console history

Find text (Ctrl+F)

Sometimes you need to search in the output of a command. Of course you can read the whole output and find what you are looking for. But, if the output is very long, you'll spend long minutes. As in many applications, you can find text using Ctrl+F

Windows Console find