apolukhin / Boost.DLL

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
110 stars 69 forks source link

Confused by the Win32 `path_from_handle()` function #57

Closed SaltfishAmi closed 2 years ago

SaltfishAmi commented 2 years ago

so it's here and the link is https://github.com/boostorg/dll/blob/a7f356119218a9a6d58fd5ea61cc6bc4c120ac9d/include/boost/dll/detail/windows/path_from_handle.hpp#L35

        // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
        boost::winapi::GetLastError();  // HERE

        // If `handle` parameter is NULL, GetModuleFileName retrieves the path of the
        // executable file of the current process.
        boost::winapi::WCHAR_ path_hldr[DEFAULT_PATH_SIZE_];
        boost::winapi::GetModuleFileNameW(handle, path_hldr, DEFAULT_PATH_SIZE_);
        ec = boost::dll::detail::last_error_code();
        if (!ec) {
            return boost::filesystem::path(path_hldr);
        }

So this really confuses me. AFAIK GetLastError() function only gets the last error, it doesn't reset it.

What does Resetting it manually. mean?

apolukhin commented 2 years ago

Many thanks for the report. The GetLastError() looked wrong, the GetModuleFileNameW function return value is now checked.