Thomas Sampson

Leave a comment

Visual Studio ‘Find & Replace’ with Regex Groups

TIL; When using Visual Studio ‘Find & Replace’, regex match groups used within the search term, can be referenced numerically in the replacement term using the $ symbol. The example below adds spaces between function parameters:

Test String


Search term


Replacement term

, $1


SomeFunctionCall(param1, param2, param3);

Leave a comment

Finding variable assignments

When searching through an entire codebase for a variable it is often useful to limit the search results to those instances where the variables is assigned a new value (or perhaps when the variable is used IN an assignment). This can be achieved with the following regular expressions…

Find assignments

MyVariableName[ \t\r\n\v\f]*=[^=]

Find usage in an assignment

=[^=][ \t\r\n\v\f]*MyVariableName

Leave a comment

Preventing Multiple Process Instances In C++

Here is a quick C++ class I knocked up earlier this week which can be used to ensure that only a single instance of your application is running at any given time, using a system-wide mutex object along with a unique process identifier to prevent concurrent execution. The implementation provided is Windows only but could easily be adapted for other platforms using pthread_mutex_trylock.

ProcessInstance Class

#include <windows.h>
#include <string>

class ProcessInstance
	ProcessInstance(const std::string& processIdentifier)
		: m_bIsAlreadyRunning(false), m_hProcessInstanceLock(0)
		m_hProcessInstanceLock = ::CreateMutex(0, true, processIdentifier.c_str());
		m_bIsAlreadyRunning = (::GetLastError() == ERROR_ALREADY_EXISTS);


	bool IsAlreadyRunning() const
		return m_bIsAlreadyRunning;

	bool m_bIsAlreadyRunning;
	HANDLE m_hProcessInstanceLock;


ProcessInstance gProcess("my_process_guid_here");
int main(int argc, char* argv[])
		return 0;

Leave a comment

Linking Debug Traces to Source Code

Today I discovered a nice little feature involving the Microsoft Visual Studio output window.

When emitting debug trace information from your application (using OutputDebugString), you can format your output string in such a way that when it is double-clicked in the output window, it jumps your code editor to the spot where it originated. The format which supports this feature is as follows:


So for example:

“C:/TextureManager.cpp(50): [TextureManager] Texture loaded!”

I often find myself copying a line from the output window and using CTRL+SHIFT+F (Find In File) to locate it’s origin. This is a nice way to get around that long winded process and provide a direct link to the file and line. You can of-course use the __FILE__ and __LINE__ pre-processor macros here to automatically format all your debug traces in this way.