Break where the error HRESULT occurs using WinDbg

When debugging, I found an error HRESULT returned: 0x80020003, but I do not know which function is producing this error. Instead of going through the call stack, there is a simple way if you use WinDbg. I can start the application and when WinDbg breaks use the following command (on 64 bit machine): t; z(@rax != 0x0000000080020003) The…


An initialization pattern to avoid problems calling virtual method in constructor

You are not supposed to call virtual methods in a constructor. The reason being that at the point you call a virtual method in the base class, the derived class has not been initialized. For example, class Base { public Base(int input) { VirtualMethod(); } public virtual void VirtualMethod() { } } class Derived :…


What does it mean for a class to be thread-safe?

We often heard the question about whether a class is thread-safe. The answer can be complicated. Effective Java describes 5 levels of thread-safety: Immutable Thread-safe: multiple threads can access the same instance in any way without causing inconsistent state. Conditionally thread-safe: weaker than thread-safe in that some sequences of method calls need external synchronization. For…

Why, in Visual Studio 2008, opening a VSTO Office 2003 AddIn project gets upgrade wizard?

Recently a customer asks an issue about using VSTO (Visual Studio Tools for Office) in Visual Studio 2008. He created an Office 2003 AddIn project, closed it, reopened it, and he is prompted to upgrade. To make it more confusing, the upgrade wizard says the solution is created by a previous version of Visual Studio….


When to catch all exceptions?

One of the general guidelines of exception handling in .Net is to catch the exceptions that you know how to handle and not to catch all exceptions. Even if you intend to re-throw the exception and know the difference between throw ex and throw, it is still better not to do it since throw does…

Determine share mode of opened file handle

If there is a file already opened and you also want to open it, you need to know the share mode: FILE_SHARE_* the previous CreateFile calls have specified. I came across the problem and the first thing I tried is to use Process Monitor, which shows all the flags passed in when CreateFile is called….


To mock or not to mock?

In Wikipedia’s definition of Unit test, it says “Ideally, each test case is independent from the others; mock or fake objects as well as test harnesses can be used to assist testing a module in isolation.” If we follow this definition strictly, whenever the class under test collaborates with other classes, we need to mock…


Waste-less Programming

Waste-less programming is my way of thinking about getting better at programming. We, as programmers, all want to build great software. To do that, we want to be the ones that can hit higher notes. The time and effort we spend are wasted if they do not help us towards those goals. Sometimes waste-less does…


Using EventProviderTraceListener

In .Net framework 3.5, there is a new trace listener, EventProviderTraceListener, which you can use to do ETW tracing. There are good introduction and some intimidating tips. It took me a while to figure out that, to do a simple ETW trace, you can just configure it like a normal trace listener by having an…


Abstract Class or Interface II

In previous post, I was convinced that abstract class was the right choice for the work I am doing. However, when I got deeper into the work, I had to switch to use interface simply because abstract class does not support multiple inheritance. I am creating a hierarchy of pure abstract classes to be accessed…