From ade8dc1a218e43c3909c4eede58f4e28e09da73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Tue, 20 Nov 2012 14:15:16 +0100 Subject: [PATCH] Fix opening of read-only files on Windows The CreateFile* functions return INVALID_HANDLE_VALUE on error, not NULL. http://article.gmane.org/gmane.comp.kde.devel.taglib/2346 --- taglib/toolkit/tfilestream.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index 16efb579..35300de6 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -56,10 +56,16 @@ namespace { { DWORD access = readOnly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE); + HANDLE handle; if(wcslen(path) > 0) - return CreateFileW(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + handle = CreateFileW(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); else - return CreateFileA(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + handle = CreateFileA(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + + if(handle == INVALID_HANDLE_VALUE) + handle = NULL; + + return handle; } size_t fread(void *ptr, size_t size, size_t nmemb, HANDLE stream)