code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx