I Get It Windows, Something Is Locking the File, But What Exactly?

My development environment  consists of several processes running together simulating various server and client applications.  Add a couple of open command prompts to the mix  and  the message:

The process cannot access the file because it is being used by another process.

appears every once in a while.

Windows wouldn’t tell which process is holding your file hostage.  That’s for it to know and you to find out.

In this post I introduce two tools that will help you interrogate the OS for  the  information:

Process Explorer

a free utility from Microsoft.   No installation required,  download it extract it from the zip archive and run.

Click Find  or enter Ctrl-F

image

In the find dialog, you can enter the name of the locked file.  After clicking search,  you can have the name and PID of the offending process.

image

LockHunter

This  freeware windows explorer extension that will  not only find the locking process, it will also let you kill that locking process or force unlocking the file.

You can download the file from lockhunter.com.  Once Installed you can right-click on a file from windows explorer and select What is locking this file? Or What is locking this folder?

image

Conclusion

I like guessing games as much as  the next person,  I just don’t want them in the middle a  busy work day.

Auto Escaping C++ Strings

Just a few weeks after celebrating C++’s  raw strings.  I found myself writing  regular expressions  in Visual Studio 2012. I was disappointed to find out VS 2012  it did not support raw strings.     It’s streaking just how half-baked VS 2012  is.  I think it safe the assume Microsoft Visual Studio’s marking team insisted  on sticking to a 2-year product release cycle while the technical team protested they need more time to implement the then new C++ 11 standard.  The compromise  was a product with only some of the exciting  new  C++  features in place.  The rest had to wait for VS 2013/2015.   As it turned out one of the casualties were raw strings.

Regex are difficult to read on their on,  writing and reading long escaped regex is a form of torture.   Fortunately the escaping and unescaping  parts follow  simple rules can be done automatically.

tomeko.net  provides tools to escape and unescape a C++ string:

image

The example above escapes a regex matching a positive float enclosed by double quotes:

“\d*\.?\d+”  ->   “\\\d*\\.?\\d+\”

Conclusion

When forced to escape C++ string manually, it’s best to leave this daunting task to a computer  program which will do a faster, better job with less mistake.

Or, be more vocal about upgrading to a newer compiler.

File hunting: the express way (windows)

Searching for files is a recurring part in our work as developers.  It might be messing header file,  a file containing a function  name, a configuration file with a specific string etc.

Everything Search

a recurring scenario for me is cloning a repository from Git and getting this message while trying to compile it: “Cannot open include file: ‘someheader.h’: No such file or directory” 

I start to wonder – do I have this file at all?  the obvious solution is running a search on the file system. It’s not  a terribly long process but still it gets me out of the zone.

Enter  Everything Search – one of the pieces of software I install as soon as possible in a new  Windows development machine.  This little free utility reads the NTFS master file table directly as its starts up. Using the NTFS ready-made index, allows it to be ready for action in an incredibly short time  even when there are hundreds of  thousands of files.  Once the index is read it is updated in real time by getting file system change  notification from  the OS.

a search such as the one  described above, takes as  much time as it takes to enter the file name into the search box. The search results just snap into place covering your entire drive(s).  You can fix the build without breaking  your stride.

this utility is also invaluable when searching for none source files like configuration files and other resource files

image

Files can be matched by partial name ,  by regex  and other useful criteria, found in the search menu.  The  list of files can be sorted by clicking on the column header.

Docfetcher

While search everything is fast, it will not be able to provide you any information about the content your files.
suppose you are working on a team implementing server/client  protocol.

You, on the server side, want to remove a  string value declared in a protocol used  by both client and server. You are pretty sure nobody is using this value in the client sources, so you can safely remove it.  However,  being a responsible developer you do a  text search over  the entire client code – a no brainer albeit discarding task.  Now,   If you could only Google for the string like you do everything else….

as a matter of fact, you can!

Docfetcher is an open source desktop file indexing  utility.  It will  turn a folder and its subfolders  into  a searchable index,  as files  added/removed/modified in  the tracked folders  the index gets updated automatically.  Your text search is completed  in a fraction of the  time it will take you to do a full-text search on your files.

