Repository: shader-slang/slang
File: source/core/slang-platform.cpp (C++)

Code before:
    }
    return SLANG_FAIL;
}

/* static */ SlangResult SharedLibrary::loadWithPlatformPath(
    char const* platformFileName,
    SharedLibrary::Handle& handleOut)
{
    handleOut = nullptr;
    if (!platformFileName || strlen(platformFileName) == 0)
    {
        if (!GetModuleHandleExA(0, nullptr, (HMODULE*)&handleOut))
            return SLANG_FAIL;
        return SLANG_OK;
    }

    // https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
    HMODULE h = LoadLibraryExA(platformFileName, nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
    // If LoadLibraryExA failed, try again with LoadLibraryA.
    // https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibrarya
    if (!h)
        h = LoadLibraryA(platformFileName);
    // If still not found, return an error.
    if (!h)
    {
        const DWORD lastError = GetLastError();
        switch (lastError)
        {
        case ERROR_MOD_NOT_FOUND:
        case ERROR_PATH_NOT_FOUND:
        case ERROR_FILE_NOT_FOUND:
            {
                return SLANG_E_NOT_FOUND;
            }
        case ERROR_INVALID_ACCESS:
        case ERROR_ACCESS_DENIED:
        case ERROR_INVALID_DATA:
            {
                return SLANG_E_CANNOT_OPEN;
            }
        default:
            break;
        }
        // Turn to Result, if not one of the well known errors
        return HRESULT_FROM_WIN32(lastError);
    }
    handleOut = (Handle)h;
    return SLANG_OK;
}

/* static */ void SharedLibrary::unload(Handle handle)

Review comment (bug):
I think it will be nicer if we leave a comment about why we are retrying.
I like to suggest a comment something like the following,
```
// We try to search the DLL in two different attempts.
// First attempt tries on the directories explicitly specified with AddDllDirectory(),
// If it failed to find one, we will search over all PATH.
// Windows API made two approaches mutually exclusive and we need to try two times.
```

Code after:
    }
    return SLANG_FAIL;
}

/* static */ SlangResult SharedLibrary::loadWithPlatformPath(
    char const* platformFileName,
    SharedLibrary::Handle& handleOut)
{
    handleOut = nullptr;
    if (!platformFileName || strlen(platformFileName) == 0)
    {
        if (!GetModuleHandleExA(0, nullptr, (HMODULE*)&handleOut))
            return SLANG_FAIL;
        return SLANG_OK;
    }

    // We try to search the DLL in two different attempts.
    // First attempt tries on the directories explicitly specified with AddDllDirectory(),
    // If it failed to find one, we will search over all PATH.
    // Windows API made two approaches mutually exclusive and we need to try two times.
    // https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
    HMODULE h = LoadLibraryExA(platformFileName, nullptr, LOAD_LIBRARY_SEARCH_USER_DIRS);
    // If LoadLibraryExA failed, try again with LoadLibraryA.
    // https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibrarya
    if (!h)
        h = LoadLibraryA(platformFileName);
    // If still not found, return an error.
    if (!h)
    {
        const DWORD lastError = GetLastError();
        switch (lastError)
        {
        case ERROR_MOD_NOT_FOUND:
        case ERROR_PATH_NOT_FOUND:
        case ERROR_FILE_NOT_FOUND:
            {
                return SLANG_E_NOT_FOUND;
            }
        case ERROR_INVALID_ACCESS:
        case ERROR_ACCESS_DENIED:
        case ERROR_INVALID_DATA:
            {
                return SLANG_E_CANNOT_OPEN;
            }
        default:
            break;
        }
        // Turn to Result, if not one of the well known errors
        return HRESULT_FROM_WIN32(lastError);
    }
    handleOut = (Handle)h;