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