THE most annoying Visual Studio bug ever

I use “Find in files” a lot. But today it stopped working big time. Even searching for common words like “public” and “namespace” gave absolutely nothing. All I got was “no files were found to look in”. Huh?!?

It turns out it’s a bug in Visual Studio, or actually a bug in Windows which might have been around since 2004. Seriously, they couldn’t fix it in 10 years!!? Cut to sad developer laughing and crying in equal measure 😛

According to SO it’s possible to fix this by trying out various key-combos, none of which worked for this time:
Ctrl + Break
Ctrl + Scroll lock
Alt + Break
Alt + Scroll lock
Break (alone)

No luck with that, so time to break out regedit. Deleting this registry key and then rebooting should solve the problem:

MyComputer\HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\[VS VERSION NUMBER]\Find

Of course, this only solves the problem until it reappears, but that’s how it goes. 


Knockout JS Tips and trick

If you are working with modern web applications, and especially if you are working on the Microsoft stack, you will have heard of the knockout framework. It is very nice MVVM framework for Javascript. Here are some tips and tricks related to knockout which I have come across while working with it:

Inspect the current viewmodel in a Javascript debugger

In those cases where you don’t have a known home for your viewmodel object in the DOM knockout comes to the rescue with the dataFor and contextFor methods. For instance, add a watch on this value in your favorite debugger:


Now you can continue to dot yourself down until you reach your observables.


Fun with TFS

So you have to use TFS at work. Well I do. This post is about all the tips and tricks I come across for working with TFS.

Getting your current changeset number

Sometimes you might want to know what changeset you are on, so that if you get the latest code you have some idea of where you where at before the update (in case you have to go back).

So open the Visual Studio Command line where all the paths are set up for you. Cd to the root of your local TFS workspace. Then use tf.exe:

tf history . /r /noprompt /stopafter:1 /version:W

It will output something along the lines of this (which conveniently displays the current Changeset):

Changeset User           Date       Comment
--------- -------------- ---------- ----------------------------------------
217040    Lars User      23.10.2013 fixed the bug with .. etc ..

Which process is using the serial port in Windows?

Which Windows process is has the COM port open, and is blocking other processes from using the serial port?

Yes you read that right.. COM ports and serial line communications. Serial line communication, whether it is the RS-282 og RS-485 standards, is still in use at thousands, if not more, industrial plants and ships all around the world.

A frequent problem when accessing Windows COM ports by software is that the port is already open and in use by another application. Quite a similar problem as when another process has a IP port open!

Cut to the chase: Sysinternals Process Explorer comes to the rescue yet again! This amazing piece of software can easily find processes which have particular COM ports open. You could probably find this information by manually entering WMI-queries (a SQL-like language for querying Windows about all sorts of system information), but it is so much easier with Process Explorer.

It’s easy as pie:

Ctrl+F in Process Explorer (Find -> “Find by handle or DLL”).

In the search box type “\Device\Serial”. All processes with open Serial lines will appear in the search results!

Searching for a single COM port is of course equally simple, COM1 = \Device\Serial0, and so on.

Note one caveat: if you have this search box open while you are launching a process, then Process Explorer will show “<Non-existent Process>” instead of the proper process name. Simply close the search box and open it again, and Process Explorer will refresh it’s internal state and show the correct process name. As long as the process is alive of course.

Happy process hunting!

When breakpoints cannot be hit in C++ code

Occasionally the debugger will not be able to hit a breakpoint in C++ code. For instance in Visual Studio the breakpoint will be grayed out or something similar. Now, considering that you double checked that the breakpoint is in fact active, here are a couple of reasons why you might have this problem.

1. The debugging symbol files are outdated.  

In Visual Studio these are *.pdb files, while in Rad Studio these have the extension *.tds. A simple way of fixing this is to delete the debugging symbol files and rebuild the solution. Rebuilding should delete and recreate the files if necessary, but to be 120% sure you can delete them yourself.

See also this SO answer Stackoverflow post

 2. The code is not compiled into the executable.

While you are working in the IDE the breakpoint will look fine and dandy, but then it becomes inactive or “un-hittable” the moment you run the executable. The problem may be that the code not included in the executable due to conditional compilation. For instance, consider what happens in the following pseudocode if CALL_INNER is not defined:

class ExampleClass
    void Outer()

    void Inner()
        // Do stuff

Assuming there are no other calls to Inner(), other than the one in Outer(), then any compiler today will optimize away Inner() when CALL_INNER is not defined. So breakpoints in Inner() cannot ever be hit.

Powershell reminders

Another useful tool on Windows is Powershell, but it seems that every time I use it I have to Google the right way of doing things. Probably because I don’t use it every day (or week). Anyhow, it’s a very useful piece of software for those odd tasks that show up sometimes.

What’s great about powershell is that it combines all the usual shell goodness (pipes, redirects, and more) with more or less the whole .NET Framework! Many people have used it for years to automate all sorts of tasks.

Dead easy powershell  commands for working with text data:

Replacing strings (example replaces semicolon with win newlines):

 "one;two;three" -replace ";", "`r`n"

And if you want to keep the semicolons as well:

 "one;two;three" -replace ";", "$&`r`n"

Sorting files, which is also dead easy:

 type .\messy_file.txt | sort > sorted_file.txt

But there is a catch! sorted_file.txt will end up with unicode encoding, which is not always what you want. Luckily it turns out that the redirect operator > is just syntactic sugar anyway, and the whole thing can be written as

 type .\messy_file.txt | sort | out-file -encoding unicode -filepath sorted_file.txt

The list of supported output encodings are in the docs, but I do believe it supports these at least:

unicode  — (16-bit)

Recursively deleting certain files with powershell

 ls -recurse -filter *.obj | rm

Find X latest modified files

 ls -recurse | sort -property LastWriteTime -descending | select -first 20

Count lines in a file matching pattern

Here i Count all the .cpp files in a Visual Studio 2012 project file:

 Get-Content MyProject.vcxproj
 | Where-Object {$_ -match "<ClCompile.*.cpp`""}
 | Measure-Object

Powershell has the Measure-Object cmdlet, which is much like “wc” in Unix.
The grave accent character ( ` ) is the Powershell escape character.

Find files or directories having a certain name

The Where-Object cmdlet also be used to for matching file or directorynames.

This is an example of finding all the GIT repositories in a directory tree:

ls -recurse -force | where {$_.Name -eq ".git"}

It is important to include the “-force” switch to powershell “ls”, because .git directories are hidden and will not be found unless that switch is included.

Powershell grep : list files containing pattern

 ls -recurse | select-string -pattern "something" -list | select Filename

This simple command will list each file once which contain a specific pattern. The final select is not necessary, but avoids outputting context around each match which is often very useful when searching binary or autogenerated files.

SQLite3 reminders

Time to dust off this old blog again.. I’ve been using the brilliantly light-weight SQLite 3 database on several different projects lately, but there is always some tricks I easily forget. To avoid wasting time with the documentation I’ll write them down here.

So far there is only one:

Unix time to human readable time:

SELECT datetime(1361865600, 'unixepoch');

2013-02-26 08:00:00

Unix time utility site:

Resetting autoincrement columns / fields

SQLite maintains the current largest counter value in the special SQLITE_SEQUENCE table. So really all you have to do is set the “seq” column to zero on the row for your table in SQLITE_SEQUENCE.

E.g. deleting all rows and resetting the counter value: