| 47 | |
| 48 | #if defined(__NATRON_WIN32__) |
| 49 | static std::string |
| 50 | applicationFileName() |
| 51 | { |
| 52 | // We do MAX_PATH + 2 here, and request with MAX_PATH + 1, so we can handle all paths |
| 53 | // up to, and including MAX_PATH size perfectly fine with string termination, as well |
| 54 | // as easily detect if the file path is indeed larger than MAX_PATH, in which case we |
| 55 | // need to use the heap instead. This is a work-around, since contrary to what the |
| 56 | // MSDN documentation states, GetModuleFileName sometimes doesn't set the |
| 57 | // ERROR_INSUFFICIENT_BUFFER error number, and we thus cannot rely on this value if |
| 58 | // GetModuleFileName(0, buffer, MAX_PATH) == MAX_PATH. |
| 59 | // GetModuleFileName(0, buffer, MAX_PATH + 1) == MAX_PATH just means we hit the normal |
| 60 | // file path limit, and we handle it normally, if the result is MAX_PATH + 1, we use |
| 61 | // heap (even if the result _might_ be exactly MAX_PATH + 1, but that's ok). |
| 62 | wchar_t buffer[MAX_PATH + 2]; |
| 63 | DWORD v = GetModuleFileNameW(0, buffer, MAX_PATH + 1); |
| 64 | |
| 65 | buffer[MAX_PATH + 1] = 0; |
| 66 | |
| 67 | if (v == 0) { |
| 68 | return std::string(); |
| 69 | } else if (v <= MAX_PATH) { |
| 70 | std::wstring wret(buffer); |
| 71 | std::string ret = StrUtils::utf16_to_utf8(wret); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | // MAX_PATH sized buffer wasn't large enough to contain the full path, use heap |
| 76 | wchar_t *b = 0; |
| 77 | int i = 1; |
| 78 | size_t size; |
| 79 | do { |
| 80 | ++i; |
| 81 | size = MAX_PATH * i; |
| 82 | b = reinterpret_cast<wchar_t *>( realloc( b, (size + 1) * sizeof(wchar_t) ) ); |
| 83 | if (b) { |
| 84 | v = GetModuleFileNameW(NULL, b, size); |
| 85 | } |
| 86 | } while (b && v == size); |
| 87 | |
| 88 | if (b) { |
| 89 | *(b + size) = 0; |
| 90 | } |
| 91 | std::wstring wideRet(b); |
| 92 | std::string ret = StrUtils::utf16_to_utf8(wideRet); |
| 93 | free(b); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | #endif // defined(__NATRON_WIN32__) |
| 98 |
no test coverage detected