diff --git a/win32/File.c b/win32/File.c index 70baf2d..7fb856c 100644 --- a/win32/File.c +++ b/win32/File.c @@ -2,6 +2,10 @@ #include #include +#ifndef MAX_PATH +#define MAX_PATH 260 +#endif + struct CgeFile { HANDLE handle; int flags; @@ -9,6 +13,21 @@ struct CgeFile { int lastError; }; +static int utf8ToWchar(const char *utf8, wchar_t *out, int size) { + int len; + + if (size <= 0) + return 0; + + len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, out, size); + if (len <= 0 || len > size) { + out[0] = L'\0'; + return 0; + } + + return 1; +} + static int errorCodeFromError(void) { DWORD error; @@ -52,6 +71,7 @@ static int errorCodeFromError(void) { static int fileInit(CgeFile *file, const char *path, int mode) { DWORD access = 0, how = 0; + wchar_t pathW[MAX_PATH]; assert(file != NULL); assert(path != NULL); @@ -83,9 +103,12 @@ static int fileInit(CgeFile *file, const char *path, int mode) { } } + if (!utf8ToWchar(path, pathW, MAX_PATH)) + return CGE_FILE_ENAMETOOLONG; + file->flags = 0; file->mode = mode; - file->handle = CreateFileA(path, access, FILE_SHARE_READ, NULL, how, FILE_ATTRIBUTE_NORMAL, NULL); + file->handle = CreateFileW(pathW, access, FILE_SHARE_READ, NULL, how, FILE_ATTRIBUTE_NORMAL, NULL); if (file->handle == INVALID_HANDLE_VALUE) return errorCodeFromError();