image

creating the index is as easy as adding your source root directory to the search scope, by right-clicking on it:

image

Once you select the directory, you can  add more files types to the plain text file list:

image

Pressing on the (…) button next to the field lets you add additional files extensions  available in the folder tree:

image

You can repeat the process, selecting additional directories – for example any other location where configuration files are stored.

DocFetcher is open source,  so yo can assure yourself by examining the utility source code , that  sensitive files  (like configuration files with database user names and passwords)  are not shared behind your back,    Like the ill-fated Google Desktop from a while ago

In addition to the rapid plain text search, which I find the most useful , DocFetcher has a search query syntax enabling you to easily query your files content. For example  “int number”~5   will find any file where number is up to 5 words apart from int ;    so this search will find both of these lines :

int number;
int index, number, value;
conclusions

While you can easily do both types of  searches with a  plain vanilla  grepdir or find  commands,  I  often find it more productive to use these tools instead, and use my time for actual development.

Online C++ Compilers

I must confess, at first I was skeptical  the usefulness of an online C++ compiler.  While I’m not quite ready to develop an entire project using an online  environment, I do find that an online C++ compiler is a handy tool to have around. I use it as a scratchpad  when I am considering coding strategies, It is also helpful when you’re  collaborating with a colleague  or want to   illustrate a point  without  going to the trouble of creating compiling and linking a small C++ program.

here are few sites I found useful:

cpp.sh:

With its short and easily  remembered URL this online compiler is my go-to tool for quick jotting.
The behind the scenes compiler is g++ (GNU C++ compiler),  the website offers  limited and uncomplicated control over the  compiler switches.

webcompiler.cloudapp.net

This  website is maintained by the visual C++  team at Microsoft,  as  you can expect it hosts the latest version of  Visual C++ (currently 2015).
The web interface is very basic , it lets you  pass  command line arguments to the compiler with some limitation.    However if VC is your primary work tool and you want  to try out  a code snippet, it’s a good tool to try.

tutorialspoint.com:

an online C++ compiler is just a small part of what this website has to offer,  it is an amazing playground for  nearly 100 online languages   IDE’s   no registration needed!
As the site name suggests  there it offers a large selection of online tutorials covering nearly every aspect of software development.
Among  the available IDE’s  there are three   C++ IDEs,  which are in fact the same interface   pre-configured to comply with  three different C++  standard versions.  As you get full access to the  g++ command line you can easily swap between  supported standards at any given “IDE”  simply by changing the  command line’s   -std   switch.  For instance
  g++ -std=c++17 -o main *.cpp  will compile your program with C++ 17 compiler although it is no listed  as an available  IDE.
clicking the compiler button on the top of the editor area, generate a command line in the terminal pane ,  you can modify.

 

ideone.com

This is another site offering a selection of over 60+ online compilers for different  languages. However what makes this website stand out is how easy it is to privately share the source with the entire build environment.   each virtual IDE has its own URL (click run to generate  the unique  address).  There is no need to register or login to share your  code.  Once you share the  URL the receiver can view your code or fork it to a new IDE (with new ULR) to  run or modify it.
the compiler powering the online IDE is  g++  or clang

Rextester

This site takes code sharing to the next level and allows multiple developers to modify  the same code at the same time (think google docs for developers). It offers a simple solution if want to discuss a code  with a  remote colleague without the hassle of sharing your desktop.  there is a  small selection of different languages  online environments. For C++  you have  a choice between g++ , clang  or VC++ 2015 compilers  (at this time).

Onlinegdb

This is a C++ compiler / Debugger combo that will actually let you debug your code online!   the compiler is g++ supporting  C++ 98 and C++ 14.
clicking debug opens a gdb terminal windows where you can some do some online debugging of your program:

Many thanks to Rajan for letting me know about this gem!

final note

Online C++ compilers are a handy tool to have in your virtual toolbox ,  however,  it worth pointing out that the code you are submitting is saved and compiled on an unknown remote machine, you should be wary of submitting sensitive code,  you wouldn’t feel safe posting on an  online C++ discussion forum.